Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I Dynamically create a Test Suite in JUnit 4?

Tags:

java

junit

junit4

I would like to create a junit test suite using JUnit 4 where the names of the test classes to be included are not known until the test suite is run.

In JUnit 3 I could do this:

public final class MasterTester extends TestCase {   /**    * Used by junit to specify what TestCases to run.    *     * @return a suite containing what TestCases to run    */   public static TestSuite suite() {     TestSuite suite = new TestSuite();      for(Class<?> klass : gatherTestClasses()) {       suite.addTestSuite(klass);     }      return suite;   } } 

and let the gatherTestClasses() method deal with figuring out what test classes to run.

In JUnit 4, the documentation says to use an annotation: @SuiteClasses({TestClass1.class, TestClass2.class...}) to build up my test suite. There are numerous SO answers showing how to do this. Unfortunately the examples I see do not seem to allow for passing a dynamically generated list of TestClasses.

This SO answer suggested I would have to subclass BlockJUnit4ClassRunner which I do not want to do.

Dynamically specified test suites seem like something that must be in JUnit 4 somewhere. Does anyone know where?

like image 974
Tom Tresansky Avatar asked Jul 15 '10 15:07

Tom Tresansky


People also ask

Which one is used to create a Test Suite?

In the main menu, click Construction > Create > Test Suite. The new test suite opens with a table of contents on the left side of the screen and an editor on the right side. At the top of the new test suite window, enter a name for the new test suite.

Is JUnit testing dynamic or static?

JUnit Dynamic Tests JUnit @TestFactory methods must not be private or static. These methods must return a Stream, Collection, Iterable, or Iterator of DynamicNode instances. Any Stream returned by a @TestFactory will be properly closed by calling stream.

Which is the correct code snippet to create a Test Suite in JUnit?

Attach @RunWith(Suite. class) Annotation with the class. Add reference to JUnit test classes using @Suite. SuiteClasses annotation.


2 Answers

To create a dynamic test suite, you need to use the @RunWith annotation. There are two common ways to use it:

@RunWith(Suite.class)

This allows you to specify, which classes compose the test suite in question. This is equivalent to the JUnit 3 style:

import junit.framework.TestSuite; import junit.framework.TestCase;  public final class MasterTester extends TestCase {    public static TestSuite suite() {     TestSuite suite = new TestSuite();     suite.addTestSuite(TestClass1.class);             suite.addTestSuite(TestClass2.class);     // etc...     return suite;   } } 

The equivalent JUnit 4 class will be:

import org.junit.runners.Suite;  @RunWith(Suite.class) @SuiteClasses({TestClass1.class, TestClass2.class}) public final class MasterTester {  } 

@RunWith(AllTests.class)

This allows you to dynamically specify the tests, which compose the test suite. If your tests are not known until runtime, you cannot specify them in the annotations. You can use this construction instead. So, if the JUnit 3 code is:

import junit.framework.TestCase; import junit.framework.TestSuite; import junit.framework.Test;  public final class MasterTester extends TestCase {    public static TestSuite suite() {     TestSuite suite = new TestSuite();     for (Test test : findAllTestCasesRuntime()) {       suite.addTest(test);     }     return suite;   } } 

The equivalent JUnit 4 code will be:

import org.junit.runners.AllTests; import junit.framework.TestSuite; import junit.framework.Test;  @RunWith(AllTests.class) public final class MasterTester {    public static TestSuite suite() {     TestSuite suite = new TestSuite();     for (Test test : findAllTestCasesRuntime()) {       suite.addTest(test);     }     return suite;   } } 
like image 144
Danail Nachev Avatar answered Sep 29 '22 02:09

Danail Nachev


I've tried this using JUnit 4.8 and it works:

@RunWith(AllTests.class) public class SomeTests {     public static TestSuite suite()     {         TestSuite suite = new TestSuite();          suite.addTest(new JUnit4TestAdapter(Test1.class));         suite.addTest(new JUnit4TestAdapter(Test2.class));          return suite;      } } 
like image 26
Andrejs Avatar answered Sep 29 '22 02:09

Andrejs