Run test suite with gradle - android

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"
}
}

Related

How to execute UI Automator integration tests when building an Android app

I have integrated UI Automator on my app, and in Android Studio I can execute the ExampleInstrumentedTest.kt and the test inside of it are executed.
I would know how can I execute these tests when I build the app with gradlew and not only when I run the test class.
I've tried these solutions:
gradlew
gradlew test
gradlew assembleDebug
But I'm noticing these tests are not executed.
Also I would ask if it's possible that, if some tests are not passed, the build fails or I can't merge the branch the tests are executed into the master repository.
Thanks a lot
You need to use Espresso
Test the UI of a single app
dependencies {
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
}
#Test
fun changeText_sameActivity() {
// Type text and then press the button.
onView(withId(R.id.editTextUserInput))
.perform(typeText(stringToBetyped), closeSoftKeyboard())
onView(withId(R.id.changeTextBt)).perform(click())
// Check that the text was changed.
onView(withId(R.id.textToBeChanged))
.check(matches(withText(stringToBetyped)))
}
Espresso code example
Espresso code example

Test Orchestrator Sample

Is anyone aware of a sample project that shows how to get test orchestrator working? I checked the google samples and there doesn't seem to be a good sample project that shows test orchestrator.
https://github.com/googlesamples/android-testing
I have been attempting to get android tests running in the test orchestrator but have been struggling to get it to work correctly. I tried both running the tests through Android Studio (latest 3.2.1) as well as the command line (https://developer.android.com/training/testing/junit-runner#ato-command-line). I used the Android developer document for reference.
https://developer.android.com/training/testing/junit-runner
Here are the steps I followed.
1) Create an empty activity application using the wizard in Android
Studio
2) Enable the test orchestrator using the steps provided here
(https://developer.android.com/training/testing/junit-runner).
3) Run the unit tests from within the IDE and from the command line.
When I do this, I get an error indicating that my "test suite is empty". I get the same error running from command-line.
Note that if I run the test without test orchestrator, then the test runs successfully.
Also note that I am using the latest test orchestrator versions
test-orchestrator (https://maven.google.com/androidx/test/orchestrator/1.1.0/orchestrator-1.1.0.apk)
test-services (https://maven.google.com/androidx/test/services/test-services/1.1.0/test-services-1.1.0.apk)
The complete configuration to run test orchestrator:
Add dependencies:
androidTestImplementation "androidx.test:runner:$testRunner"
androidTestUtil "androidx.test:orchestrator:$testOrchestrator"
Add clear package instruction (within defaultConfig in app's build.gradle):
//allows run all tests on an isolated way. If we need to debug a test, should disable this and the orchestrator
testInstrumentationRunnerArguments clearPackageData: 'true'
Add to testOptions Android/AndroidX orchestrator:
testOptions {
execution 'ANDROIDX_TEST_ORCHESTRATOR'
}

How to set teamcity to run android tests for every build

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.

Running specific tests using Espresso and Spoon

I am using Espresso and Spoon for my android tests. I am extremely pleased with these. My only problem is I am trying to run a specific test. I found this on the spoon site:
gradle spoon -PspoonClassName=fully.qualified.TestCase
But I cannot get this to work. It still runs all my test. Any suggestions?
I am running the command like so:
gradle spoon -PStressTest=com.espresso.websocket
Where StressTest is my class, and com.espresso.websocket is my package.
Figured it out: had to add this to my build.gradle
spoon {
if (project.hasProperty('spoonClassName')) {
className = project.spoonClassName
}
}
Then run tests with this:
gradle spoon -PspoonClassName=com.espresso.websosket.StressTest

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