有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何创建robotium测试套件?

使用下面的代码,测试不会按照我想要的顺序执行。 在测试屏幕之前执行测试屏幕

我想指定要运行的测试及其执行顺序。 我认为我需要创建一个测试套件,但我不知道如何实现它

package com.myapp.test;
import com.jayway.安卓.robotium.solo.Solo;
import 安卓.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 - How to manage the execution order of testcases?


共 (3) 个答案

  1. # 1 楼答案

    首先,依赖以特定顺序运行的测试是不好的。如果它们需要一个接一个地运行,你应该问问自己,为什么它们是单独的测试?如果他们依赖于之前的测试,说明之前测试中的任何失败都将导致下一个测试失败

    话虽如此,你可能是在说我不在乎,我只是想让它发挥作用。所以不管怎样,我都会给你答案。当然,您可以像其他人所说的那样,将测试重命名为按字母顺序运行。但你似乎想要更多的控制,所以这里是:

    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;
        }
    }
    

    这有很多问题,因为您必须以字符串的形式给出测试名称,因此如果重构测试名称,您的套件将崩溃(以及许多其他原因)。通常,测试套件更多地用于在一次运行中将测试类分组在一起

  2. # 2 楼答案

    正如我们所知,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); 
    } 
    
  3. # 3 楼答案

    您可以这样命名测试用例:

    public void test1_whatever()....

    public void test3_other()...

    public void test2_mytest()...

    当你运行它们时,顺序是:

    test1_whatever()

    test2_mytest()

    test3_other()