【问题标题】:Run specific junit4 categories using gradle使用 gradle 运行特定的 junit4 类别
【发布时间】:2017-07-31 13:58:25
【问题描述】:

我正在使用 gradle 在 Android 手机上运行一些测试。但是我希望能够选择要运行的测试。对于我使用 jUnit4 和类别的测试。

这是从 jenkins 构建和执行测试的方式:

call gradle assembleDebug assembleDebugAndroidTest
call gradle connectedDebugAndroidTest

这是一个测试的样子:

@Category(IncludeTest.class)
@Test
public void test_Test_06() throws Exception {
    TestData.setUpTest("test_Test_06");
    Log.d("Test: Test 6 included");
}

@Category(ExcludeTest.class)
@Test
public void test_Test_07() throws Exception {
    TestData.setUpTest("test_Test_07");
    Log.d("Test: Test 7 excluded");
}

在我的 gradle.build 中,我尝试了以下但没有成功:

test {
  useJUnit {
    includeCategories 'com.abc.def.IncludeTest'
    excludeCategories 'com.abc.def.ExcludeTest'
  }
}

我的结构如下:

/someFolder/gradle.build
/someFolder/app/src/android/java/

在 java 中,我有一个名为 com.abc 的包,在该包中还有另一个包 def > 我的 IncludeTestExcludeTest 接口在哪里。

我在 gradle.build 中尝试了包含/ExludeTest 的不同路径,但它不起作用,所有测试都始终执行。

我还尝试将 includeCategories/excludeCategories 放入任务中,并确保该任务已实际启动。但仍然执行了所有测试。似乎 includeCategories/excludeCategories 什么都不做。

有什么基本的我做错了吗?还有其他选择类别的方法吗?

【问题讨论】:

    标签: java android jenkins gradle junit


    【解决方案1】:

    经过更多研究,我发现 includeCategories/excludeCategories 不适用于 Android。

    我在使用 AndroidJUnitRunner 时发现了一个不同的解决方案。在 Gradle 中,可以过滤注释上的测试。要包含注释:

    call gradle connectedDebugAndroidTest -Pandroid.testInstrumentationRunnerArguments.annotation=com.abc.def.IncludeTest
    

    也可以使用 notAnnotation 来排除注释。

    当然,如果您想使用带有 OR/AND 组合的多个注释,还有语法。

    如果您不使用 connectedDebugAndroidTest,以下也是可能的:

    adb shell am instrument -w -e annotation com.android.foo.MyAnnotation com.android.foo/android.support.test.runner.AndroidJUnitRunner
    

    一些文档:https://developer.android.com/reference/android/support/test/runner/AndroidJUnitRunner.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-11
      • 2011-12-06
      • 2017-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多