如何创建一个Robotium测试套件?

3

使用下面的代码,测试并不能按照我想要的顺序执行。 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 - 如何管理测试用例的执行顺序?

3个回答

3
我们知道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); 
} 

我更多地是在询问如何实现一个测试套件,列出具体的测试和它们的执行顺序。否则,做一个测试套件有什么意义呢? - Franck

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

这个有很多问题,因为你必须将测试名称作为字符串给出,所以如果你重构测试名称,你的套件将会崩溃(还有许多其他原因)。通常,测试套件更多地用于将测试类组合在一起进行运行。


0
你可以像这样命名你的测试用例:

public void test1_whatever()....

public void test3_other()...

public void test2_mytest()...

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

test1_whatever()

test2_mytest()

test3_other()


这是一个非常薄弱的解决方案,因为它只适用于测试数量小于11个的情况。测试按字母顺序运行,所以 test13_ 将在 test7_ 之前执行。 - Paweł Byszewski
使用 0 在每个数字前面命名测试文件,例如 test007_ 和 test013_,这样可以按顺序运行。 - gionnut

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接