Android UiAutomator Testing



我创建了一个简单的活动,其中我有两个提交和取消按钮。我只是想通过使用UI自动化测试测试这个项目。所以我创建了一个android测试项目并创建了一个类。我让这个测试类扩展UiAutomatorTestCase。我还添加了uiautomator.jar、android.jar以及junit3库。但是当我运行测试用例时,它给了我一个关于

的错误

TestSuiteConstruction failed and java.lang.RuntimeException.

然而,我添加了构造函数,但因为它不带任何参数,所以我无法添加任何参数。下面是我的测试用例代码。请尽快解决这个错误。你能告诉我我在项目中还有什么没有添加的吗?

package com.example.automatorapp.test;
import android.test.suitebuilder.TestSuiteBuilder;
import android.util.Log;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
import com.example.automatorapp.MainActivity;
public class testDemo1 extends UiAutomatorTestCase
{
    public testDemo1()
    {
    }
    public void testdemo() throws UiObjectNotFoundException
    {   
        getUiDevice().pressHome();
        Log.e("how r u","hello");
    }
}

尝试更改您的类的名称,使不是以'test'开头,并删除构造函数。按照惯例,类名以大写字母开头,而你的类名以小写"t"开头。同样,按照惯例,JUnit3测试类把"test"这个词放在最后。http://junit.sourceforge.net/junit3.8.1/javadoc/junit/framework/TestCase.html因此,在您的示例中,testdemo1的类名将是Demo1Test行(记住文件名需要与类名匹配)。

然而,令我惊讶的是,一个稍微修改过的代码版本在我的机器上运行了。下面是运行OK的代码。

package com.example.automatorapp.test;
import android.util.Log;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
public class testDemo1 extends UiAutomatorTestCase
{
    public testDemo1()
    {
    }
    public void testdemo() throws UiObjectNotFoundException
    {
        getUiDevice().pressHome();
        Log.e("how r u","hello");
    }
}

我在Android日志中也得到了日志信息。

E/how r u (24667): hello

因此,您可能在项目设置或构建环境中遇到一些问题。据我所知,这个项目(用于测试)应该独立于你想要测试的应用程序的代码或项目。然而,你有一个导入到你想要测试的应用程序。

import com.example.automatorapp.MainActivity;

既然你现在已经发布了相当多的问题与UI自动化也许你可以总结你的进展到目前为止。例如,您是否曾经成功运行过UI Automator测试?

PS:我希望看到一些反馈,以回答你之前的问题。如果没有你的反馈,很难知道你的方向,以及答案是否与你相关或有用。

最新更新