Is it possible to parameterize a TestSuite in junit 4 ?
For declaring a class as a test suite I need the annotation @RunWith(Suite.class)
, but the same annotation is also needed to declare the test as parameterized: @RunWith(Parameterized.class)
so I cannot add both to the same class.
I found a similar question in this site that did not help much. So far, all the examples I have found explain how to parameterize simple unit tests, not a complete test tuite.
I agree, it's not possible with the provided classes, but there are workarounds that will get you most of the way there, like @mikemil's.
I've spent some time extending Suite and delegating to Parameterized, with partial success; it is possible to build runner that does what you want, and the code is more-or-less written for you in those two classes. The way those classes interact (in particular, the definition of
Parameterized#getChildren()
) makes it difficult to extend or delegate to those classes to accomplish what you need, but creating a whole new class than extendsParentRunner
and lifts code from the other two would be fairly easy.I'll try to get more time to come back to this later. If you do build a new runner before I get around to it, please post it as an answer, I'd love to use it myself.
As already stated multiple times, it's not possible to parameterize a test suite with the runners provided by
JUnit 4
.Anyway, I wouldn't recommend to make your testclasses dependent from some externally provided state. What if you want to run a single testclass?
I would recommend to make your separate test classes
@Parameterized
and use a utility class to provide the parameters:I was able to parameterize a test suite and use its data in a test class member of the suite as follows:
In JUTsuite:
Next, in test class:
the best solution will be, keep suit classes separately in a blank class. For example, I am testing logins as Parameterized tests and putting in a suit (for navigation performance measurement)
and LoginPageTest is actually Parameterizedtests
Maybe this answer helps: Parameterized unit test suites
It uses
@RunWith(Enclosed.class)
and seems to solve the problem.I believe the basic answer is No, because as you said, the @RunsWith only take one parameter. I found a blog posting that got a bit creative in how to handle this situation.
We don't use the parameterized tests, but may you could create a separate suite like we do that only lists the test classes and the parameterized test could be part of that. I modified our test suite to include a parameterized test class to part of the suite and it ran fine. We create our suite like below where PrimeNumberCheckerTest was a simple I pulled from the web.
Here's the source for the parameterized test case;