0

I have 2 test classes, both extend TestCase. Each class contains a bunch of individual tests which run against my program.

How can I execute both classes (and all tests they have) as part of the same suite?

I am using jUnit 4.8.

Jonathan Drapeau
  • 2,610
  • 2
  • 26
  • 32
James Raitsev
  • 92,517
  • 154
  • 335
  • 470

3 Answers3

5

In jUnit4 you have something like this:

@RunWith(Suite.class)
@SuiteClasses({
    SomeTest.class,
    SomeOtherTest.class,
    ...
    })
public class AllTests {}

If you want the Eclipse GUI suite builder (New > JUnit Test suite), you have to add

public static junit.framework.Test suite() {
   return new JUnit4TestAdapter(SomeTest.class);
}

to each of your test classes s.t. the GUI test suite builder recognizes your test.

Juri
  • 32,424
  • 20
  • 102
  • 136
0

Create TestClass and override suite() method and run newly created TestClass.

 public static Test suite()
    {
        TestSuite suite = new TestSuite("Test ExpenseTest");
        suite.add(TestCase1.class);
        suite.add(TestCase2.class);
        return suite;
    }
Vinay Lodha
  • 2,185
  • 20
  • 29
  • Maybe this JavaDoc will help http://kentbeck.github.com/junit/javadoc/4.8/org/junit/runners/Suite.html. I have never used it JavaDoc have enough information – Vinay Lodha Sep 02 '10 at 14:56
  • 1
    My test set is invoked as part of @Suite.SuiteClasses({MyTests.class}). Where should I add your code to produce a subsuit? Who consumes the suite produced? – Val Sep 17 '13 at 14:25
  • @VinayLodha, sorry, but cannot understand what class did you extend. And the link is not found. – CoolMind Jan 19 '23 at 14:42
0

JUnit 3 supported TestSuite and public static Test suite(). JUnit 4 doesn't support it (it creates added test classes, but doesn't launch test methods).

But you can launch a subsuite of tests inside a suite.

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

@RunWith(Suite::class)
@Suite.SuiteClasses(
    Test1::class,
    TestSuite1::class
)
class StartTest

Then inside StartTest you can add test classes and test suites.

@RunWith(Suite::class)
@Suite.SuiteClasses(
    Test2::class,
    Test3::class,
    TestSuite2::class
)
class TestSuite1

@RunWith(Suite::class)
@Suite.SuiteClasses(
    Test4::class,
    Test5::class
)
class TestSuite2

This way you can join test classes into groups (to have a list of 10 items instead of 100).

CoolMind
  • 26,736
  • 15
  • 188
  • 224