Running android unit tests from the command line?

Viewed 22459

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?

9 Answers

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

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
Related