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.
- Create a new Package (e.g. com.selftechy.testsuite)
- Create three JUnit Test Cases in Eclipse under this package (First, Second, and Third)
- Create a fourth JUnit test case as RunTestSuite
- Right Click on RunTestSuite –> Run As –> JUnit Test
- Console output should be as in the below picture
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 { }
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
Thanks Sowmya
you should change the subject to: Executing Multiple Test Suites
Yes…thanks for your suggestion.. What you told is correct… I will do that…
Thanks for sharing this information….
I would like to know “How to execute multiple test cases simultaneously” using Junit and selenium.
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
very neat and clean. keep up the good work. best wishes….
Thank you…
This is the best explanation I ever had. Really good work….
Thank you Shivam
could you tell me the diff b/n click and clickAt in selenium RC.
When to use it.
Please refer the below link that explains the difference between click & clickAt:
http://stackoverflow.com/questions/1340171/use-of-clickat-selenium-comand
Nice Post :)…very informative
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
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
Excellent work Seetaram. Keep it a Good work
Thanks Mahesh
Very nice work….keep it up….
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
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?
if First.class takes 3 minutes second.class takes 3 minutes and Third.class takes three minutes i wish not to wait 9 minutes but be able to run three parallel threads can anyone suggest how to accomplish same