Android:UiTesting时运行时错误

时间:2016-04-03 23:16:26

标签: android unit-testing ui-automation android-testing uiautomator

我正在尝试运行Ui测试,每次运行应用程序时,它都会在控制台中出现以下错误并关闭正在运行的应用程序。我有uiautomator.jar,android.jar&导入JUnit4库。我正在使用Eclipse。我在这里缺少什么?

  

[2016-04-04 04:44:00 - UiTests]测试运行失败:仪表运行   由于'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();
    }

}

清单

<?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>

编辑1:即使testDemo()构造函数中没有代码,它也会给出相同的异常

编辑2:Logcat

  

无法找到测试类......

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 个答案:

答案 0 :(得分:0)

Android建议使用studio进行uiautomator测试项目并使用gradle进行构建。

在您的情况下,请尝试在测试类定义的开头添加@RunWith(AndroidJUnit4.class)注释。

如果将其移至工作室,您的代码示例肯定会有效。或者使用ant进行编译并使用已编译的jar文件运行。