在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()),这样我就可以在一个单独的测试文件中调用它们然后运行代码2 。这是因为我希望在单独的文件中进行测试,并且不希望继续添加相同数量的 CODE 1 代码,而只是调用方法/构造函数来启动该过程。
有办法做到这一点吗?提前谢谢。
答案 0 :(得分: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() { … }
}