如何创建一个测试套件robotium?(how to create a robotium tests

2019-07-03 11:28发布

用下面的代码,测试中没有我想的顺序执行。 test_homescreen是test_splashscreen之前执行。

我想指定要进行的测试,它们的执行顺序。 我相信我需要创建一个测试程序,但我不知道如何实现这一点。

package com.myapp.test;
import com.jayway.android.robotium.solo.Solo;
import android.test.ActivityInstrumentationTestCase2;
import com.myapp.R;

public class myTest extends ActivityInstrumentationTestCase2{

    private static final String TARGET_PACKAGE_ID="com.myapp.test";
    private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME="com.myapp.gui.SplashScreen";
    private static Class launcherActivityClass;
    static{
        try
        {
            launcherActivityClass=Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
        } catch (ClassNotFoundException e){
            throw new RuntimeException(e);
        }
    }
    public myTest ()throws ClassNotFoundException{
        super(TARGET_PACKAGE_ID,launcherActivityClass);
    }
    private Solo solo;

    @Override
    protected void setUp() throws Exception{
        solo = new Solo(getInstrumentation(),getActivity());
    }

    public void test_splashscreen() throws InterruptedException {
        TextView splashAppVersion = (TextView) solo.getView(R.id.AppVersion); 
        assertTrue(splashAppVersion.isShown());     
    }

    public void test_homescreen() throws InterruptedException {
        ListView lv = (ListView) solo.getView(R.id.List);
        assertTrue(lv.isShown());
        }   

    @Override
    public void tearDown() throws Exception {
        try { 
            solo.finishOpenedActivities();    
        } catch (Throwable e) { 
            e.printStackTrace(); 
        }       
        super.tearDown(); 
    }       
}
  1. 执行第一test_splashscreen(),然后test_homescreen()

  2. 仅执行test_homescreen()

这篇文章似乎是接近想我,但我一直没能利用它。 太普通。 Android的Robotium -如何管理测试用例的执行顺序?

Answer 1:

因为我们知道robotium运行在字母顺序排列的测试案例。 因此,为了更好的结果,我们可以有独立的测试用例单独活动。 后来此活动相关的其它测试情况都可以保存在同一个包(保留单独的活动单独的包)。 这将有助于在运行同一活动的测试用例在一起。 为了改变测试的顺序,你总是可以玩字母命名时测试用例。 如:“testAddSplash”将“testHomeScreen”之前运行。

您也可以使用suite()方法:

public static final Test suite()
{ 
                TestSuite testSuite = new TestSuite(); 
                testSuite.addTest(new MyTestCase("test1")); 
                testSuite.addTest(new MyTestCase("test2")); 
                return testSuite; 
} 

你的测试用例务必有一个无参数的构造函数,并用字符串参数看起来像这样的构造函数。

public MyTestCase(String name)
{ 
            setName(name); 
} 


Answer 2:

首先,依托于特定的顺序运行测试是坏的。 如果他们需要一个又一个的运行,你应该问自己,为什么他们在所有单独的测试? 如果他们依靠先前的测试状态,以前的测试的任何故障将导致未来的人失败。

现在话说回来,你可能会说我不在乎,我只想这个工作。 因此,对于你我无论如何都会给你答案。 当然,你可以做其他人所说的和重命名你的测试按字母顺序运行。 但你似乎希望控制更多的级别,所以在这里它是:

import junit.framework.Test;
import junit.framework.TestSuite;
public class AllTests {
    public static Test suite() {
        TestSuite suite = new TestSuite(AllTests.class.getName());
        suite.addTest(TestSuite.createTest(myTest.class, "test_splashscreen"));
        suite.addTest(TestSuite.createTest(myTest.class, "test_homescreen"));
        suite.addTest(TestSuite.createTest(myTest.class, "test_splashscreen"));
        return suite;
    }
}

这有很多的问题,因为你必须给该测试的名称为字符串,所以如果你(为此事和许多其他的原因也是如此)重构测试名称您的套房将打破。 典型地,测试套件是更多地用于在一次运行中的分组的测试类在一起。



Answer 3:

你能说出你测试的情况是这样的:

公共无效test1_whatever()....

公共无效test3_other()...

公共无效test2_mytest()...

当你运行它们的顺序将是:

test1_whatever()

test2_mytest()

test3_other()



文章来源: how to create a robotium testsuite?