-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Introduce support for parameterized classes #878
Comments
I can imagine supporting something like @ArgumentSource(...)
class MyTest {
@ParameterizedTest
void feature1() { ... }
@ParameterizedTest
void feature2() { ... }
} Would that suit your needs? |
I think that is a good solution. |
For real-life examples, see GaugeTest and most of the other tests in its package. |
@ParameterizedTest
on class types for TCKs
@jkschneider Do you have time to work on a PR? |
If a parameter source annotation is placed on an individual method in a type that's been annotated like this would it make sense for the more specific annotation to take precedence? |
Yes as a matter of determinism? I think in practice, this would not be the way I'd recommend folks structure their test. |
Is this feature on the roadmap? I can't migrate to 5 without it. |
...unless someone knows of a workaround. |
@wrlyonsjr I flipped it around with Micrometer's TCK by defining an abstract class with my test methods, each of which takes an implementation of Then there is a corresponding test for each implementation of The approach has the disadvantage that you can't run the TCK abstract class from the IDE and execute the tests for all implementations, but this is the best I could do. |
It seems like my problem could be solved with TestTemplate et al. |
Moved to 5.1 backlog due to repeated requests for a feature like this at conferences, etc. |
Let me share my experience with parameterized tests in the new JUnit. Hope that helps to clarify the uses cases and requirements for Parameterized classes. I think that this feature will be universally useful, not for TCK only. I see its ultimate goal in bringing down the cost of defining multiple tests sharing the same parameters source. Currently, the following code is duplicated:
Having to repeat that code discourages the users to write short, focused tests. On top of that, if developers do have a luxury of copy-pasting that code, reviewers and maintainers have to read all of it. As an alternative, I've tried a dynamic test for a simple use case (little setup code, no teardown), but got both a personal impression and some reviews from colleagues that it's "overcomplicated". Uses casesA perfect use case for this feature, in my opinion, is the following:
|
I think supporting something like I'm out of ideas where you'd put formatting strings for parameters that are shared for all parameterized tests, though. |
@marcphilipp , I can think of a class-level annotation with If no one on the core team is planning to work on this issue soon, I may try to implement an MVP. I am new to the code base, and have a couple of questions about the requirements:
@CsvSource(/* primary parameters, injected into constructor/BeforeEach/fields */)
class FooTest {
/** Invoked for each parameter in the source specified at the class level */
@ParameterizedTest
void foo() { }
/**
* Invoked for the cartesian product of parameters from
* the source specified at the class level
* and parameters from the source at the method level.
*/
@ParameterizedTest
@ValueSource(/* secondary parameters for #bar only */)
void bar(int secondaryParameter) { }
} ? |
Is there a particular reason why you'd prefer an |
so the problem is as follows: we have a bunch of junit4 tests which look like
An ideal situation would be to modify only a few lines - the class annotation, and the method which supplies the parameters, leaving the rest as is. Whether the parameters are injected into fields or passed to the class constructor is not that important (I think). edit: here is an example: |
You mean something like : @ParameterizedContainer(name = ...)
@ValueSource(strings = { "param1", "param2", "param3"})
public class TestSuite {
public TestSuite(String param1) {
objectToTest = new MyObjectToTest(param1);
} ? The reason is to avoid too much code modification from (Probably, not so important because, I guess there will be lot of code modification anyway 🤔 ) |
Personally I'm happy with both options but the |
@royteeuwen Yes, this will provide a clear migration path from JUnit 4's @junit-team/junit-5 One thing we need to discuss are the semantics of junit5/junit-jupiter-api/src/main/java/org/junit/jupiter/api/BeforeAll.java Lines 27 to 28 in 656b9ed
Those new annotations could be specific to junit-jupiter-params and be called |
I've just submitted #4342 which introduces Feedback welcome! |
Team decision: Rename |
Overview
Currently, the target of
@ParameterizedTest
is constrained to methods. When creating technology compatibility kits, it would be awesome to be able to apply this (or a similar annotation) to the test class so that all tests in that class are parameterized the same way.Proposal
Rather than:
Something like:
Related Issues
The text was updated successfully, but these errors were encountered: