【问题标题】:How to debug a single robolectric unit test in Android Studio如何在 Android Studio 中调试单个 robolectric 单元测试
【发布时间】:2014-12-22 20:36:51
【问题描述】:

如果我使用以下步骤运行所有单元测试,我可以调试我的 android gradle 项目:

首先使用 Gradle 运行/调试配置配置 IDE,以便使用 Robolectric 和 JUnit 进行单元测试

  1. 在 IDE 菜单中执行:File / Settings / Compiler / Gradle :取消选中“Use in-process build”
  2. 在 IDE 中执行:运行/编辑配置/+/Gradle
  3. 在字段中输入以下数据:名称:test,Gradle 项目:.../app/build.gradle,任务:test,脚本参数:--debug --stacktrace

接下来,从 IDE 运行/调试测试:

  1. 从配置中选择“测试”
  2. 运行:运行/运行“测试”
  3. 调试:运行/调试“测试”

这允许我在调试器中运行所有测试。

我现在正在寻找一种方法来做同样的事情,但仅限于一个指定的测试。任何建议都会很棒。

【问题讨论】:

  • 你做了所有的步骤来调试 android studio 中的一个测试?哪个插件使用你?一些插件在 android studio 中为您提供完整的 robolectric 支持,例如 github.com/JCAndKSolutions/android-unit-test

标签: android debugging gradle android-studio robolectric


【解决方案1】:

这取决于您使用的 gradle 版本。如果您使用的是 1.x 版本,则将以下脚本参数添加到您的运行配置中

-Dtest.single=<testfilename> 

例如

-Dtest.single=MyTest

您不必担心文件的路径 - 只需包含包含测试类的文件的名称,而无需尾随 .java

如果您使用的是 gradle 2.x,则将以下脚本参数添加到您的运行配置中

--tests <test class name>

例如

--tests com.example.data.MyTest

或 --tests *.MyTest

使用通配符,您不仅可以运行一个测试,还可以运行一部分测试

--tests com.example.data.*

【讨论】:

  • 我正在使用 gradle 1.12 并且 -Dtest.single=MyTest (将 MyTest 替换为没有包资格的 impl 特定测试类)不起作用。改为运行所有测试。我尝试同时指定为 VM 选项和脚本参数,但都没有帮助。
  • 上述答案中与 gradle 2.x 相关的部分对我有用。与 gradle 1.x 相关的部分没有。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多