Currently I have a test/src/java
folder where all of the tests for the android application are stored (tests are done using junit, mockito and robolectric).
And I can run those using ./gradlew test
What I'd like to achieve is having two folders:
integrationTest/src/java
- for integration teststest/src/java
- for unit testsAnd also I'd like to run them separately, like ./gradlew test
and ./gradlew integrationTest
.
I've managed to split directories with tests using sourceSets
like this:
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 googled many examples on how to create custom test tasks, but most of them are related to java instead of android and the others are out-of-date. I've spent on that the whole day now and so if someone can help me I would really appreciate that.
Integration tests verify the way different parts of your app work together. They are slower than unit tests, and should therefore only be used when you need to test how things interact. When interacting with the Android framework you can rely on an Android device or emulator, or use Robolectric.
Keep your testing suites separateIntegration tests should not be run together with unit tests. Developers working on specific business logic in the code must be able to run unit tests and get near-immediate feedback to ensure that they haven't broken anything before committing code.
Unit Testing is a kind of white box testing, whereas Integration Testing is a kind of black-box testing. For Unit Testing, accessibility of code is required, as it tests the written code, while for Integration Testing, access to code is not required, since it tests the interactions and interfaces between modules.
And what are they using it for? Even after 20 years of JUnit's existence it is still mostly being used for unit tests. If you are lucky you may see an integration test or two. JUnit is, in fact, much more than a unit testing framework.
If your integration tests are instrumented tests, then you can just use the default folders test
and androidTest
and run them separately using ./gradlew test
and ./gradlew connectedAndroidTest
Another way (if you can have the integration tests inside the test
folder) would be 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.*"
...
I had the same problem a few days ago.
In order to solve it and be able to run each type of test independently, I separated my tests like this:
// 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 the commands. If you want to run only your unit tests, you can only include the folder(s) that include your unit tests and exclude the folders that include your integration tests.
You can use a similar logic when creating a gradle command to run only your integration tests.
To execute your tests using this configuration and gradle, use:
./gradlew test
to execute the unit tests only../gradlew integrationTests
to execute the integration tests only../gradlew allTests
to execute both the integration and the unit tests.NOTE: You can setup the paths in the includes / excludes in order to include / exclude tests or classes when executing your tests. It is also possible to include only one test and exclude the others and vice-versa.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With