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
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 the order status Parallely. I mean each method should execute and give the test result and parallely this test mehtod output should pass to another common method which will take the input as order Id and check the order status and then based on status value this common method should do assertEqual.
So all test method execution flow will not stop untill the common method execution response. Please suggest.
Hi can we get Successful test result in Junit Test Suite
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
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)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:209)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at org.apache.http.impl.io.SessionInputBufferImpl.streamRead(SessionInputBufferImpl.java:139)
at org.apache.http.impl.io.SessionInputBufferImpl.fillBuffer(SessionInputBufferImpl.java:155)
at org.apache.http.impl.io.SessionInputBufferImpl.readLine(SessionInputBufferImpl.java:284)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:140)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:57)
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:261)
at org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader(DefaultBHttpClientConnection.java:165)
at org.apache.http.impl.conn.CPoolProxy.receiveResponseHeader(CPoolProxy.java:167)
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:272)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:124)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:271)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:71)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)
at org.openqa.selenium.remote.internal.ApacheHttpClient.fallBackExecute(ApacheHttpClient.java:143)
at org.openqa.selenium.remote.internal.ApacheHttpClient.execute(ApacheHttpClient.java:89)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:134)
at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.execute(NewProfileExtensionConnection.java:168)
at org.openqa.selenium.firefox.FirefoxDriver$LazyCommandExecutor.execute(FirefoxDriver.java:392)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:578)
… 52 more
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.
Oh no!!! Where is your eyes friend????!!!
realyy nice explaination of JUNIT test with example
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.**
**STEP 2 : CREATE TEST CLASS.**
**STEP 3 : CREATE TEST SUITE CLASS**
**STEP 4 : CREATE TEST RUNNER**
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 you…………
does the @RunWith(Suite.class) always run the classes in @Suite.SuiteClasses() in the order they are specified?
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
use setup();
Thanks for your all example and support. It was really helpful to understand in quick time.
Nice article, depicting the topics effectively.
keep up the work.
rastogi
Consultants available for Software testing, Test Management requirements. reach us at Testing-Associates
Hi, can i use @Runwith(Suite.class) Next to @SpringBootTest?
Hi Mkyong,
All examples I found about SuiteClass has 2 or 3 actual tests in them.
It may be convenient for a quick tutorial, but how do you handle hundreds of tests in a test suite ? Do you add them all one by one in @SuiteClass() or can you use some agregation to automatically select them, all or groups of them.
Thanks
All good… I found this library that allows to use wildcards to select any test in your project.
https://github.com/michaeltamm/junit-toolbox
package examples;
import org.junit.runner.RunWith;
//import org.junit.runners.Suite;
//import org.junit.runners.Suite.SuiteClasses;
import com.googlecode.junittoolbox.WildcardPatternSuite;
import com.googlecode.junittoolbox.SuiteClasses;
//@RunWith(Suite.class)
//@SuiteClasses({TestA.class, TestB.class, TestC.class})
@RunWith(WildcardPatternSuite.class)
@SuiteClasses({“**/*.class”}) // Select all test classes recursively
public class TestSuite {
}