4

I have a project testsuite with a set of files which I want to use as a testsuite - one test per file.

But the number of tests will go to hundreds, and each tests is the same: Parse the file and check the output. Maintaining a method per each file is tedious.

The JBoss Operation Network used TestNG which allowed to create a test suite, and stuff it with several instances of the same class, and allowing to explicitly set the test name.

In jUnit, I haven't found a way to change the test name. So it would be the same for all.

Is there some way or extension to run the same class multiple times with custom resulting test name?

PS: I'd prefer not to resort to class instrumentation.

Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277

2 Answers2

4

When you create TestSuite, you can pass desiredTestCaseName as constructor argument:

public class AllTestSuite extends TestSuite{

  public static Test suite() {
      TestSuite suite= new TestSuite();
      Test testCase1 = new MyTest("TestCaseOne");
      Test testCase2 = new MyTest("TestCaseTwo");
      suite.addTest(testCase1);
      suite.addTest(testCase2);
      return suite;
  }
}

When you run the suite, it will refer the names supplied in constructor i.e. TestCaseOne and TestCaseTwo while test class is MyTest.

EDIT:

Please make sure you have defined the constructor in MyTest class as:

public MyTest(String name) {
    super(name);
}

If additional parameters support is desired, you can add an constructor in Test case to accept the parameters and assign them to test case class variables(initially defaulted with default test scenario values), which can be used during test execution:

public MyTest(String name, String param1, Integer param2...) {
    super(name);
    this.param1 = param1;
    this.param2 = param2;
}

Once you run your test suite, JUnit Console looks like screen below: enter image description here

Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
3

If you're using JUnit4, you can look at @Parameterized. The basic idea is that you provide a list of things to do, and JUnit executes the same test method for each item in the list.

With version 4.10, the naming isn't too great, you get 0, 1, 2... as names, but with 4.11-beta-1 (which should come out of beta soon), you can get a better naming scheme, because you can specify the name. See the javadoc at the top of @Parameterized.

@RunWith(Parameterized.class)
public class FibonacciTest {
    @Parameters(name= "{index}: fib({0})={1}") // we specify the name here
    public static Iterable<Object[]> data() {
        return Arrays.asList(new Object[][] { { 0, 0 }, { 1, 1 }, { 2, 1 },
                { 3, 2 }, { 4, 3 }, { 5, 5 }, { 6, 8 } });
    }

    private int fInput;

    private int fExpected;

    public FibonacciTest(int input, int expected) {
        fInput= input;
        fExpected= expected;
    }

    @Test
    public void test() {
        assertEquals(fExpected, Fibonacci.compute(fInput));
    }
}

In the above, you end up with names such as

fib(0)=0
fib(1)=1
fib(2)=1

etc.

Matthew Farwell
  • 60,889
  • 18
  • 128
  • 171
  • Parametrized is nice, but the method has to be static. I intended to have this class extendable as there are multiple sets of files with slightly different settings for the project, and I'd have a class for each. Is that possible somehow? – Ondra Žižka Oct 18 '12 at 17:31