简体   繁体   English

如何创建机器人测试套件?

[英]how to create a robotium testsuite?

with the code below, the tests are not executed in the order I'd like. 使用下面的代码,测试不会按照我想要的顺序执行。 test_homescreen is executed before test_splashscreen. test_homescreen在test_splashscreen之前执行。

I'd like to specify the tests to run and their order of execution. 我想指定要运行的测试及其执行顺序。 I believe I need to create a testsuite, but I don't know how to implement that. 我相信我需要创建一个测试套件,但我不知道如何实现它。

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. execute first test_splashscreen(), then test_homescreen() 执行第一个test_splashscreen(),然后执行test_homescreen()

  2. execute only test_homescreen() 只执行test_homescreen()

this post seems to be close to what I'd like but I have not been able to utilize it. 这篇文章似乎接近我想要的但我还没有能够利用它。 too generic. 太通用了。 Android Robotium - How to manage the execution order of testcases? Android Robotium - 如何管理测试用例的执行顺序?

as we know robotium runs the test cases in alphabetical order. 我们知道robotium按字母顺序运行测试用例。 So, for better result we can have separate test cases for separate activities. 因此,为了获得更好的结果,我们可以为不同的活动分别设 Later on other test cases related to that activity could be kept in same package (keep separate package for separate activity). 稍后,与该活动相关的其他测试用例可以保存在同一个包中(为单独的活动保留单独的包)。 this will help in running the test cases of same activity together. 这将有助于将相同活动的测试用例一起运行。 For changing the order of test you can always play with alphabets while naming the test cases. 要更改测试顺序,您可以在命名测试用例时始终使用字母表。 eg: "testAddSplash" will run before "testHomeScreen". 例如:“testAddSplash”将在“testHomeScreen”之前运行。

You can also use suite() method: 您还可以使用suite()方法:

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

Your test case musts have a no-arg constructor and a constructor with string parameter that looks like this. 您的测试用例必须具有no-arg构造函数和带有字符串参数的构造函数,如下所示。

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

First off, relying on tests running in a specific order is bad. 首先,依赖于按特定顺序运行的测试是不好的。 If they require one to run after another you should be asking yourself why are they separate tests at all? 如果他们要求一个接一个地运行你应该问自己为什么他们分开测试? If they rely on the previous tests state any failure in a previous test will cause the next ones to fail. 如果他们依赖于先前的测试状态,则先前测试中的任何失败都将导致下一个测试失败。

Now having said that, you are probably saying I do not care i just want this to work. 现在已经这么说了,你可能会说我不在乎我只想让它起作用。 So for you I will give you the answer anyway. 因此,无论如何,我会给你答案。 You can of course do as others have said and rename your tests to run in alphabetical order. 您当然可以像其他人所说的那样做,并将您的测试重命名为按字母顺序运行。 But you seem to want more level of control, so here it is: 但是你似乎想要更多级别的控制,所以这里是:

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

This has lots of issues because you have to give the test name as a string so if you refactor the test name your suite will break (and lots of other reasons too for that matter). 这有很多问题,因为您必须将测试名称作为字符串给出,因此如果您重构测试名称,您的套件将会中断(还有很多其他原因)。 Typically a test suite is more used for grouping classes of tests together in one run. 通常,测试套件更多地用于在一次运行中将测试类分组在一起。

you can name you test cases like this: 你可以这样命名测试用例:

public void test1_whatever().... public void test1_whatever()....

public void test3_other()... public void test3_other()...

public void test2_mytest()... public void test2_mytest()...

and when you run them the order will be: 当你运行它们时,订单将是:

test1_whatever() test1_whatever()

test2_mytest() test2_mytest()

test3_other() test3_other()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM