如何创建一个robotium测试套件?套件、创建一个、测试、robotium

2023-09-07 13:01:04 作者:要你一口咔吱脆

与下面的code中,测试未在顺序我想执行。 test_homescreen是test_splashscreen之前执行的。

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

 包com.myapp.test;
进口com.jayway.android.robotium.solo.Solo;
进口android.test.ActivityInstrumentationTestCase2;
进口com.myapp.R;

公共类MYTEST扩展ActivityInstrumentationTestCase2 {

    私有静态最后弦乐TARGET_PACKAGE_ID =com.myapp.test;
    私有静态最后弦乐LAUNCHER_ACTIVITY_FULL_CLASSNAME =com.myapp.gui.SplashScreen;
    私有静态类launcherActivityClass;
    静态{
        尝试
        {
            launcherActivityClass =的Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
        }赶上(ClassNotFoundException异常E){
            抛出新的RuntimeException(E);
        }
    }
    公共MYTEST()抛出ClassNotFoundException的{
        超(TARGET_PACKAGE_ID,launcherActivityClass);
    }
    私人独奏独唱;

    @覆盖
    保护无效设置()抛出异常{
        独奏=新梭罗(getInstrumentation(),getActivity());
    }

    公共无效test_splashscreen()抛出InterruptedException的{
        TextView的splashAppVersion =(TextView中)solo.getView(R.id.AppVersion);
        assertTrue(splashAppVersion.isShown());
    }

    公共无效test_homescreen()抛出InterruptedException的{
        ListView的LV =(ListView控件)solo.getView(R.id.List);
        assertTrue(lv.isShown());
        }

    @覆盖
    公共无效的teardown()抛出异常{
        尝试 {
            solo.finishOpenedActivities();
        }赶上(的Throwable E){
            e.printStackTrace();
        }
        super.tearDown();
    }
}
 

执行第一test_splashscreen(),然后test_homescreen()

执行仅test_homescreen()

这个帖子似乎接近我想,但我一直没能利用它。过于笼统。 Android Robotium - 如何管理测试用例的执行顺序

解决方案

据我们所知robotium运行按字母顺序排列的测试用例。因此,为了更好的结果,我们可以有不同的测试用例单独活动。后来有关该活动的其他测试用例可以保存在同一个包(保持独立包单独活动)。这将有助于在运行同一活动的测试用例在一起。为了改变测试的顺序,你总是可以玩字母命名时测试用例。例如:testAddSplash将testHomeScreen之前运行 您也可以使用套件()方法:

 公共静态最终的测试套件()
{
                TestSuite的测试包=新的TestSuite();
                testSuite.addTest(新MyTestCase的(测试1));
                testSuite.addTest(新MyTestCase的(测试2));
                返回测试包;
}
 

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

 公众对MyTestCase(字符串名称)
{
            的setName(名);
}
 
人工智能有了幽默感很危险

with the code below, the tests are not executed in the order I'd like. test_homescreen is executed before 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(); 
    }       
}

execute first test_splashscreen(), then test_homescreen()

execute only 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?

解决方案

as we know robotium runs the test cases in alphabetical order. 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". You can also use suite() method:

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.

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