When running espresso tests, app resumes instead of restarting - android

When I run multiple espresso tests, I find that the app does not restart but starts from the same location that it is left off. How to make it start from the beginning everytime

Consider Android Test Orchestrator. It will clear all data and restart Instrumentation between each test.
Add to app Gradle:
android {
defaultConfig {
...
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
testOptions {
execution 'ANDROID_TEST_ORCHESTRATOR'
}
}
dependencies {
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestUtil 'com.android.support.test:orchestrator:1.0.1'
}
And then run it as follows:
./gradlew connectedCheck

Related

Android Instrumentation Orchestrator's `clearPackageData` does not clear data

I have defined these into my build.gradle, to enable Orchestrator in my Android Instrumented tests:
defaultConfig {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
testInstrumentationRunnerArguments clearPackageData: 'true'
}
testOptions {
animationsDisabled = true
execution 'ANDROIDX_TEST_ORCHESTRATOR'
}
dependencies {
androidTestImplementation 'androidx.test:runner:1.5.1'
androidTestUtil 'androidx.test:orchestrator:1.4.2'
}
Sadly, when I start tests, data is not cleared and it persists between test runs.
I tried both, start test from Android Studio and by gradle cli ./gradlew connectedStageDebugAndroidTest
Any idea what I am doing wrong?

AndroidJUnitRunner unit tests are very slow to execute

I'm using the AndroidJUnitRunner in my project, but the unit tests are painfully slow to execute on a device and emulator both in Android Studio and from the command-line via gradlew.
I'm using the master branch of this open-source project, OneBusAway (as of this commit):
https://github.com/OneBusAway/onebusaway-android
I'm seeing execution time of all 142 tests that is upwards of 3 minutes in real elapsed time, but Android only registers a smaller portion of this in the execution time it shows under "Test Results". Before switching to the AndroidJUnitRunner all of these unit tests executed under 20 seconds consistently.
Here's what an example test looks like:
/**
* Tests to evaluate utility methods related to math conversions
*/
#RunWith(AndroidJUnit4.class)
public class MathUtilTest {
#Test
public void testOrientationToDirection() {
// East
double direction = MathUtils.toDirection(0);
assertEquals(90.0, direction);
}
}
Here's the build.gradle config:
android {
dexOptions {
preDexLibraries true
}
compileSdkVersion this.ext.compileSdkVersion
buildToolsVersion "27.0.3"
defaultConfig {
minSdkVersion 14
targetSdkVersion 21
versionCode 93
versionName "2.3.8"
multiDexEnabled true
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
// The following argument makes the Android Test Orchestrator run its
// "pm clear" command after each test invocation. This command ensures
// that the app's state is completely cleared between tests.
testInstrumentationRunnerArguments clearPackageData: 'true'
}
...
testOptions {
execution 'ANDROID_TEST_ORCHESTRATOR'
unitTests.includeAndroidResources true
}
...
}
dependencies {
...
// Unit tests
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestUtil 'com.android.support.test:orchestrator:1.0.2'
...
}
Why is this running unit tests so slow?
Apparently the slowdown is related to including a dependency on Android Test Orchestrator, which I don't need (even if I'm running tests on a device that require an Android context). It wasn't clear to me from the existing documentation that Orchestrator wasn't required for these tests.
I removed the following lines from build.gradle, and my total execution time via Android Studio dropped back down into the ~15 second range:
// The following argument makes the Android Test Orchestrator run its
// "pm clear" command after each test invocation. This command ensures
// that the app's state is completely cleared between tests.
testInstrumentationRunnerArguments clearPackageData: 'true'
...
execution 'ANDROID_TEST_ORCHESTRATOR'
...
androidTestUtil 'com.android.support.test:orchestrator:1.0.2'
So here's the new build.gradle that executes tests in ~15 seconds:
android {
dexOptions {
preDexLibraries true
}
compileSdkVersion this.ext.compileSdkVersion
buildToolsVersion "27.0.3"
defaultConfig {
minSdkVersion 14
targetSdkVersion 21
versionCode 93
versionName "2.3.8"
multiDexEnabled true
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
...
testOptions {
unitTests.includeAndroidResources true
}
...
}
...
dependencies {
...
// Unit tests
androidTestImplementation 'com.android.support.test:runner:1.0.2'
}
I thought that maybe the testInstrumentationRunnerArguments clearPackageData: 'true' was the primary culprit causing increased execution time to clear the package data, but removing that line alone didn't change the total execution time of tests - I had to completely remove the Orchestrator dependency.
Here's the commit on Github that removed Orchestrator:
https://github.com/OneBusAway/onebusaway-android/commit/a1657c443258ac49b1be83a75399cf2ced71080e

How to run specific test using using gradlew on command line?

I'm quite new with the Android gradle build.
I built and application and it work quite ok. Now I would like to add unit test and instrument test into it, and execute them on command line.
I have to test files:
MainActivityTest.java <== This used instrumentation test which should be executed on a device.
CustomerFragmentTest.java <== This used junit test which should be executed on JVM only.
Now from the command line in Project Directory I called
gradlew test --continue
And it executed all tests (both JVM tests and Device Tests) while I expect only the JVM tests to be run.
So:
How can I run the junit test cases on JVM only (Ignore the instrumentation test cases)?
When I call gradlew connectedCheck I got the execution failed message:
"Task 'connectedCheck' not found in root project'Android5Camera'"
How can I execute the instrumentation tests
(on Device) using command line?
Thank for any help and advice.
Here by my app.gradle config in case you need:
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.huynhle.android5camera"
minSdkVersion 16
targetSdkVersion 22
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
java.srcDirs = ['src/main/java']
}
main.setRoot('src/main')
androidTest.setRoot('src/test')
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.0'
compile 'com.astuetz:pagerslidingtabstrip:1.0.1'
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.10.19'
androidTestCompile 'com.android.support.test:runner:0.3'
androidTestCompile 'com.android.support.test:rules:0.3'
}
As i can assume, you have put both class-files to the same directory src/test. This directory is appropriate only for unit tests, and test runner don't distinguish test files between Intsrumentation or simple JUnit. That's why the gradlew test --continue command launches both classes.
Android Instrumentation Tests classes should be located in the src/androidTest directory and should be heirs (direct or no) of InstrumentationTestCase or AndroidTestCase, then you can launch instrumentation tests using gradlew connectedCheck.

Test running failed: Unable to find instrumentation info for: ComponentInfo{} -- error trying to test in IntelliJ with Gradle

Everytime I try to run my tests the console says this:
Running tests
Test running startedTest running failed: Unable to find instrumentation info for:
ComponentInfo{com.employeeappv2.employeeappv2.test/android.test.InstrumentationTestRunner}
Empty test suite.
I've been stuck on this for a while and the solutions I've seen online so far have not helped.
My project structure is set up like this:
*Main Module
-src
*instrumentTest
-java
*main
-java
-manifest
*build.gradle
My build.gradle file looks like this:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
android {
compileSdkVersion 19
buildToolsVersion "19.1.0"
defaultConfig {
minSdkVersion 16
targetSdkVersion 19
versionCode 1
versionName "2.1.0"
testPackageName "login.test"
testInstrumentationRunner "android.test.InstrumentationTestRunner"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard- rules.txt'
}
}
packagingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
exclude 'META-INF/notice.txt'
exclude 'META-INF/license.txt'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile files('libs/scandit.zip')
compile project(':pullToRefresh')
compile 'com.android.support:appcompat-v7:19.+'
compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.1+'
compile 'org.springframework.android:spring-android-rest-template:1.0.1+'
compile 'org.json:json:20090211'
compile 'com.fasterxml.jackson.core:jackson-databind:2.3.1'
compile 'com.fasterxml.jackson.core:jackson-annotations:2.3.0'
compile 'com.fasterxml.jackson.core:jackson-core:2.3.1'
compile 'com.android.support:support-v4:19.1.+'
compile 'com.mcxiaoke.volley:library:1.0.+#aar'
androidTestCompile 'junit:junit:3.8'
}
Do you need to have a separate manifest for your tests directory? If so what would that look like?
Edit: I tried adding a manifest to my instrumentTest directory with no luck. Note that I could not get IntelliJ to resolve the targetPackage name, so it appears red.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.employeeappv2.employeeappv2.src.instrumentTest"
android:versionCode="1"
android:versionName="1.0.0">
<application>
<uses-library android:name="android.test.runner" />
</application>
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.employeeappv2.employeeappv2.src.main"/>
</manifest>
I had the same error when I tried adding multiDexEnabled true to build.gradle.
I'm adding my experience here, because this is one of the first Google hits when searching with the ... Unable to find ... ComponentInfo ... error message.
In my case adding testInstrumentationRunner like here did the trick:
...
android {
...
defaultConfig {
...
testInstrumentationRunner "com.android.test.runner.MultiDexTestRunner"
}
}
(I have com.android.tools.build:gradle:1.5.0)
I am using Android Studio 1.1 and the following steps solved this issue for me:
In Run - Edit Configurations - Android Tests
Specify instrumentation runner as android.test.InstrumentationTestRunner
Then in the "Build variants" tool window (on the left), change the test artifact to Android Instrumentation Tests.
No testInstrumentationRunner required in build.gradle and no instrumentation tag required in manifest file.
When I created a new package, Studio created an ApplicationTest class. Using us.myname.mypackage as an example, the following directory structure was created:
app/[myPackage]/src/androidTest/java/us/myname/mypackage/ApplicationTest.class
Initially it worked out of the box. It quit working after I installed product flavors. I subsequently made the following changes to build.gradle:
defaultConfig {
...
testInstrumentationRunner "android.test.InstrumentationTestRunner"
}
some prefer
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
(With my current configuration, I have to use ...InstrumentationTestRunner when in debug, and AndroidJUnitRunner while using release build type.)
The above configuration only works with the debug build type. If you wish to use it with release or with a custom build type, you can include the following in build.gradle:
buildTypes {
release {
...
}
debug {
...
}
}
testBuildType "release"
In the Build Variants tab on the lower left side of Studio, make sure you have Android Instrumentation Tests and the correct buildType selected.
I had to do a combination of VikingGlen's answer, Liuting's answer, and this answer. This answer works for Android Studio version 2.1.
Run -> Edit Configurations... -> General -> Specific instrumentation runner (optional): "android.support.test.runner.AndroidJUnitRunner"
In build.gradle (the one with all your dependencies), put this:
defaultConfig {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Then it could run tests of this type:
#RunWith(AndroidJUnit4.class)
#LargeTest
public class ApplicationTest {
#Rule
public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class);
#Test
public void login() {
//Open Drawer to click on navigation.
onView(withId(R.id.drawer_layout))
.check(matches(isClosed(Gravity.LEFT))) // Left Drawer should be closed.
.perform(open()); // Open Drawer
}
}
For what it's worth, AS 2.3 got hung up when i created a custom test runner after using the regular test runner. I got the same error as posted in the question.
Deleting the Debug Configurations for ALL Android Instrumented Tests and rebuilding fixed it. I believe the problem lied in the fact you no longer can choose a custom runner in the Debug Configurations because it's most likely built in via gradle.
So the main problem was that when I created an androidTest folder under /src/, it wasn't being picked up by IntelliJ as a source folder for testing (java subdirectory should turn green). I was using IntelliJ 13.0.3 and after upgrading to 13.1.3, all of my troubles went away.
*Note: do not try to add a manifest to your androidTest folder, the Gradle docs specifically state that the manifest should be auto-generated when you create the androidTest folder. The problem for me was that the file wasn't being generated as androidTest wasn't being recognized by IntelliJ/Gradle, thus throwing the no instrumentation error.
I had this problem and fixed it by going to Run -> Edit Configurations -> Green '+' button at the top left -> JUnit
From there, set the 'use the classpath mod...' to 'app' (or your default app name, which is the one that appears to the left of the run (play button) when you run the app)
Finally, put your test class name in the 'class:' textbox. Click apply and okay.
At this point, if the test class doesn't have other errors, it should work.
This is what I noticed in my project, in my app(main) module build.gradle I had the following buildType configuration
buildTypes {
debug {
multiDexEnabled true
}
mock {
initWith(buildTypes.debug)
}
}
testBuildType "mock"
When I used AndroidJUnitRunner as the test runner(both from Android Studio) and as testInstrumentationRunner in build.gradle, tests ran without hitch.
In a submodule that had multiDexEnabled true as defaultConfig
defaultConfig {
multiDexEnabled true
....
}
I ran into the problem of
Test running startedTest running failed: Unable to find instrumentation info for:{mypackage.x.y/android.support.test.runner.AndroidJUnitRunner"}
when I specified AndroidJUnitRunner in IDE and the submodule build.gradle. And this was fixed by specifying MultiDexTestRunner as the test runner in IDE/build.gradle.
To summarize, Use MultiDexTestRunner to run tests when multiDexEnabled true is specified in build.gradle, else use AndroidJUnitRunner as the test runner.
Make sure the app has been uninstalled for all users.
Go to settings -> apps (all apps) -> If your app is there then tap on it -> menu -> uninstall for all users.
My issue was that the app was at one point uninstalled, however still on the device; meaning it was not uninstalled for all users. (Another issue, not sure how to resolve. I'm the only user on my device)
Because of this, the app wouldn't re-install, and the test suite had nothing to run against.
The solution for my problem is to change the method name from
#Test
public void test() {
...
}
to
#Test
public void testSomething() {
...
}
Hope it helps someone.

instrumentation tests stops working after rebuild: No tests found

I'm trying to get the androidTest (instrumentation tests) working for the openScale Android app using the following build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.health.openscale"
testApplicationId "com.health.openscale.test"
minSdkVersion 18
targetSdkVersion 22 // don't set target sdk > 22 otherwise bluetooth le discovery need permission to ACCESS_COARSE_LOCATION
versionCode 22
versionName "1.7 (beta)"
javaCompileOptions {
annotationProcessorOptions { arguments = ["room.schemaLocation":"$projectDir/schemas".toString()] }
}
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
sourceSets {
androidTest.assets.srcDirs += files("$projectDir/schemas".toString())
}
lintOptions {
abortOnError false
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
ext {
supportLibVersion = '27.0.2'
}
dependencies {
implementation "com.android.support:design:${supportLibVersion}"
implementation "com.android.support:support-v4:${supportLibVersion}"
implementation "com.android.support:appcompat-v7:${supportLibVersion}"
// HelloCharts
implementation 'com.github.lecho:hellocharts-library:1.5.8#aar'
// Simple CSV
implementation 'com.j256.simplecsv:simplecsv:2.2'
// CustomActivityOnCrash
implementation 'cat.ereza:customactivityoncrash:2.2.0'
// Room
implementation 'android.arch.persistence.room:runtime:1.0.0'
annotationProcessor 'android.arch.persistence.room:compiler:1.0.0'
androidTestImplementation 'android.arch.persistence.room:testing:1.0.0'
// Local unit tests
testImplementation 'junit:junit:4.12'
// Instrumented unit tests
androidTestImplementation "com.android.support:support-annotations:${supportLibVersion}"
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test:rules:1.0.1'
}
tasks.withType(Test) {
testLogging {
exceptionFormat "full"
events "started", "skipped", "passed", "failed"
showStandardStreams true
}
}
There's a single test file under android_app/app/src/androidTest/java/com.health.openscale/DatabaseTest.java
I'm using the command ./gradlew --no-daemon --no-build-cache -i connectedDebugAndroidTest to build and run the tests and it works fine the first time: both tests in the file are executed and pass.
But now, if I change the test file (small change to trigger a rebuild) and run the above command again I get the following output:
...
Putting task artifact state for task ':app:compileDebugAndroidTestJavaWithJavac' into context took 0.0 secs.
file or directory '/<path>/android_app/app/src/androidTestDebug/java', not found
Executing task ':app:compileDebugAndroidTestJavaWithJavac' (up-to-date check took 0.007 secs) due to:
Input property 'source' file /<path>/android_app/app/src/androidTest/java/com.health.openscale/DatabaseTest.java has changed.
Compiling with source level 1.7 and target level 1.7.
...
Putting task artifact state for task ':app:transformClassesWithDexBuilderForDebugAndroidTest' into context took 0.0 secs.
Executing task ':app:transformClassesWithDexBuilderForDebugAndroidTest' (up-to-date check took 0.024 secs) due to:
Input property '$3' file /<path>/android_app/app/build/intermediates/classes/androidTest/debug/com/health/openscale/DatabaseTest.class has been removed.
Transform inputs calculations based on following changes
/<path>/android_app/app/build/intermediates/classes/androidTest/debug/com/health/openscale/DatabaseTest.class:REMOVED
...
Starting 0 tests on Nexus_5X_API_26(AVD) - 8.0.0
[XmlResultReporter]: XML test result file generated at /<path>/android_app/app/build/outputs/androidTest-results/connected/TEST-Nexus_5X_API_26(AVD) - 8.0.0-app-.xml. Total tests 0,
com.android.builder.testing.ConnectedDevice > No tests found.[Nexus_5X_API_26(AVD) - 8.0.0] FAILED
No tests found. This usually means that your test classes are not in the form that your test runner expects (e.g. don't inherit from TestCase or lack #Test annotations).
[XmlResultReporter]: XML test result file generated at /<path>/android_app/app/build/outputs/androidTest-results/connected/TEST-Nexus_5X_API_26(AVD) - 8.0.0-app-.xml. Total tests 1, failure 1,
...
Does anyone know why the rebuild doesn't work? Why is the class removed from the build?
I have verified with the APK analyzer in Android studio that the DatabaseTest class is indeed missing from the APK after the rebuild.
It seems that moving the test case from .../com.health.openscale/DatabaseTest.java to .../com/health/openscale/DatabaseTest.java fixed the problem.

Categories

Resources