Android: Runtime error while UiTesting

£可爱£侵袭症+ 提交于 2019-12-11 00:00:07

问题


I am trying to run a Ui test, and everytime I run the applicatoin it gives the following error in the console and closes the app that is running. I have the uiautomator.jar, android.jar & JUnit4 libraries imported. Im using Eclipse. What am I missing here?

[2016-04-04 04:44:00 - UiTests] Test run failed: Instrumentation run failed due to 'java.lang.RuntimeException'

TestClass

package android.support.v7.appcompat.test;

import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;

public class DMTest extends UiAutomatorTestCase
{
    public void testDemo() throws UiObjectNotFoundException
    {
        // setText
        new UiObject(new UiSelector().description("edittext_brute")).setText("bazinga");

        // clickButton
        //new UiObject(new UiSelector().description("button_done")).click();

        UiObject btDone = new UiObject(new UiSelector().description("button_done"));
        btDone.click();
    }

}

Manifest

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="android.support.v7.appcompat.test"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="14" />

    <instrumentation
    android:name="android.test.InstrumentationTestRunner"
    android:targetPackage="com.example.sony.reel" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <uses-library android:name="android.test.runner" />
    </application>

</manifest>

Edit 1: It gives the same exception even if there is no code within the testDemo() constructor

Edit 2: Logcat

Could not find test class...

04-04 07:25:48.539: E/AndroidRuntime(12026): FATAL EXCEPTION: main
04-04 07:25:48.539: E/AndroidRuntime(12026): Process: com.example.sony.reel, PID: 12026
04-04 07:25:48.539: E/AndroidRuntime(12026): java.lang.RuntimeException: Exception thrown in onCreate() of ComponentInfo{android.support.v7.appcompat.test/android.test.InstrumentationTestRunner}: java.lang.RuntimeException: Could not find test class. Class: android.support.v7.appcompat.test.DMTest
04-04 07:25:48.539: E/AndroidRuntime(12026):    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4599)
04-04 07:25:48.539: E/AndroidRuntime(12026):    at android.app.ActivityThread.access$1500(ActivityThread.java:148)
04-04 07:25:48.539: E/AndroidRuntime(12026):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1353)
04-04 07:25:48.539: E/AndroidRuntime(12026):    at android.os.Handler.dispatchMessage(Handler.java:102)
04-04 07:25:48.539: E/AndroidRuntime(12026):    at android.os.Looper.loop(Looper.java:135)
04-04 07:25:48.539: E/AndroidRuntime(12026):    at android.app.ActivityThread.main(ActivityThread.java:5312)
04-04 07:25:48.539: E/AndroidRuntime(12026):    at java.lang.reflect.Method.invoke(Native Method)
04-04 07:25:48.539: E/AndroidRuntime(12026):    at java.lang.reflect.Method.invoke(Method.java:372)
04-04 07:25:48.539: E/AndroidRuntime(12026):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
04-04 07:25:48.539: E/AndroidRuntime(12026):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
04-04 07:25:48.539: E/AndroidRuntime(12026): Caused by: java.lang.RuntimeException: Could not find test class. Class: android.support.v7.appcompat.test.DMTest
04-04 07:25:48.539: E/AndroidRuntime(12026):    at android.test.AndroidTestRunner.runFailed(AndroidTestRunner.java:255)
04-04 07:25:48.539: E/AndroidRuntime(12026):    at android.test.AndroidTestRunner.loadTestClass(AndroidTestRunner.java:89)
04-04 07:25:48.539: E/AndroidRuntime(12026):    at android.test.AndroidTestRunner.setTestClassName(AndroidTestRunner.java:50)
04-04 07:25:48.539: E/AndroidRuntime(12026):    at android.test.suitebuilder.TestSuiteBuilder.addTestClassByName(TestSuiteBuilder.java:78)
04-04 07:25:48.539: E/AndroidRuntime(12026):    at android.test.InstrumentationTestRunner.parseTestClass(InstrumentationTestRunner.java:444)
04-04 07:25:48.539: E/AndroidRuntime(12026):    at android.test.InstrumentationTestRunner.parseTestClasses(InstrumentationTestRunner.java:425)
04-04 07:25:48.539: E/AndroidRuntime(12026):    at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:371)
04-04 07:25:48.539: E/AndroidRuntime(12026):    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4596)
04-04 07:25:48.539: E/AndroidRuntime(12026):    ... 9 more

回答1:


Android recommends to use studio for uiautomator test projects and gradle for building.

In your case try adding @RunWith(AndroidJUnit4.class) annotation at the beginning of your test class definition.

Your code sample will definitely work if you move this to studio. Or compile with ant and run with compiled jar file.



来源:https://stackoverflow.com/questions/36392443/android-runtime-error-while-uitesting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!