GitHub - PeterWippermann/parameterized-suite: Provides a new Runner for JUnit 4 that combines the features of Suite and Parameterized (original) (raw)

Build Status Maven Central

Parameterized Suite

This library enables you to define parameters for a test suite.

It provides a new Runner ParameterizedSuite for JUnit 4 that combines the features of Suite and Parameterized. The new Runner can be used as a drop-in replacement for Suite.

Features

Use cases

Usage

This is how a parameterized test suite looks like:

@RunWith(ParameterizedSuite.class) @SuiteClasses({OneTest.class, TwoTest.class}) public class MyParameterizedTestSuite { @Parameters(name = "Parameters are {0} and {1}") public static Object[] params() { return new Object[][] {{'A',1}, {'B',2}, {'C',3}}; }

/**
 * Always provide a target for the defined parameters - even if you only want to access them in the suite's child classes.
 * Instead of fields you can also use a constructor with parameters.
 */
@Parameter(0)
public char myCharParameter;

@Parameter(1)
public int myIntParameter;

}

Your test case reads the current parameter from the ParameterContext:

@RunWith(Parameterized.class) public class MyParameterizedTestCase {

@Parameters(name = "Parameters are {0} and {1}") // Always define a name here! (See "known issues" section)
public static Iterable<Object[]> params() {
    if (ParameterContext.isParameterSet()) {
        return Collections.singletonList(ParameterContext.getParameter(Object[].class));
    } else {
        // if the test case is not executed as part of a ParameterizedSuite, you can define fallback parameters
    }
}

private char myCharParameter;
private int myIntParameter;

public MyParameterizedTestCase(char c, int i) {
    super();
    this.myCharParameter = c;
    this.myIntParameter = i;
}

@Test
public void testWithParameters() {
    // ...
}

}

You can find a fully working example in the tests of this project: ExampleParameterizedSuite.
Multiple parameterized suites (and non-parameterized) can also be combined by another test suite as TopLevelNoParametersSuite demonstrates. They won't interfere with each other.

Dependency setup

Add the following dependency declaration of parameterized-suite together with JUnit 4.

Maven

com.github.peterwippermann.junit4 parameterized-suite 1.1.0 test junit junit 4.12 test

Gradle

dependencies { testCompile 'junit:junit:4.12', 'com.github.peterwippermann.junit4:parameterized-suite:1.1.0' }

Known issues

Define a name for the @Parameters method in your test cases

Define a name like the following: @Parameters(name = "Parameter set #{index} - {0};{1}").
This makes your test cases not only more readable, but it also prevents you from an Eclipse bug!
When the same test with the same name is executed twice, the results will only be attributed to the last node of that test in the test execution hierarchy tree - leaving the other nodes of the same test stale. By defining a unique name you can circumvent this bug. Therefore I suggest to include a String representation of your current parameters (e.g. {0};{1} for two parameters).

Background info

Before I made this library publicly available, I blogged about Implementing a parameterized test suite in JUnit.

License

This library is licensed under EPL - version 1.
The package com.github.peterwippermann.junit4.parameterizedsuite.util also contains source from JUnit 4. See details in the package info and the license file.

Contributing