I use @Parameterized
in many cases to run tests on a number of permutations. This works very well and keeps the test-code itself simple and clean.
However sometimes I would like to have some of the test-methods still run only once as they do not make use of the parameters, is there a way with JUnit to mark the test-method as "singleton" or "run-once"?
Note: This does not concern running single tests in Eclipse, I know how to do that :)
JUnit 4 has introduced a new feature called parameterized tests. Parameterized tests allow a developer to run the same test over and over again using different values.
JUnit Jupiter @RepeatedTest annotation is used to repeat the test case for specified no. of times. Each invocation of the test case behaves like a regular @Test method, so it has support for the same lifecycle callbacks and extensions in JUnit 5.
You could structure your test with the Enclosed runner.
@RunWith(Enclosed.class) public class TestClass { @RunWith(Parameterized.class) public static class TheParameterizedPart { @Parameters public static Object[][] data() { ... } @Test public void someTest() { ... } @Test public void anotherTest() { ... } } public static class NotParameterizedPart { @Test public void someTest() { ... } } }
You can associate any number of test classes to run together using a suite. This way all the tests are run when you test your class and you can mix different test runners.
Add the other class(es) containing non parameterized tests.
import org.junit.runners.Suite; import org.junit.runner.RunWith; @RunWith(Suite.class) @Suite.SuiteClasses({ParameterizedTestClass.class, UnitTests.class, MoreUnitTests.class}) public class SutTestSuite{ //Empty... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With