JUnit – Suite Test, run multiple test cases

In JUnit, you can run multiple test cases with @RunWith and @Suite annotation. Refer to the following examples :

SuiteAbcTest.java

package com.mkyong;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({
        Exception1Test.class, //test case 1
        TimeoutTest.class     //test case 2
})
public class SuiteAbcTest {
	//normally, this is an empty class
}

P.S Tested with JUnit 4.12

References

  1. JUnit @RunWith JavaDoc

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
kimo
Guest
kimo
Hi Mkyong, I have one requirement regarding parallel unit test cases in Integration testing. I will explain my requirement. Request you to kindly help me out for solution. Problem Statement : I am having 6 Junit classes and each class is having “n” number of test methods.Each test method will contact one service call.This service will create unique order and sends unique orderid to test method. After that those services will process the orders in backend(this is service task not my test method’s task). So existing all test cases are doing that. Now my updated requirement is, Need to track… Read more »
Prat
Guest
Prat

Hi can we get Successful test result in Junit Test Suite

Deji
Guest
Deji

Hi Mkyong,

I am trying to write a test suit using groovy but it is saying their is error in the code

package TestSuits;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({
Exception1Test.class //test case 1
TimeoutTest.class //test case 2
})
public class SuiteAbcTest {
//normally, this is an empty class
}

Error Message:
(gotten on line 8)
Annotation List attribute must use groovy notation [el1,el2] in @org.junit.runners.Suite$SuiteClasses

AnyIdea?
Guest
AnyIdea?
Let’s assume i’ve got Test inside JunitTest1.class that fails for any reason. How to pass back error message to suite e.g like: org.openqa.selenium.remote.UnreachableBrowserException: Error communicating with the remote browser. It may have died. Build info: version: ‘2.46.0’, revision: ‘61506a4624b13675f24581e453592342b7485d71’, time: ‘2015-06-04 10:22:50’ System info: host: ‘STI-WR-029’, ip: ‘172.16.20.99’, os.name: ‘Windows 8.1’, os.arch: ‘amd64’, os.version: ‘6.3’, java.version: ‘1.8.0_40’ Driver info: driver.version: RemoteWebDriver at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599) at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:269) at org.openqa.selenium.remote.RemoteWebElement.clear(RemoteWebElement.java:114) at containers.TestMethods.search(TestMethods.java:1050) at containers.TestMethods.search(TestMethods.java:1041) at tests.SearchAndAutoComplete.checkArtistAliases(SearchAndAutoComplete.java:265) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:55) at org.junit.rules.RunRules.evaluate(RunRules.java:20) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)… Read more »
olive
Guest
olive

hi
i have few doubts, could you please clarify
in Step3 : Create Test Suite Class : – how can i get the details of junit test classes executed( pass/fail, test case execution start time, end time etc..) from this suite class ?
in Step4 : Create Test Runner : How Can i call/execute this class from Ant Build File or Jenkins server ?
I am trying to generate a custom html report file for the Junit Test Suite, so for that i want to know above details.

YourBestFriend
Guest
YourBestFriend

Oh no!!! Where is your eyes friend????!!!

Shishupal Singh
Guest
Shishupal Singh

realyy nice explaination of JUNIT test with example

Shrikant kale
Guest
Shrikant kale
HI GUYS ,I am Providing you the complete Solution for this : KEEP CODING Note : Just Create hierarchy : E:. ? .classpath ? .project ? projectSet.psf ? ????.settings ? org.eclipse.jdt.core.prefs ? ????bin ? ????com ? ????cxt ? ????java ? ????source ? ? Bank.class ? ? ? ????test ? BankTest.class ? BankTestSuite.clas ? MyTestRunner.class ? ????src ????com ????cxt ????java ????source ? Bank.java ? ????test BankTest.java BankTestSuite.java MyTestRunner.java ————————————————————————————— **STEP 1 : CREATE THE SOURCE PROGRAM.** package com.cxt.java.source; public class Bank { public long accno; public double amount; public double withdraw; static double security_amount=1000.00; public double depositeMoney(long accountno, double amount) {… Read more »
rajasekhar burepalli
Guest
rajasekhar burepalli
Hi MK Yong … Iam big fan of you. Recently i started reading your posted on various technologies, it is really help for others. I have a doubt on JUNIT Testcases. Please provide the information to that public class Emp { public void insertEmp(Connection,String sql) { // Logic to perform conn and insert an employee object into the database } } So, i want to perform a testcase to the above Emp class insertEmp(-,-), but the return type of insertEmp(-,-) is void . so How should we need to write testcase for that insert(-,-) ASP please give the solution.. Thank… Read more »
Shrikant Kale
Guest
Shrikant Kale
PLEASE HELP TO SOLVE THE ISSUE. ============================================ /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package junit1; /** * * @author Shrikant.Kale */ public class JUNIT1 { public int add(int x, int y) { return x+y; } public int sub(int x, int y) { return x-y; } public int mult(int x, int y) { return x*y; } } ------------------------------------------------ /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package junit1; import org.junit.After; import org.junit.Before; import org.junit.Test; import static org.junit.Assert.*;… Read more »
kulkar
Guest
kulkar

does the @RunWith(Suite.class) always run the classes in @Suite.SuiteClasses() in the order they are specified?

Monika
Guest
Monika

Hi. Is there any way to use a class object initialized in first Test method to be used in all other Test methods in the same class. Like

public class TestCl extends TestCase
{
Student stud;
Fees fee;
@Test
public void create()
{
fee=new Fee();
fee.setAmt(10);
feeService.save(fee);
  stud=new Student();
  stud.setName("Monika");
 stud.setFees(fee);
  studService.save(stud);
}
@Test
public void testFeeByStud()
{
Fee feeFound= studService.findFeeByStud(stud);
//Here stud is always null. Can we get the stud initializes in previous method.
Assert.assertNotNull(feeFound);
}
}
kapil
Guest
kapil

use setup();

Manoj
Guest
Manoj

Thanks for your all example and support. It was really helpful to understand in quick time.

rastogi
Guest
rastogi

Nice article, depicting the topics effectively.

keep up the work.

rastogi

Consultants available for Software testing, Test Management requirements. reach us at Testing-Associates

trackback
Integrando Testes de JUnit ao Projeto « Como Fiz

[…] http://www.mkyong.com/unittest/junit-4-tutorial-5-suite-test/ Share this:TwitterFacebookGostar disso:GostoSeja o primeiro a gostar disso post. Publicado em: Uncategorized […]

trackback
JUnit Tutorials | Tutorials

[…] Tutorial 5 – Suite Test Bundle a few unit test cases and run it together. […]