java - 如何抽象 Robotium Setup 来运行多个测试文件

标签 java android eclipse robotium

在 Blackbox 测试环境中,我需要包含 代码 1 并以 代码 2 结尾,以通过运行 Android JUnit 测试来执行测试(如 Robotium 网站所述) ):

代码 1:

public class ConnectApp extends ActivityInstrumentationTestCase2 {
private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME="com.example.android.notepad.NotesList";
private static Class<?> launcherActivityClass;
private Solo solo;
static { 
    try { launcherActivityClass=Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME); }
    catch (ClassNotFoundException e) { throw new RuntimeException(e); }
}
public ConnectApp() throws ClassNotFoundException {
    super(launcherActivityClass);
}
public void setUp() throws Exception {
    this.solo = new Solo(getInstrumentation(), getActivity());
}

代码2:

public void testNumberOne() { … }
public void testNumberTwo() { … }

}

但是,我想抽象代码的代码1(其中包括getInstrumentation()和getAcitvity()),以便我可以简单地在单独的测试文件中调用它们,然后运行代码2。这是因为我想在单独的文件中进行测试,并且不想继续添加相同数量的 CODE 1 代码,而只是调用方法/构造函数来启动该过程。

有办法做到这一点吗?预先感谢您。

最佳答案

是的,有一种方法可以做到这一点。您需要做的是创建一个空的测试类,例如:

public class TestTemplate extends ActivityInstrumentationTestCase2 {

    private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME="com.example.android.notepad.NotesList";
    private static Class<?> launcherActivityClass;
    private Solo solo;

    static { 
        try { launcherActivityClass=Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME); }
        catch (ClassNotFoundException e) { throw new RuntimeException(e); }
    }
    public ConnectApp() throws ClassNotFoundException {
        super(launcherActivityClass);
    }

    public void setUp() throws Exception {
        super.setUp();//I added this line in, you need it otherwise things might go wrong
        this.solo = new Solo(getInstrumentation(), getActivity());
    }

    public Solo getSolo(){
        return solo;
    }
}

然后,对于您将来想要的每个测试类,您将扩展 TestTemplate,而不是扩展 ActivityInstrumentationTestCase2。

例如:

public class ActualTest extends TestTemplate {
    public ActualTest() throws ClassNotFoundException {
    super();
}

    public void setUp() throws Exception {
        super.setUp();
        //anything specific to setting up for this test
    }

    public void testNumberOne() { … }
    public void testNumberTwo() { … }
}

关于java - 如何抽象 Robotium Setup 来运行多个测试文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13240444/

相关文章:

java - 使用 PDFBox 从特定页面读取文本

java - 使用 GSON 反序列化嵌套的通用类时的奇怪行为

java - kafka 0.8.2.2有spring支持吗?

Android 应用程序在导入 Facebook 库后在 Eclipse 中出现红色感叹号

eclipse - 缺少...的 POM,没有可用的依赖信息

java - 在 Embedded Derby 中启用用户身份验证

jquery - 在触摸屏移动设备上启用拖放

java - 为什么我的自定义 ArrayAdapter 没有被填充?

c - 显示包含特定标题的文件

java - 无法启动 Eclipse Helios