0

在 Blackbox 测试环境中,我需要包含CODE 1并以CODE 2结尾,以通过运行 Android JUnit Test 来执行测试(如 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() { … }

}

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

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

4

1 回答 1

2

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

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;
    }
}

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

例如:

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() { … }
}
于 2012-11-06T13:55:55.583 回答