【问题标题】:Several method test issue - hang on second test- Robotium with ActivityInstrumentationTestCase2几个方法测试问题 - 挂在第二个测试上 - Robotium with ActivityInstrumentationTestCase2
【发布时间】:2015-08-06 11:00:14
【问题描述】:

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

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

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

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

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

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

这是一个简短的例子,说明什么是有效的(一种方法 - testAll())我在 cmets 中编写了 currents 活动:

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()

这是我如何将metod 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");
   }

发生testLog没有执行代码,因为“test B started”不在日志中

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

【问题讨论】:

    标签: android unit-testing automated-tests ui-automation robotium


    【解决方案1】:

    我找到了答案。方法 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();
        }
    }
    

    然后我写了我自己的方法 startUp() 与 setUp() 和 tearDown() 一样删除。

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

    然后 setUp() 在测试前执行时什么也不做。与teadDown() 相同,除非我在测试方法中设置exit==true,否则它什么都不会。

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

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

    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() 中:solo = new Solo(getInstrumentation());最后是 turnDown() 方法

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-19
      • 2022-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-02
      • 1970-01-01
      相关资源
      最近更新 更多