I have the following tests in project/src/java/test.project.example/. When i commit some file to repository teamcity check this and build apk file. So i want that teamcity run all tests from project/src/java/test.project.example/ too. How i can do it?
Examples of tests:
public class ContentIdsParserTest extends AndroidTestCase {
public void testParser() {
assert(somethingExp,somethingReal);
}
}
When specifying which gradle tasks to run in your CI configuration, add testRelease (or testDebug or whatever particular build variant you want) to the list of tasks. This will run the unit tests. See the Android unit test documentation for more details.
Related
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.
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
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
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
I have a question about running TestSuites with gradle.
So under androidTest/java I have two packages, the first one contains instrumentation tests (ActivityInstrumentationTestCase2) and the other is a TestSuite which contains those instrumentation tests. If I run the test suite in Android Studio, it works fine. How can I run it from gradle? I mean, I would like to run it from Jenkins. What should be that gradle task? I have tried with connectedAndroidTest, and it runs the instrumentation test in non-specific order, instead of the test suite.
You can add filters to choose your tests. If you only chose your test suite, you can use the normal test task provided by gradle.
test {
filter {
includeTestsMatching "*MyTestSuite"
}
}