-
-
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
A way to provide the arguments "name" without actually having it as an argument. #2301
Comments
I had the exact same use case not long ago, and I ended up passing the name as an argument like you did. I agree that it's not ideal... That being said, I'm not sure how your solution whould deal with the |
I think in the proposed solution it would replace it, though you could also do that latter, I'm not certain on the right answer there. TBH I'm not even certain the proposed answer is the right answer. |
Personally I think I'd prefer the latter.
Well I guess there is no right answer anyway 🤓 |
I think the request in #1154 would also solve your problem, wouldn't it? |
no, seems completely unrelated, since mine was about having to receive a parameter that I'm not using at all, other than to go into the test name. That would still be true even if the parser was improved. #1154 is still a great idea though. Thing is, in my example filename isn't in the array at all, it serves no real purpose other than to describe the file that I'm reading |
Actually I might have described my proposed solution poorly, a better Api might be something like (obviously some pseudocode)
then it wouldn't actually pass that first argument to the method signature, but it would still be passed to the description. this is a pretty minor problem though. |
How about a simple |
personally not opposed |
I like the idea! 👍 |
Tentatively slated for 5.8 Backlog to be considered in conjunction with #2375. |
Just in case, there also was a previous discussion on this issue, where it had been considered to make
|
Just had a similar problem caused by useless descriptions generated for function-type arguments. One solution right now is something like this:
and then define the test as:
Finally, the method providing the parameters:
This seems already pretty fine. The only real benefit here would be for JUnit to support using Note that this solution obviously allows you to support any kind of description generation. You could easily extend it to use |
Team decision: Add a public interface Named<T> {
static <T> Named<T> of(String name, T payload) {
return new Named<T>() {
@Override
public String getName() {
return name;
}
@Override
public T getPayload() {
return payload;
}
@Override
public String toString() {
return name;
}
};
}
String getName();
T getPayload();
} |
This could be really useful when replicating Go style table-driven tests with import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertEquals;
class TableDrivenTest {
@TestFactory
Stream<DynamicTest> tableDrivenTestFromStream() {
record TestCase(String name, int a, int b, int sum) {
public void check() {
assertEquals(sum, a + b, name);
}
}
var testCases = Stream.of(
new TestCase("test1", 1, 2, 3),
new TestCase("test2", 2, 2, 4),
new TestCase("test3", 4, 2, 6)
);
return DynamicTest.stream(testCases, TestCase::name, TestCase::check);
}
} See: https://twitter.com/thomasdarimont/status/1312100991127285760 With the Btw. if the ...
record TestCase(String name, int a, int b, int sum) {
@Test
@DisplayName("$name: $a + $b = $sum")
public void check() {
assertEquals(sum, a + b, name);
}
}
... Here is a gist with some more examples: https://gist.github.com/thomasdarimont/1650ab4d914072bb32d32b58a9ccc571 |
What if we have several parameters in a test as follows ? @ParameterizedTest
@ArgumentsSource(DataProvider.class)
void test(String firstInput, String secondInput) {
// A test
} For such a test, I would get my parameters as follows : Stream.of(arguments("first input", "second input")); It seems that I cannot write Stream.of(arguments(Named.of("cool name", "first input", "second input"))); Is there any trick I missed ? |
Hence, you could do Stream.of(arguments(named("cool name", "first input"), "second input")); and then use the @ParameterizedTest(name = "{0}")
@ArgumentsSource(DataProvider.class)
void test(String firstInput, String secondInput) {
// A test
} |
I see, thank you @marcphilipp. |
So I have a parameterized test, and each parameter is a List of Beans where each bean represents a line in a csv file (opencsv). What I want for the actual "parameterized name" is the filename.
I'm thinking something like return
Obviously the workaround is to simply pass it as the first parameter and include it as an unused argument. I just have a dislike of unused arguments.
The text was updated successfully, but these errors were encountered: