简体   繁体   中英

android test from command line --> class not found

I am trying to run my android tests from the command line like so:

./gradlew -Pandroid.testInstrumentationRunnerArguments.class=com.my-app.MyAndroidTestClass connectedDebugAndroidTest

When I execute this in the terminal, I get the message that the build was unsuccessful and this stack trace in the report:

java.lang.ClassNotFoundException: com.my-app.MyAndroidTestClass
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:453)
at androidx.test.internal.runner.TestLoader.doCreateRunner(TestLoader.java:72)
at androidx.test.internal.runner.TestLoader.getRunnersFor(TestLoader.java:104)
at androidx.test.internal.runner.TestRequestBuilder.build(TestRequestBuilder.java:789)
at androidx.test.runner.AndroidJUnitRunner.buildRequest(AndroidJUnitRunner.java:543)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:386)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2145)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.my-app.MyAndroidTestClass" on path: DexPathList[[zip file "/system/framework/android.test.mock.jar", zip file "/system/framework/android.test.runner.jar", zip file "/system/framework/org.apache.http.legacy.boot.jar", zip file "/data/app/com.xxxxx.xxxxx.test-yNK7q-kp71fIx71z7ZeJUg==/base.apk"],nativeLibraryDirectories=[/data/app/com.xxxxx.xxxxx.test-yNK7q-kp71fIx71z7ZeJUg==/lib/x86, /data/app/com.xxxxx.xxxxx.test-yNK7q-kp71fIx71z7ZeJUg==/base.apk!/lib/x86, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
... 8 more

The odd thing is that I can run the test class from Android Studio.
What am I missing?

All the build targets including test are defined as tasks in gradle build system. Get list of tasks using command.

./gradlew tasks

Then run specific task using command

./gradlew task-name

For your test run this command

./gradlew connectedDebugAndroidTest

When running the tests manually, in the Build and Run window of Android Studio the I can see the Gradle tasks and adb shell commands that were executed.
Then I just had to copy those into my own bash script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM