Separation block and integration tests for Android

Currently, I have a test/src/java folder where all the tests for the Android application are stored (tests are performed using junit, mockito and robolectric).

And I can run those using ./gradlew test

I would like to get two folders:

  • integrationTest/src/java - for integration tests
  • test/src/java - for unit tests

And also I would like to run them separately, for example ./gradlew test and ./gradlew integrationTest .

I was able to split test directories using sourceSets as follows:

  sourceSets { test { java { srcDirs = ['src/test/java', 'src/integrationTest/java', 'src/commonTest/java'] } resources { srcDirs = ['src/test/resources', 'src/integrationTest/resources', 'src/commonTest/resources'] } } } 

And I had many examples of how to create custom test tasks, but most of them are related to Java, not Android, and others are outdated. I spent the whole day on this, and therefore, if someone can help me, I would really appreciate it.

+8
source share
4 answers

If your integration tests are instrumental tests, you can simply use the default folders test and androidTest and run them separately using ./gradlew test and ./gradlew connectedAndroidTest

Another way (if you can have integration tests inside the test folder) is to use separate packages inside the test folder and run the tests separately using:

 ./gradlew testDebug --tests="com.yourapplication.unittests.*" ./gradlew testDebug --tests="com.yourapplication.integrationtests.*" ... 
+3
source

I had the same problem a few days ago.

To solve this problem and be able to run each type of test myself, I separated my tests as follows:

 // run only unit tests test{ include '**/unit/**' exclude '**/integration/**' doLast { println 'Unit tests execution finished.' } } // run only integration tests task integrationTest(type: Test){ include '**/integration/**' exclude '**/unit/**' doLast { println 'Integration tests execution finished.' } } // run all tests (unit + integration) task allTests(type: Test){ include '**/integration/**' include '**/unit/**' doLast { println 'All tests execution finished.' } } 

The include keyword indicates which files you want to include when executing commands. If you want to run only your unit tests, you can only include folders (s) that include your unit tests and exclude folders containing your integration tests.

You can use the same logic when creating the gradle command to run only your integration tests.

To run your tests using this configuration and gradle, use:

  • ./gradlew test to run unit tests only .
  • ./gradlew integrationTests perform ./gradlew integrationTests tests only .
  • ./gradlew allTests perform both integration and unit tests.

NOTE. . You can configure the paths in include / excludes to include / exclude tests or classes when running your tests. It is also possible to include only one test and exclude the rest and vice versa.

+2
source

Maybe something like

 sourceSets { integrationTest { java { compileClasspath += main.output runtimeClasspath += main.output srcDir 'src/integrationTest/java' } resources.srcDir 'src/integrationTest/resources' } } configurations { integrationTestCompile { extendsFrom compile } integrationTestRuntime { extendsFrom runtime } } task integrationTest(type: Test) { testClassesDir = sourceSets.integrationTest.output.classesDir classpath = sourceSets.integrationTest.runtimeClasspath } check.dependsOn integrationTest 

Then you could do

 dependencies { integrationTestCompile 'org.seleniumhq.selenium:selenium-java:3.0.1' integrationTestRuntime 'com.foo:bar:1.0' } 
0
source

I found that code snippets include / exclude unit tests that do not work on Android submodules.

See my answer here, how to separate unit and integration tests in Android modules: fooobar.com/questions/13665495 / ...

0
source

Source: https://habr.com/ru/post/1013123/


All Articles