简体   繁体   English

如何在Android Studio中调试单个robolectric单元测试

[英]How to debug a single robolectric unit test in Android Studio

I am able to debug my android gradle project if I run all the unit tests using the following steps: 如果我使用以下步骤运行所有单元测试,我可以调试我的android gradle项目:

First configure IDE With a Gradle Run/Debug config for Unit Testing Using Robolectric & JUnit 首先使用Grabo Run / Debug配置IDE,使用Robolectric和JUnit进行单元测试

  1. in IDE menu do: File / Settings / Compiler / Gradle : Uncheck "Use in-process build" 在IDE菜单中执行:文件/设置/编译器/ Gradle:取消选中“使用进程内构建”
  2. In IDE do: Run / Edit Configuration / + / Gradle 在IDE中执行:运行/编辑配置/ + / Gradle
  3. Enter the following data in fields: Name: test , Gradle project: .../app/build.gradle, Tasks: test, Script parameters: --debug --stacktrace 在字段中输入以下数据:名称:test,Gradle项目:... / app / build.gradle,任务:测试,脚本参数: - debug --stacktrace

Next, to run / debug tests from IDE do: 接下来,从IDE运行/调试测试:

  1. Choose "test" from configuration 从配置中选择“测试”
  2. To Run: Run / Run 'test' 运行:运行/运行'test'
  3. To Debug: Run / Debug 'test' 要调试:运行/调试'test'

This allows me to run all the tests in the debugger. 这允许我在调试器中运行所有测试。

I am now looking to find a way to do the same but for a single specified test only. 我现在正在寻找一种方法来做同样的事情,但只针对一个指定的测试。 Any suggestions how would be great. 任何建议怎么会很棒。

It depends on the version of gradle you are using. 这取决于您使用的gradle版本。 If you are using version 1.x, then add the following script parameter to your run configuration 如果您使用的是版本1.x,请将以下脚本参数添加到运行配置中

-Dtest.single=<testfilename> 

eg 例如

-Dtest.single=MyTest

You don't have to worry about the path to the file - just the name of the file containing the test class without the trailing .java 您不必担心文件的路径 - 只是包含测试类的文件的名称而没有尾随.java

If you are using gradle 2.x, then add the following script parameter to your run configuration 如果您使用的是gradle 2.x,请将以下脚本参数添加到运行配置中

--tests <test class name>

eg 例如

--tests com.example.data.MyTest

or --tests *.MyTest 或--tests * .MyTest

With wildcards you can run not just one test but a subset of your tests 使用通配符,您不仅可以运行一个测试,还可以运行测试的一个子集

--tests com.example.data.*

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM