Task for changing the AndroidTest runner - android

I'm trying to setup a task that will setup a different runner in the android gradle plugin.
My idea is to have something like:
./gradlew differentRunner connectedAndroidTest
That would run androidTests with a different runner.
What I have so far:
task differentRunner << {
project.android.defaultConfig.testInstrumentationRunner = "com.example.DifferentRunner"
println 'Different runner set.'
}
But, even though I see the log printed when issuing the above mentioned command, tests are run with the runner set in defaultConfig.
How could I achieve this?
P.S. I know I could use flavours, but those are just not an option for me, I would like to setup a custom task.

I understand you don't want to use flavors etc to accomplish this, but if you're flexible on using a custom task then properties may be a viable option.
testInstrumentationRunner project.hasProperty('customRunner') ? 'com.example.DifferentRunner' : 'default runner'
and then to run using custom runner:
./gradlew -PcustomRunner connectedAndroidTest

Related

Run all UI/Unit tests from a multi-module project using a single command

We have a multi-module Android project, in which some modules contain UI tests and some contain Unit tests. We wish to run all UI tests from all modules using a single Gradle command and do the same thing for Unit tests.
The only way that we found to do this was with the following config inside the main app module that implements all the other sub-modules (basically make the app module know about all the other androidTest and test folders in the project):
app/build.gradle:
sourceSets {
androidTest.java.srcDirs += ["${project(':feature-login').projectDir}/src/androidTest/java"]
test.java.srcDirs += ["${project(':feature-login').projectDir}/src/test/java"]
test.java.srcDirs += ["${project(':core').projectDir}/src/test/java"]
}
Then we run the following Gradle commands:
./gradlew app:connectedDemoDebugAndroidTest
./gradlew app:testDemoDebugUnitTest
Question: Is there a better/simpler way to achieve this? Or is there a way to add the androidTest and test folders from the above solution dynamically (using a relative path), instead of having to write the srcDirs line for each and every module (we have 40+ modules)?
just run in terminal:
./gradlew test
Please, see docs for more options
I do that every day (I usually have a source set for unit tests and one for slower integration tests): ./gradlew check. It runs all test tasks.
From the gradle docs:
check
Depends on: test
Aggregate task that performs verification tasks, such as running the tests. Some plugins add their own verification tasks to check. You should also attach any custom Test tasks to this lifecycle task if you want them to execute for a full build. This task is added by the Base Plugin.

Exclude Gradle #LargeTest

I am trying to run unit test on a separate task from the UI tests that I have within the integration tests in Android Studio, unfortunately I have to use
apply plugin: 'com.android.application'
in the build.gradle file so I cannot add the custom test tasks as far as I can tell. Since the UI tests are tagged as "#Test" and extend InstrumentationTestCase they get run whenever
gradle connectedCheck
is called which is not needed, instead I want one gradle command to run UI tests and one to run unit tests. I figured that I would be able to leverage tagging the UI tests as LargeTests but have not been able to complete a gradle task that can do this. I am not able to use the "test" task in the build.gradle since we are using the com.android.application plugin, and advice?
Thanks
You can do this from command-line without modifying the build.gradle file:
./gradlew cAT -Pandroid.testInstrumentationRunnerArguments.notAnnotation=android.test.suitebuilder.annotation.LargeTest
What ended up working for me is adding the
#LargeTest
using
import android.support.test.filters.LargeTest;
annotation to the tests I needed and then adding the following lines to the build.gradle
if(!project.hasProperty('android.testInstrumentationRunnerArguments.annotation')) {
testInstrumentationRunnerArgument 'notAnnotation', 'android.support.test.filters.LargeTest'
}
this way unless I specify in the command line to run the large tests they will,be ignored. To run the large tests use:
gradle cAT -Pandroid.testInstrumentationRunnerArguments.annotation=android.support.test.filters.LargeTest

Jvm options in android when run gradlew test

I have a project that using Robolectric for unit test purpose. This project uses Robolectric 3.0 and need to add -ea and -noverify options in Virtual Machine options.
In Android Studio, I created new JUnit configuration in Run > Edit Configurations... and then set VM Options to -ea -noverify. With this way I success to run my unit test. This is image about my configure, view Here
However, for continuous deployment, I need run unit test with command line. So I use ./gradlew test to run unit test. I also add org.gradle.jvmargs=-ea -noverify to gradle.properties file. Unfortunately, it doesn't work. I can run unit test but I got java.lang.VerifyError and I think that gradle.properties was not load.
So, my question is, how to make gradle.properties load or do you know any way to fix my vm options problem?
It is already answered but this may be an easier solution:
In your application modules' build.gradle file in android closure, add this.
android {
....
testOptions {
unitTests.all {
jvmArgs '-noverify'
}
}
}
I found that we can add this block to app's build.gradle to solve this problem
tasks.whenTaskAdded { theTask ->
def taskName = theTask.name.toString()
if ("testDevDebug".toString().equals(taskName)) {
theTask.jvmArgs('-ea', '-noverify')
}
}
DevDebug is my build variant.
Maybe this
./gradlew -Dorg.gradle.jvmargs="-ea -noverify" test

Android unit test support does not work in android library modules

I am writing junit tests on android project using the new unit test support http://tools.android.com/tech-docs/unit-testing-support.
While the unit tests run on the 'com.android.application' module perfectly but they always fail on the 'com.android.library' modules. This has not been documented in http://tools.android.com/tech-docs/unit-testing-support . So I wonder whether I am the culprit.
When I write those tests on library modules, the tests can not find the classes on the module and always gives following errors:
package does not exist
error: cannot find symbol
The android unit test support is in experimental phase right now, but is there a solution to it.
UPDATE
I have added this issue to android issue tracker https://code.google.com/p/android/issues/detail?id=161038
It looks like the task to compile the unit tests doesn't depend on the task to compile the library code.
The following fixed it for me:
afterEvaluate {
tasks['assembleDebugUnitTest'].dependsOn(tasks['assembleDebug'])
}
I run the tests using
./gradlew testDebug
If you don't want to modify your build.gradle, manually specify the assembleDebug task on the command line should also do the trick:
./gradlew assembleDebug testDebug
In my android library project I also failed to get the tests running. What I did was create a test application that uses the library and wrote tests in the application that call the library methods.
This might not be the ideal solution, but was the way we got this to work.
Have a look over here https://github.com/nenick/AndroidStudioAndRobolectric
There you can run unit tests on libraries and flavors. And no you don't need to use Robolectric as Gaurav Vashisth stated. You can if you want to.
Here is an example of JUnit test in a library module

Android gradle test framework: single class

Is it possible to run a single test class using the new Android gradle build framework?
I have a test package that has multiple test classes (All of them are InstrumentationTestCase classes). I've been able to setup my build.gradle file to run the test package
defaultConfig{
testPackageName "com.company.product.tests"
testInstrumentationRunner "android.test.InstrumentationTestRunner"
}
But is there a way to test only one test case in that package? Otherwise I'll be using the age old adb shell am instrument -w .......
P.S. I don't have time right now to switch to Roboelectric, but I do see that its pretty much the defacto framework nowadays.
Using android.test.InstrumentationTestRunner no, this is not possible. You do, however, have options:
Custom Test Runner
Extend android.test.InstrumentationTestRunner
Add a buildConfigField 'String', 'TEST_NAME', '${testName}', where testName is '"${project.properties.get("test")}"' if set, otherwise null
In your runner, only run tests that match BuildConfig.TEST_NAME (if null, run all tests)
Replace the testInstrumentationRunner with your custom runner
Run tests with ./gradlew connectedCheck -Ptest=com.example.Test.testSomething
Use Spoon
Spoon is an excellent test runner extension that, among other things (like beautiful multi-device test reports), lets you run individual tests. Since you're using gradle, I recommend the Spoon Gradle Plugin, which has an example of exactly what you want to do in its README.
Update: Run Individual Unit Tests with Android Gradle Plugin
With the addition of unit testing support, Android now supports running individul unit tests.
This is just an anchor task, actual test tasks are called testDebug and testRelease etc. If you want to run only some tests, using the gradle --tests flag, you can do it by running ./gradlew testDebug --tests='*.MyTestClass'.
Source
Edit: I should also add that Spoon is a drop-in replacement for running tests. You will not have to modify anything but a few lines in your build script to use it.
As answered in https://stackoverflow.com/a/32603798 there is now
android.testInstrumentationRunnerArguments.class

Categories

Resources