Instrumented tests not found with custom instrumentation - conditional instrumentation? - android

I have a working Android app with a manifest containing this <instrumentation> node
<instrumentation
android:name=".MyInstrumentation"
android:targetPackage="my.package"/>
I have instrumented tests also, but when I run those I get this
$ adb shell am instrument -w -r-e debug false my.package.test/my.package.MyInstrumentation
Client not ready yet..
Started running tests
Test running failed: Unable to find instrumentation info for: ComponentInfo{my.package.test/my.package.MyInstrumentation}android.util.AndroidException: INSTRUMENTATION_FAILED: my.package.test/my.package.MyInstrumentation
. onError: commandError=true message=INSTRUMENTATION_FAILED: my.package.test/my.package.Instrumentation
and tests are not run.
If I remove the instrumentation declaration from the manifest it works fine, when I run the tests like this I get this
$ adb shell am instrument -w -r -e debug false my.package.test/androidx.test.runner.AndroidJUnitRunner
Client not ready yet..
Started running tests
Tests ran to completion.
androidx.test.runner.AndroidJUnitRunner is the instrumentation that gets executed.
This happens when I run the tests from Android Studio and also when I execute ./gradlew connectedAndroidTest.
How can I tell gradle or Android Studio that I want to run the tests with the AndroidJUnitRunner instrumentation? With still allowing my own.

add this line in your build.gradle file, and try
android {
...
defaultConfig {
...
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
...
}
...
}

Related

how to execute individual Junit test in android using gradle

My project contains multiple test classes and each class contains some test methods.
I can run all the test using command line
gradlew testDebugUnitTest
Is there any gradlew command to run specific testMethod from a class?
Is there any gradlew command to run all test mmethods from a specific class?
Here is a class Foo2 and bar3 is the particular method you would like to test. This was taken from the Google developer Junit unit test command line documentation
$ adb shell am instrument -w \
> -e class com.android.demo.app.tests.Foo2#bar3 \
> com.android.demo.app.tests/android.support.test.runner.AndroidJUnitRunner

How to build and create APK for a library module using command line?

In my Android studio project I have a librarymodule (Name:Network-sdk) and have some test cases for the library module. I am creating an automation script for running test cases from the bash for the library module.
I done the following script but its gives test case not found error.
#To clean the project
./gradlew clean
#Build the project
./gradlew packageDebug
#Install the APK
adb install ./IrisReferenceClient/app/build/outputs/apk/app-debug.apk
#Test case which in library module
adb shell am instrument -w -r -e debug false -e class 'package.ConnectivityTestSenderTest' package/android.support.test.runner.AndroidJUnitRunner
Log information:
android.util.AndroidException: INSTRUMENTATION_FAILED: package.test/android.support.test.runner.AndroidJUnitRunnerINSTRUMENTATION_STATUS: id=ActivityManagerService
INSTRUMENTATION_STATUS: Error=Unable to find instrumentation info for: ComponentInfo{package.test/android.support.test.runner.AndroidJUnitRunner}
INSTRUMENTATION_STATUS_CODE: -1
at com.android.commands.am.Am.runInstrument(Am.java:890)
at com.android.commands.am.Am.onRun(Am.java:400)
at com.android.internal.os.BaseCommand.run(BaseCommand.java:51)
at com.android.commands.am.Am.main(Am.java:121)
at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:262)
I want to know how to build and create APK for file library module and run my test case which in library module.

Cannot run androidTest in certain module

I am having a problem running an androidTest. It cannot locate my tests. The weird thing is, that it's only the case for a certain module. Other modules which I put the test into and configure their build.gradle files eqully are working. Here are some informations. Any help is very appreciated!
The output is as follows:
Running tests
$ adb shell am instrument -w -r -e debug false -e class my.package.MainActivityTest my.package.module.test/android.support.test.runner.AndroidJUnitRunner
Client not ready yet..
Started running tests
Test running failed: Unable to find instrumentation info for: ComponentInfo{my.package.test/android.support.test.runner.AndroidJUnitRunner}
Empty test suite.
The test is located under
myModule/src/androidTest/java/my/package/MainActivityTest.java
and looks as follows:
#LargeTest
#RunWith(AndroidJUnit4.class)
public class MainActivityTest {
#Rule
public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule<>(MainActivity.class);
#Test
public void thisTestIsntExecuted() {
assertEquals(1, 2);
}
}
The modules build.gradle file has the following settings:
android {
defaultConfig {
(...)
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
}
dependencies {
(...)
testImplementation "junit:junit:4.12"
androidTestImplementation "com.android.support.test.espresso:espresso-core:3.0.1"
androidTestImplementation "com.android.support.test:runner:1.0.1"
}
Instrumentation runner on device:
instrumentation:my.package.test/android.support.test.runner.AndroidJUnitRunner (target=my.package.module)
//edit: I had a closer look at the output from the run command and the instrumentation runner on the device. I forgot to add .module on two places.
I found out to solve the problem. As stated in the comment above I was able to start my tests manually with the command:
adb shell am instrument -w -r -e debug false -e class my.package.module.MainActivityTest my.package.test/android.support.test.runner.AndroidJUnitRunner
The run configuration which Android Studio created for me executed the command:
adb shell am instrument -w -r -e debug false -e class my.package.module.MainActivityTest my.package.module.test/android.support.test.runner.AndroidJUnitRunner
So as you can see a path was wrong, the module part was too much.
This path is the testApplicationId and can be changed via gradle, see here. When it's not set, the value is automatically created by taking the applicationId and add a .test to it. This resulted in a wrong path in my case, maybe because I am generating the applicationId based on the build variant.
Don't forget to sync and clean.

Travis CI skip tests (Gradle Android project)

Now I'm configuring Travis CI for my Gradle-based Android project. Is it possible to temporary disable tests launch from Travis to just know - if project could be built or not?
By default, Travis-ci executes ./gradlew build connectedCheck if no script: section is found.
Failed tests are ignored if you add the next code to all your tested modules (build.gradle files).
project.gradle.taskGraph.whenReady {
connectedAndroidTest {
ignoreFailures = true
}
}
Another option is to skip the install stage and only to use ./gradlew build (or ./gradle build without gradle wrapper) so tests are not performed.
install:
# Check install section: http://docs.travis-ci.com/user/build-configuration/#install
# If you'd like to skip the install stage entirely, set it to true and nothing will be run.
- true
script:
# By default Travis-ci executes './gradlew build connectedCheck' if no 'script:' section found.
- ./gradlew build
You can use the -x command line argument which excludes any task (see this answer).
gradle build -x test
Check output of
./gradlew tasks
and then use:
./gradlew assemble
Like in this thread:
Gradle build without tests

Running android unit tests from the command line?

I'm trying to run unit tests on the android platform in accordance with tutorial. Say, for example, I want to run tests for Email application. I open /apps/Email/tests/AndroidManifest.xml file, look for the <manifest> element, and look at the package attribute, which is com.android.email.tests, and in the <instrumentation> element I look at the android:name attribute, which is android.test.InstrumentationTestRunner. Now I open the console, and run
$ . build/envsetup.sh
$ lunch 1
$ adb shell am instrument -w com.android.email.tests/android.test.InstrumentationTestRunner
But that fails:
INSTRUMENTATION_STATUS: id=ActivityManagerService
android.util.AndroidException: INSTRUMENTATION_FAILED: com.android.email.tests/android.test.InstrumentationTestRunner
INSTRUMENTATION_STATUS: Error=Unable to find instrumentation info for: ComponentInfo{com.android.email.tests/android.test.InstrumentationTestRunner}
So.. What am I doing wrong?
Please run
python development/testrunner/runtest.py email
and then you will see it works :).
Basically you do not have com.android.email.tests package installed.
You can see what is available for instrumentation
pm list instrumentation
And you should see
instrumentation:com.android.email.tests/android.test.InstrumentationTestRunner
(target=com.android.email)
And when doing
pm list packages
package:com.android.email
package:com.android.email.tests
You may need to setup a test project with the android create test-project command first. Check this page on the Android Dev site: Testing In Other IDE's for more info. I've used this method to enable command line testing with ant.
What I actually forgot to do was building and installing that test packages onto my device/emulator. Discovered that after doing:
$ adb shell
# cd data/packages
# ls
And no com.android.email.tests package there.
My issue was this tag:
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:label="Tests for app.under.test.package"
android:targetPackage="app.under.test.package" />
Firstly I had the android:name attribute wrong then the target package wrong (above is the correct solution)
Test Package Not Installed on the Emulator
I had the exact same issue and realized that the test package hadn't been installed on the emulator, which is what #marekdef is saying.
Instead of using the command found in the generated test file, I used the following:
ant debug install test
*I had my test project in <project_home>/tests so the following command is what I ended up using from my project home directory:
(cd tests && ant debug install test;)
Hope that helps.
I received the "Unable to find instrumentation info" error under this condition: I defined my test project with a src package name that was the same as that of the project-under-test. For example, the source for both projects was in package com.mycompany.android. This parallel-src-package setup worked fine in Eclipse, but on the emulator it very much appeared that the test apk was overwriting the app apk.
Fix: I changed the src packge of the test project to test.mycompany.android.
Note that, in either case, the Android manifest for the test project defines:
< manifest package="pkg-of-project-under-test" ...>
and
< instrumentation android:targetPackage="pkg-of-project-under-test" ...>
For gradle user you can use the following tasks:
$ gradle :project:installDebug :project:installDebugAndroidTest
I have this run_all_test.sh script to run all unit and instrumented test:
#!/bin/bash
./gradlew --no-daemon clean test connectedCheck --stacktrace;
if [ $? -eq 0 ]
then
echo "tests are successful"
else
echo "tests FAILED"
exit 1
fi
Explanation:
test -> execute all unit test
connectedCheck -> execute all instrumented test
You can start from here to customize it based on your needs following the documentation here: https://developer.android.com/studio/test/command-line
[Android test types]
To run all tests from Command Line using gradlew[About]
JUnit test (UnitTest suffix)
./gradlew test
./gradlew :<moduleName>:test<variantName>UnitTest
Instrument test(AndroidTest suffix)
./gradlew connectedAndroidTest
./gradlew :<moduleName>:connected<variantName>AndroidTest
If you want just to build tests and don't run them use assemble
//Unit
./gradlew :<moduleName>:assemble<variantName>UnitTest
//functional
./gradlew :<moduleName>:assemble<variantName>AndroidTest

Categories

Resources