This project is no longer maintained. Feel free to fork. If it is useful to anyone, let me know!
Script to generate parameterized JUnit-4 tests for test runners that do not support parameterized tests.
JUnit-4 has a design flaw where test runners cannot be combined to perform
multiple behaviors. For example, JUnit's Parameterized
test runner cannot
be combined with RobolectricTestRunner
, or any other test runner because
only one @RunWith
annotation is permitted per class.
generateParameterizedTests.js
solves this by parsing special comments in the
test...
class StepUnitFormatterTests {
//> Zero(0, "0 steps")
//> Singular(1, "1 step")
//> Plural(2, "2 steps")
//> 999(999, "999 steps")
//> Thousand(1000, "1,000 steps")
//> Million(1000000, "1,000,000 steps")
//> Decimal(3.3, "3 steps")
//> Negative(-5, "0 steps")
@ParameterizedTest
public void testFormat(double numberOfSteps, String expected) {
Assert.assertEquals(expected, StepUnitFormatter().format(numberOfSteps));
}
}
...and converting them to individual tests, which get appended to the end of the file like this:
// DO NOT EDIT BELOW THIS LINE
@Test
public void testFormatZero() {
testFormat(0, "0 steps")
}
@Test
public void testFormatSingular() {
testFormat(1, "1 step")
}
@Test
public void testFormatPlural() {
testFormat(2, "2 steps")
}
// Etc.
}
- Copy
generateParameterizedTests.js
anywhere into your Java project. - Copy
ParameterizedTest.java
into your test source code folder. - Update
package x.x.x;
inParameterizedTest.java
to reflect your project's package structure.
Node must be installed, but npm install
is not required. To generate the
parameterized tests, run the following:
$ node generateParameterizedTests.js path/to/tests
To avoid having to run the generator manually, a task can be added to your
build.gradle
file to run the script on preBuild
, or any other preferred
Gradle task.
task generateParameterizedTests(type: Exec) {
executable "sh"
args "-c", "node scripts/generateParameterizedTests.js src/test"
}
preBuild.dependsOn generateParameterizedTests
This code is dedicated to the public domain, so no attribution is needed.
This script is based on a similar implementation by Elliot Chance for generating parameterized tests in Swift.