Robotium:未启动第二次测试的活动

时间:2013-03-17 17:43:57

标签: android junit robotium

我已经能够成功运行一个测试方法的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();
}

}

1 个答案:

答案 0 :(得分:3)

您可能需要@Override您的拆解方法

@Override
public void tearDown()  throws  Exception  { 
    solo.finishOpenedActivities();
    super.tearDown();
}