Exclude Gradle #LargeTest - android

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

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.

Task for changing the AndroidTest runner

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

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 Instrumentation test does not find classes from library project

So I'm getting a NoSuchMethodError when running my Activity/instrumentation tests from Android Studio, on the code line which tries to call a method in a library module from the unit test.
So this is my test:
public class MainActivityTest extends ActivityInstrumentationTestCase2 {
public void testMainActivity() {
final MainActivity target = (MainActivity) getActivity();
MyLibarary.someStaticMethod(); // yields java.lang.NoSuchMethodError
}
What's the deal here? I've defined my library as a "compile"-dependency in build.gradle, and it's compiling just fine. The library is also invoked from the main app classes without problems. It's only when I call it from the tests it fails. My app and my tests are in the same module.
I've tried running the clean task, the assembleDebug and assembleDebugTest tasks manually. No avail.
Project structure:
Root
|---MyApp
| |---src/main/...
| |---src/androidTest/...
|----MyLibrary
Running Android Studio v1.0.2
Gradle build tools v1.0.0
Running as an "Android Test" on module "MyApp" from the Run/Debug configurations of AS with default Instrumentation test runner.
Ok this one was a bit tricky. Keyword: proguard minify. Since the newly implemented method so far only was used by the instrumentation test, proguard didn't pick up on its usage and therefore removed it from the DEX in the proguardDebugTest build step.
Solution: Either disable minification in the debug build (In gradle: android.buildTypes.debug.minifyEnabled false), or use the method in the main app.
Not really up-to-date with Gradle. But i think we are supposed to specify the testCompile or the androidTestCompile dependency as well in the build.gradle if trying to write instrumentation tests.
Helpful Links:
http://gradle.org/docs/current/userguide/java_plugin.html
Specifying test dependencies with the Gradle Android build system
Hope this helps

Gradle Android testing

I'm new to gradle and Android Studio, and I'm trying to figure out how to run tests. I followed instructions in http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Testing and I'm able to run Instrument Tests but only executing /gradlew connectedInstrumentTest.
I'm having troubles understanding the other check tasks, that don't execute any code in my app.
This is what ./gradlew tasks prints
...
Verification tasks
------------------
check - Runs all checks.
connectedCheck - Runs all device checks on currently connected devices.
connectedInstrumentTest - Installs and runs the tests for Build 'Debug' on connected devices.
deviceCheck - Runs all device checks using Device Providers and Test Servers.
...
If check runs all checks... shouldn't it run the ones I get from connectedInstrumentTest?
Also, how can I run tests that don't need the Android environment? Should I place them under /test/java ? If so, what command should I run to execute them?
Sorry if these questions seem very obvious, but I just haven't been able to find any answer to these questions in the docs.
Thank you!
Edit:
So I have made some progress here.
It looks like check is not doing anything. It would be (right now) up the developer to add dependant tasks to check to run some JUnit tests. You will need to have to create a task, make it find the sources, compile them and run them.
connectedCheckand connectedInstrumentTest: runs instrumentationTest in the device. (this always worked).
deviceCheck: This is useful, as the docs say, for Continuos integration testing.
If check runs all checks... shouldn't it run the ones I get from connectedInstrumentTest?
you can have to run connectedInstrumentTest task as dependency to check task.
check.dependsOn connectedInstrumentTest
How can I run tests that don't need the Android environment?
For android projects we can discussed about 3 types of Tests
Junit Test
Robolectric Test
Instrument Test
Junit test
We can't use plain junit test to check android related classes. What we can do is separate core java classes to a java library project and add that dependency to android project.
Robolectric Test
We can use robolectric test to run the unit tests outside of the Emulator. This makes the tests fast and easy to configure with CI servers.
To run robolectric test we use gradle-android-test-plugin
It clearly describes how to use that plugin.
Project structure
We have to use default folder structure in order to use this plugin. We have to use folder called 'test' to keep robolectric tests:
MyProject/
| settings.gradle
| build.gradle
- app/
| build.gradle
-main
-java
-com.example.calculator
-test
-java
-com.example.calculator.robolectrictests
build file is
buildscript {
repositories {
mavenCentral()
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots/'
}
}
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
classpath 'com.squareup.gradle:gradle-android-test-plugin:0.9.1-SNAPSHOT'
}
apply plugin: 'android'
apply plugin: 'android-test'
repositories {
mavenCentral()
}
android {
compileSdkVersion 19
buildToolsVersion "19.0.0"
defaultConfig {
minSdkVersion 8
targetSdkVersion 19
}
}
dependencies {
compile 'com.android.support:appcompat-v7:+'
testCompile 'junit:junit:4.10'
testCompile 'org.robolectric:robolectric:2.1.+'
testCompile 'com.squareup:fest-android:1.0.+'
}
This test task will automatically executes with the check task.
Instrument Test
As you mentioned, this requires using the android emulator. That makes the tests slow, meaning that they're not a good way to do TDD.
We can use robolectric test as unit tests in the TDD process.
Instrument test we can use as integrated test in the TDD.
check SHOULD run instrumentation tests IIRC. About running non android tests. Currently the android-gradle plugin doesn't support it. I made a plugin however to run JUnit tests with robolectric so you don't need instrumentation tests. Here is the link: android-unit-test

Categories

Resources