我已经能够使用一种测试方法成功运行 android junit 测试,但是当涉及多个测试方法时,它只运行第一个测试,并且在 tearDown 之后,活动不会重新启动以进行后续测试。结果,我所有的测试方法都失败了,保存第一个。
在调试时,我注意到 setUp 方法在运行第一个 testMethod 之前成功启动了 MainActivity,但是在第二个 testMethod 开始之前重新访问时,相同的活动不会重新启动。代码如下:
package PACKAGE.test;
import com.jayway.android.robotium.solo.Solo;
import android.test.ActivityInstrumentationTestCase2;
public class Login extends ActivityInstrumentationTestCase2 {
private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "*.*.MainActivity";
private static Class<?> launcherActivityClass;
static {
try {
launcherActivityClass = Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
}
catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
@SuppressWarnings("unchecked")
public Login() throws ClassNotFoundException {
super(launcherActivityClass);
}
private Solo solo;
public void setUp() throws Exception {
super.setUp();
solo = new Solo(getInstrumentation(), getActivity());
}
@Test
public void testLoginScreen() {
solo.enterText(0, "user-name");
solo.enterText(1, "pwd");
solo.clickOnButton("Login");
solo.waitForActivity("*.*.*.nextActivity");
solo.clickOnRadioButton(2);
}
@Test
public void testSearch(){
solo.enterText(0, "user-name");
solo.enterText(1, "pwd");
solo.clickOnButton("Login");
solo.waitForActivity("*.*.*.nextActivity");
solo.clickOnRadioButton(1);
}
public void tearDown() throws Exception {
solo.finishOpenedActivities();
super.tearDown();
}
}