-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Support multiple annotations as source for Parameterized Tests #1101
Comments
You could either use a MethodSource @ParameterizedTest
@MethodSource("stringAndIntProvider")
void testWithMultiArgMethodSource(String first, int second) {
assertNotNull(first);
assertNotEquals(0, second);
}
static Stream<Arguments> stringAndIntProvider() {
return Stream.of(Arguments.of("foo", 1), Arguments.of("bar", 2));
} or unroll it to distinct (individually startable) test methods. Why do you want to "program logic" with annotations, anyway? |
Thanks for the quick reply. So, the feature request with multiple annotations is for readability and simplicity. |
In addition to the This is my preferred way. Clean, simple, basic. Full life-cyle support. Full IDE support for executing a single test:
Use
Use
All 3 propsals refer to this "internal" method:
With CsvSource and MethodSource included, I don't think we need a sixth way. :) |
@sormuras your recent examples are totally correct, but not really regarded to Parameterized Tests ;-) Surly these are different ways how to express the different cases. My point is simply to make it as easy as possible to read and understand the tests, which is not really the point for current implementation of @ParameterizedTest So, I'd suggest to keep the feature request open for a while for team and community discussion. |
You are restricted by the types that can be in an annotation, so keep that in mind when you are trying to model these tests. If you do really want this behavior, you could implement your own extension, too.
What would you like to see built into JUnit 5 to support your use case? |
Thanks for your neat example. The generic solution would a String array for value() similar to Annotation
ParameterSource:
Test:
IMHO this is still more readable than CSVSource, which I expect to see often:
|
You can also write the above like this: @ParameterizedTest
@CsvSource({
"10, 10000",
"20, 20000",
"30, 30000"
})
void calculate(int input, String expected) {
assertEquals(expected, Integer.toString(input * 1000));
} Another possible API for an extension: @ParameterizedTest
@ParameterSource({
@Parameter({"10", "10000"})
@Parameter({"20", "20000"})
@Parameter({"30", "30000"})
})
void calculate(int input, String expected) {
assertEquals(expected, Integer.toString(input * 1000));
} In my opinion, readability differs only very slightly. Thus, I'm closing this issue for now but encourage you to implement it as an extension. 🙂 |
Some second thoughts and practical implications:
|
@jochenchrist @JensPiegsa I could imagine supporting the following: @ParameterizedTest
@ValueSource({ @Values({"foo", "1"}), @Values({"bar", "2"}), @Values({"baz, qux", "3"}) })
void testWithValuesInValueSource(String first, int second) {
assertNotNull(first);
assertNotEquals(0, second);
}
@junit-team/junit-lambda Thoughts? |
Added to 5.3 Backlog for team discussion. |
I just came across this issue because of NUnit' [TestCase(12, 3, ExpectedResult=4)]
[TestCase(12, 2, ExpectedResult=6)]
[TestCase(12, 4, ExpectedResult=3)]
public int DivideTest(int n, int d)
{
return n / d;
} Especially an equivalent of the |
This functionality has been implemented as part of jUnit Pioneer with their @CartesianProductTest pom.xml
Test.java
Maybe this is something that should be considered for addition to junit core? |
Tentatively slated for 5.8 M2 solely for the purpose of team discussion. |
Team decision: Since the above syntax would only support strings, it would not provide much benefit compared to what's possible with |
Feature Request to simplify Parameterized Tests.
I am looking for an easy way to implement Parameterized Tests with multiple arguments.
While I could use
@CsvSource
, I'd appreciate multiple annotations, similar to NUnit'sTestCase
.Example:
Any thoughts on this?
The text was updated successfully, but these errors were encountered: