-
-
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 containers (classes, records, etc) #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) { }
} ? |
It would be nice to support a clearer migration path from junit4 to junit5 with class-level parameterized tests. |
@andy-goryachev-oracle, this is scheduled for 5.12 M1, and there's a good chance it will make it into 5.12. |
@sbrannen Is there a link where we can see how it will look like and/or how we will be able to use it ? (sorry, if answer is already given in previous comment, I didn't re-read the whole post 🙇♀️) |
It will probably look sth. like this: @ParameterizedContainer
@ArgumentSource(...) // any `@...Source` annotation
class SomeTests {
@Parameter(0) // Alternatively, constructor injection should be supported, e.g. for use with records as test classes
String firstArg;
@Parameter(1)
int secondArg;
// test methods as usual
} |
Thx for quick answer 🙏 ! it could help migration for people who use the workarround 1) from : #3157 (comment) |
Are you referring to supporting resolving class-level parameters for |
I like that very much. Injection or the constructor. The main idea is to minimize modification to the existing tests that use |
I was talking about "injecting" parameter as method argument instead of class attribute. @ParameterizedContainer(name = ...)
@ValueSource(strings = { "param1", "param2", "param3"})
public class TestSuite {
@BeforeEach
@Parameterized
public void start(String param1) {
objectToTest = new MyObjectToTest(param);
}
... ... ou @ParameterizedContainer(name = ...)
@ValueSource(strings = { "param1", "param2", "param3"})
public class TestSuite {
@BeforeEach
public void start(@Parameter(0) String param) {
objectToTest = new MyObjectToTest(param);
}
... .. |
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 |
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: