Farhan Ahmed Wasim
Farhan Ahmed Wasim

Reputation: 949

How to run robotium tests in a specific order?

I have this robotium testproject which has one testclass. The test class contains all the test methods. Right now when I run the test class, the test methods are run in alphabetical order. Since I have some dependencies between the test methods(i know this is not the preferred practice) I want the test methods to run in a specific order. The reason I am having dependencies is that, it allows for less code to be written and makes the whole test class to run faster. Is there any way to run the test methods in a specific order?

Upvotes: 2

Views: 2641

Answers (3)

Mukesh Y
Mukesh Y

Reputation: 652

Create an Class which contains the order of testsuit. For eg:

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

}

By this way you can set the order of execution of your robotium test suit. For complete tutorial check this link

Upvotes: 3

IgorGanapolsky
IgorGanapolsky

Reputation: 26821

You can just try to follow JUnit recommendations to organize your tests, as described here: http://etutorials.org/Programming/Java+extreme+programming/Chapter+4.+JUnit/4.8+Organizing+Tests+into+Test+Suites/

Upvotes: 0

BlackHatSamurai
BlackHatSamurai

Reputation: 23483

The way that I got them to run in a specific order was to create an object for each test case, and then instantiate each object in just one of the test cases. This allows you to determine what test runs when. The down side is that you only run the setup method once.

Here is what I did:

...//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\"");
}
  }

Upvotes: 1

Related Questions