JUnit 4 – Executing multiple Test Suites

by seetaram on April 16, 2011

So far we have learnt JUnit 4 from the aspect of running a single test case.  In this post, we will explore how to run multiple test cases with JUnit 4.

I am less bothered about how the JUnit is used for unit testing, but my concern is how we can utilize JUnit (referring to JUnit 4) for functional testing along with Selenium.  We will learn executing multiple test cases using JUnit with a simple test.

Note: If you are a newbie to Selenium / JUnit / Eclipse, then please refer to my previous posts that explain various topics such as Setting up Eclipse with Selenium, Creating test cases in Eclipse, etc.

Let us write three simple tests and then using @RunWith and @SuiteClasses annotations we will execute all of them together as a TestSuite.

  1. Create a new Package (e.g. com.selftechy.testsuite)
  2. Create three JUnit Test Cases in Eclipse under this package (First, Second, and Third)
  3. Create a fourth JUnit test case as RunTestSuite
  4. Right Click on RunTestSuite –> Run As –> JUnit Test
  5. Console output should be as in the below picture

RunSuite

ExecutionSuite-Output

 

Create three JUnit test cases First, Second, Third, and RunTestSuite using the below code and execute RunTestSuite

package com.selftechy.testsuite;
import org.junit.Test;

public class First {
    @Test
    public void testOne(){
        System.out.println("Executing first test");
    }

}
package com.selftechy.testsuite;
import org.junit.Test;


public class Second {
    @Test
    public void testTwo(){
        System.out.println("Executing second test");
    }

}
package com.selftechy.testsuite;

import org.junit.Test;


public class Third {
    @Test
    public void testThree(){
        System.out.println("Executing third test");
    }

}
package com.selftechy.testsuite;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({First.class,Second.class,Third.class})
public class RunTestSuite {
}

{ 20 comments… read them below or add one }

Sowmya May 12, 2011 at 11:45 am

Thanks for the information about Selenium and Eclipse.. You sure have made the life easy for newbies in Selenium and Eclipse.. Keep posting more..

Best Regards,
S

Reply

seetaram May 13, 2011 at 12:50 pm

Thanks Sowmya

Reply

Harry Soehalim June 22, 2011 at 5:03 am

you should change the subject to: Executing Multiple Test Suites

Reply

seetaram June 23, 2011 at 6:19 am

Yes…thanks for your suggestion.. What you told is correct… I will do that…

Reply

ajit July 27, 2011 at 12:13 pm

Thanks for sharing this information….

I would like to know “How to execute multiple test cases simultaneously” using Junit and selenium.

Reply

seetaram July 27, 2011 at 2:21 pm

Ajit,

If it is “test cases” then keep all the tests (ideally test methods in a java class that start with “test” – testGoogleAdvancedSearch) in the class and then Run As –> Junit Test

This will easily run all the tests in the class

Reply

Biswabrata Banerjee August 20, 2011 at 10:30 am

very neat and clean. keep up the good work. best wishes….

Reply

seetaram August 20, 2011 at 6:51 pm

Thank you…

Reply

SHIVAM September 23, 2011 at 7:35 am

This is the best explanation I ever had. Really good work….

Reply

seetaram September 23, 2011 at 11:45 am

Thank you Shivam

Reply

A.K Ramesh October 25, 2011 at 1:15 pm

could you tell me the diff b/n click and clickAt in selenium RC.
When to use it.

Reply

seetaram October 25, 2011 at 2:22 pm

Please refer the below link that explains the difference between click & clickAt:
http://stackoverflow.com/questions/1340171/use-of-clickat-selenium-comand

Reply

Anita November 3, 2011 at 11:27 am

Nice Post :)…very informative

Reply

Jyothi November 22, 2011 at 9:32 am

Hi,

Can you please explain the same thing without using eclipse? I would like to run the same tests from the command prompt.

Thanks,
Jyothi

Reply

Kewal Nautiyal December 19, 2013 at 11:09 am

Hi Jyothi,

You can run tests from command line as :

For a single Test:
adb shell am instrument -e -w /android.test.InstrumentationTestRunner

For all your Tests:
adb shell am instrument -w /android.test.InstrumentationTestRunner

Reply

Mahesh December 13, 2011 at 7:00 pm

Excellent work Seetaram. Keep it a Good work

Reply

seetaram December 14, 2011 at 12:22 pm

Thanks Mahesh

Reply

Ramya December 26, 2011 at 9:58 pm

Very nice work….keep it up….

Reply

Vinu July 17, 2012 at 5:32 am

Hi Seetaram,

I have automated few test cases and saved them into different test suites based on their respective areas (like..all login test cases in Login folder, all Registration into registration folder, all product test cases into Product folder etc…) now I want to run these test suite all at once using Ant.
Can anyone guide me how to do this.
I am able to run individual test suite by creating a testsuite.java class and in that class I am using the annotations

@RunWith(Suite.class)
@Suite.SuiteClass({ ,
,….
})

and calling this single test suite in the build.xml file
The above case works fine when I run individual test suite. But how to call multiple test suites in the scenario which I mentioned.

Thanks

Reply

mani October 25, 2012 at 12:02 pm

Hi

This information is useful. But I have query.
Here how can I give first.class and second.class dynamically

@SuiteClasses({First.class,Second.class,Third.class})

in SuitesClasses annotation. I want to pass class names dynamically. Is it possible?

Reply

Leave a Comment

Previous post:

Next post:

Do you want to get updates on the recent articles written? Please subscribe to RSS feed or Email