几个方法测试问题 - 挂在第二个测试 - Robotium with ActivityInstrumentationTestCase2

Several method test issue - hang on second test- Robotium with ActivityInstrumentationTestCase2

我在做什么: 我正在使用 Robotium 测试 Android 应用程序。

什么有效:如果我用一种测试方法进行测试,一切都很好

什么不起作用: 如果我尝试将这个测试分成两个较小的部分,那么第一个测试通过,第二个测试挂起(或冻结 - 我不知道怎么命名)

为什么我需要它 我需要它来在 sppon 中生成报告,显示每个测试的条形图(我将有:testLogin、testAddCustomer、testLogout 等)。勺子报告示例如下所示:

如果我有一个大测试 (testAll),则只有一个大绿色条,但我需要为每种测试方法设置许多短条,如上图所示。

我所做的:我已经阅读了很多关于类似问题的不同主题,但对我没有帮助

这是一个有效的简短示例(一种方法 - testAll())我在评论中写了当前活动:

public class LogInLogOut extends ActivityInstrumentationTestCase2 {
    private Solo solo;
    private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "mobile.touch.core.activity.SplashScreenActivity";
    private static Class<?> launcherActivityClass;
    static {
        try {
            launcherActivityClass = Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    public LogInLogOut() throws ClassNotFoundException {
        super(launcherActivityClass);
    }

    public void setUp() throws Exception {
        super.setUp();
        solo = new Solo(getInstrumentation());
        getActivity();
    }
    @Override
    protected void tearDown() throws Exception {
        try {
            solo.finalize();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        getActivity().finish();
        super.tearDown();
    }

    public void testAll() {
        // here is LoginActivity <<-----
        // username
        solo.clickOnView(solo.getView(0x3));
        solo.enterText((android.widget.EditText) solo.getView(0x3), "user");
        // enter password
        solo.clickOnView(solo.getView(0x3, 1));
        solo.enterText((android.widget.EditText) solo.getView(0x3, 1), "password");
        // click on log in button
        solo.clickOnView(solo.getView(android.widget.Button.class, 0));

        // here ContainerActivity starts <<-----
        //click on log out
        solo.clickOnMenuItem("LogOut");
       }
}

使用testAll()方法,所有测试都通过了。但我需要将其分为 testLogin() 和测试 testLogout().

这里是我如何将方法 testAll 分成两个较小的 (testLogin() & testLogout()):

public void testLogin() {
    // here is LoginActivity <<-----
    // username
    solo.clickOnView(solo.getView(0x3));
    solo.enterText((android.widget.EditText) solo.getView(0x3), "user");
    // enter password
    solo.clickOnView(solo.getView(0x3, 1));
    solo.enterText((android.widget.EditText) solo.getView(0x3, 1), "password");
    // click on log in button
    solo.clickOnView(solo.getView(android.widget.Button.class, 0));
    // here ContainerActivity starts <<-----
}
public void testOut() {
    //click on log out
    solo.clickOnMenuItem("LogOut");
   }

现在第一个测试(testLogin())通过了,第二个(testLogout())挂了

为了检查测试是否开始,我登录了它。

public void testOut() {
    Log.i("checkTestB", "test B started"); <<-- here is the log
    //click on log out
    solo.clickOnMenuItem("LogOut");
   }

碰巧测试日志没有执行代码,因为 "test B started" 不在日志中

问题:我该如何解决这个问题?

我找到了答案。方法 setUp() 在 each 测试方法之前执行,tearDown() 在 each 测试方法之后执行。它引起了问题。

我重写了 setUp() 和 tearDown() :

@Override
    public void setUp() {

    }
@Override
protected void tearDown() throws Exception {
    if (exit == true) {
        try {
            solo.finalize();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        solo.finishOpenedActivities();
        super.tearDown();
    }
}

然后我写了我自己的 mehtods startUp() 与删除的 setUp() 和 tearDown() 相同。

public void startUp() {
        solo = new Solo(getInstrumentation());
        getActivity();
    }

然后setUp()在测试之前执行时什么都不做。 teadDown() 也是一样,它什么都不做,直到我在测试方法中设置 exit==true。

解决方案是在 FIRST 测试方法中启动 manulay startUp(),在 LAST 测试方法中启动 exit=ture(默认为 false),然后 tearDown() 将 运行 并关闭一切。

现在我在testLogin()中手动启动activity:

public void testLogin() {
    startUp(); <<<<<<<-----------------same method as setUp() but can started manually
    solo.clickOnView(solo.getView(0x3));
    solo.enterText((android.widget.EditText) solo.getView(0x3), "user");
    solo.clickOnView(solo.getView(0x3, 1));
    solo.enterText((android.widget.EditText) solo.getView(0x3, 1), "password");
    solo.clickOnView(solo.getView(android.widget.Button.class, 0));
}

在第二种方法 testLogout() 中,我在 begginig 上使用:solo = new Solo(getInstrumentation());和最后的 turnDown() 方法

public void testLogOut() {
    solo = new Solo(getInstrumentation());
    solo.clickOnMenuItem("LogOut");
   exit=true;
    }