简体   繁体   中英

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.

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()

  2. 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); 
} 

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 test3_other()...

public void test2_mytest()...

and when you run them the order will be:

test1_whatever()

test2_mytest()

test3_other()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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