Raffe81
Raffe81

Reputation: 141

Run specific junit4 categories using gradle

I am running some tests on an Android phone using gradle. However I would like to be able to select which tests to run. For the tests I am using jUnit4 and categories.

This is how the tests are build and executed from jenkins:

call gradle assembleDebug assembleDebugAndroidTest
call gradle connectedDebugAndroidTest

This is how a test looks like:

@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");
}

In my gradle.build I have tried the following without success:

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

My structure is as follows:

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

In java i have a package named com.abc and in that package there is another package, def where my IncludeTest and ExcludeTest interfaces are.

I have tried different paths to Include/ExludeTest in gradle.build but it just does not work, all test all always executed.

I have also tried putting the includeCategories/excludeCategories in a task and made sure the task was actually started. But still all test were executed. Just seems like includeCategories/excludeCategories does not do anything.

Is there anything basic I am doing wrong? Are there any other ways of selecting categories?

Upvotes: 3

Views: 858

Answers (1)

Raffe81
Raffe81

Reputation: 141

After some more research I found out that the includeCategories/excludeCategories does not work with Android.

I found a different solution when using AndroidJUnitRunner. In Gradle it is possible to filter tests on annotations. To include annotations:

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

There is also the possibility to exclude annotations using notAnnotation instead.

And of course there is syntax if you want to use multiple annotations with OR/AND combinations.

The following is also possible if you are not using connectedDebugAndroidTest:

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

Some documentation: https://developer.android.com/reference/android/support/test/runner/AndroidJUnitRunner.html

Upvotes: 1

Related Questions