如何按特定顺序运行robotium测试?

时间:2012-08-01 18:14:59

标签: automated-tests functional-testing robotium

我有这个robotium testproject,它有一个测试类。测试类包含所有测试方法。现在,当我运行测试类时,测试方法按字母顺序运行。由于我在测试方法之间存在一些依赖关系(我知道这不是首选实践),我希望测试方法以特定顺序运行。我有依赖的原因是,它允许编写更少的代码并使整个测试类运行得更快。有没有办法按特定顺序运行测试方法?

3 个答案:

答案 0 :(得分:3)

创建一个包含testsuit顺序的类。例如:

package com.android.test; 

import junit.framework.TestSuite;
import android.app.Activity;
import android.test.ActivityInstrumentationTestCase2;

public class AllTests extends  ActivityInstrumentationTestCase2<Activity> {



    public AllTests(Class<Activity> activityClass) {
        super(activityClass);
    }

    public static TestSuite suite() {
        TestSuite t = new TestSuite();
        t.addTestSuite(SplashScreenTest.class);
        t.addTestSuite(MainLoginScreenTest.class);
        t.addTestSuite(EmailSignUpScreenTest.class);
        t.addTestSuite(EmailVerificationTest.class);
        t.addTestSuite(EmailLoginScreenTest.class);

        return t; 
    }

    @Override
    public void setUp() throws Exception {

    }


    @Override
    public void tearDown() throws Exception {
    }

}

通过这种方式,您可以设置机器人的执行顺序  测试服。有关完整教程,请查看 link

答案 1 :(得分:1)

我让它们以特定顺序运行的方式是为每个测试用例创建一个对象,然后在其中一个测试用例中实例化每个对象。这允许您确定何时运行测试。缺点是你只运行一次安装方法。

这是我做的:

...//class variables and such
static {
    try {
        launcherActivityClass = Class
                .forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
}

public NewRobotiumTest() throws ClassNotFoundException {
    super(TARGET_PACKAGE_ID, launcherActivityClass);
}

private Solo solo;

@Override
protected void setUp() throws Exception {

    solo = new Solo(getInstrumentation(), getActivity());
    to = new TestObject();


    /*This resets the test documents. 
     * If running multiple test methods in this class
     * remove this method from the setup and place in first test method*/
    to.restTestDocuments(); 

}

public void testSuite() throws IOException {

    try {

        // Test the Recommended For You section
        RecommendForYou rfy = new RecommendForYou();
        rfy.testRecommedForYou(solo, to);
        Log.i(TAG, RECOMMENDED + " Section test complete.");

        //Test the Music section of the app
        Music music = new Music();
        music.testMusic(solo, to); 
        Log.i(TAG, MUSIC + " Section test complete.");

        // Test Social Networking section of the app
        Social social = new Social();
        social.testSocial(solo, to);
        Log.i(TAG, SOCIAL + " Section test complete.");

        // Test Verizon Apps section of the app
        VerizonApps vza = new VerizonApps();
        vza.testVerizonApps(solo, to);
        Log.i(TAG, VERIZONAPPS + " Section test complete.");

        // Test Entertainment section of the app
        Entertainment ent = new Entertainment();
        ent.testEntertainment(solo, to);
        Log.i(TAG, ENTERTAINMENT + " Section test complete.");

        // Test Games Section of the app
        Games games = new Games();
        games.testGames(solo, to);
        Log.i(TAG, GAMES + " Section test complete.");

        // Test Featured section of the app
        Featured featured = new Featured();
        featured.testFeatured(solo, to);
        Log.i(TAG, FEATURED + " Section test complete.");

        // Test Business section of catalog
        Business bus = new Business();
        bus.testBusiness(solo, to);
        Log.i(TAG, BUSINESS + " Section test complete.");

        // Test the New Apps link in the ODP catalog
        NewApps nat = new NewApps();
        nat.testNewApps(solo, to);
        Log.i(TAG, NEW + " Section test complete.");

        //Test the Top Sellers section of the app
        TopSeller ts = new TopSeller();
        ts.testTopSellers(solo, to);
        Log.i(TAG, TOPSELLER + " Section test complete.");

        //Test the Top Download section of the app
        TopDownload td = new TopDownload();
        td.testTopDownload(solo, to);
        Log.i(TAG, TOPDOWNLOAD + " Section test complete.");

        //Test the Themes section of the app
        Themes themes = new Themes();
        themes.testThemes(solo, to); 
        Log.i(TAG, THEMES + " Section test complete.");

        //Test the Tools section of the app
        Tools tools = new Tools();
        tools.testTools(solo, to);
        Log.i(TAG, TOOLS + " Section test complete.");

        //Test the News section of the app
        News news = new News();
        news.testNews(solo, to);
        Log.i(TAG, NEWS + " Section test complete.");

        //Test the Sports section of the app
        Sports sports = new Sports();
        sports.testSports(solo, to);
        Log.i(TAG, SPORTS + " Section test complete.");

        //Test the Reading section of the app
        Reading read = new Reading();
        read.testReading(solo, to); 
        Log.i(TAG, READING + " Section test complete.");

        //Test the Money section of the app
        Money money = new Money();
        money.testMoney(solo, to);
        Log.i(TAG, MONEY + " Section test complete.");

        //Test the Shopping section of the app
        Shopping shop = new Shopping(); 
        shop.testShopping(solo, to);
        Log.i(TAG, SHOPPING + " Section test complete.");

        //Test the Fitness section of the app
        Fitness fit = new Fitness(); 
        fit.testFitness(solo, to); 
        Log.i(TAG, FITNESS + " Section test complete.");

        //Test the Travel Section of the app
        Travel travel = new Travel();
        travel.testTravel(solo, to);
        Log.i(TAG, TRAVEL + " Section test complete.");

        //Test the Spanish section of the app
        Spanish spanish = new Spanish();
        spanish.testSpanish(solo, to); 
        Log.i(TAG, SPANISH + " Section test complete.");

        // Test the search functionality
        TestSearch test = new TestSearch();
        test.testSearchFunctionality(solo, to);


    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        printTotals(to);
    }

}

@Override
public void tearDown() throws Exception {

    try {

        bw.close();

        solo.finalize();
    } catch (Throwable e) {
        e.printStackTrace();
    }
    solo.finishOpenedActivities();

    super.tearDown();

}

public void printTotals(TestObject to) throws IOException {

    // if there is no SD card
    if (Environment.getExternalStorageState() == null) {
        directory = new File(Environment.getDataDirectory()
                + "/RobotiumTestLog/");
        photoDirectory = new File(Environment.getDataDirectory()
                + "/Robotium-Screenshots/");

        // if no directory exists, create new directory
        if (!directory.exists()) {
            directory.mkdir();
        }

        // if phone DOES have sd card
    } else if (Environment.getExternalStorageState() != null) {
        // search for directory on SD card
        directory = new File(Environment.getExternalStorageDirectory()
                + "/RobotiumTestLog/");
        photoDirectory = new File(Environment.getExternalStorageDirectory()
                + "/Robotium-Screenshots/");

        // if no directory exists, create new directory to store test
        // results
        if (!directory.exists()) {
            directory.mkdir();
        }
    }// end of SD card checking

    String fileName2 = "TotalTestResults.csv";
    File totLogRes = new File(directory, fileName2);
    bw = new BufferedWriter(new FileWriter(totLogRes, true));

    int totCases = to.getTestCase();
    int totPass = to.getTestPass();
    int totFail = to.getTestFail();
    int totRev = to.getNeedReview();
    bw.write("\"\n\"");
    bw.write("Total Test Results\",\"" + totCases + "\",\"" + totPass
            + "\",\"" + totFail + "\",\"" + totRev + "\"\n\"");
}
  }

答案 2 :(得分:0)