JUnit 4 Test Suites
Asked Answered
A

5

104

How do I create test suites with JUnit 4?

All the documentation I've seen doesn't seem to be working for me. And if I use the Eclipse wizard it doesn't give me an option to select any of the test classes I have created.

Aseptic answered 19/1, 2009 at 11:14 Comment(2)
With Eclipse 3.7 Indigo, the test suite wizard now supports JUnit 4Bobbinet
See also https://mcmap.net/q/183740/-how-to-add-test-cases-to-a-suite-using-junit for nested test suites.Hampden
W
155
import org.junit.runners.Suite;
import org.junit.runner.RunWith;

@RunWith(Suite.class)
@Suite.SuiteClasses({TestClass1.class, TestClass2.class})
public class TestSuite {
  //nothing
}
Walkway answered 19/1, 2009 at 11:22 Comment(0)
L
64

You can create a suite like so. For example an AllTest suite would look something like this.

package my.package.tests;

@RunWith(Suite.class)
@SuiteClasses({
    testMyService.class,
    testMyBackend.class,
    ...
})

public class AllTests {}

Now you can run this in a couple different ways:

  1. right-click and run in Eclipse as Junit test
  2. create a runable Java Application; Main class='org.junit.runner.JUnitCore' and Args='my.package.tests.AllTests'
  3. run from the command line:

    $ java -cp build/classes/:/usr/share/java/junit4.jar:/usr/share/java/hamcrest-core.jar org.junit.runner.JUnitCore my.package.tests.AllTests
    
Leund answered 10/2, 2011 at 0:41 Comment(1)
Would add even more VoteUps for the CMDLine :)Membranophone
V
9

I think TestSuite has fallen out of favor. That might have been the style before 4.x, but it's not now as far as I know.

I just annotate the tests I want and then run the class. All the annotated tests are run. I might use Ant, but most of the time I have IntelliJ run them for me.

Venusian answered 19/1, 2009 at 11:19 Comment(4)
I could be wrong, but i think TestSuite is still good when we need to specify the order of tests, especially in automated integration tests where testing smaller scenarios should come before more complex scenarios.Chervil
@Venusian I find your comment interesting as I have so far been coached in the pre-4.x line of thought regarding organizing testcases into testsuitesLithiasis
If you have larger numbers of tests, different functional areas/modules, a distinction between 'core' and auxiliary/ slower/ expensive non-core tests -- for any of these reasons -- you would structure tests into a TestSuite.Inconvenience
You can use the @SuiteClass annotation in multiple classes and you can nest Suites within Suites. The line of thought for organizing them in 3.x is still totally valid (and ought to be used). You can structure everything the exact same way as you did before; you just use the annotation syntax instead of TestSuite.suite(). There's really no benefit to using TestSuite over the annotation and you can take advantage of all the JUnit 4 enhancements if you use the organizationally equivalent annotation.Georgia
H
4

Here are the steps to create a JUnit suite in eclipse:

  1. In the 'Package Explorer' view of the eclipse 'Java' perspective, select your unit test(s) in their package, inside the eclipse java project.
  2. Right-click on any one of the selected tests.
  3. In the pop-up menu, select New, Other…
  4. Open the ‘Java’ folder, then open the ‘JUnit’ folder
  5. Select ‘JUnit Test Suite’ and then select the ‘Next’ button
  6. Select button ‘Finish’
  7. Result: ‘AllTests.java’ suite file is created, with tests automatically included
  8. Select the Run button in eclipse
  9. Result: all tests in suite run
  10. You can now point to this suite file with ANT, Jenkins or other build configuration continuous integration tool.

Version info: this is for eclipse Neon and JUnit 4. You can also select JUnit 3 before selecting 'Finish' in step 6.

Holcman answered 24/9, 2017 at 22:31 Comment(1)
Could you post a build.xml file showing how to run this test suite from ANT?Melicent
V
1

Of the top of my head create a TestSuite and the invoke addTests. If you want somesource to look at try any opensource lib like hibernate or something from apache and take a look under the test directory of the source for a Tests suite ...

Village answered 19/1, 2009 at 11:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.