在 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/