-
-
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
Provide access to arguments of parameterized tests in lifecycle callback methods #944
Comments
FYI: this is a direct result of the changes performed in conjunction with #833. |
Assigned to 5.0 M6 in order to make a decision about how to proceed. |
The reason is that we no longer resolve parameters from |
Yes. I need the parameter on @beforeeach and @AfterEach for several tests for Selenium and Appium testing. My Appium test examples do not demonstrate 'need', but more like 'nice to have', but are public. https://github.com/larryricker/ProjectOrganizerAppium/ |
@larryricker Look at how Paul is solving the WebDriver injection task: https://github.com/paul-hammant/JUnit5_DependencyInjection_WebDriver |
If I've understood the topic correctly, I need this as well. I have a dozen or so complex objects that all my tests use that I set up statically. I recently identified a couple default parameters for the object creation that actually need to be tested at multiple values... My only options forward seem to be explicitly forwarding parameters to a setup function in each test case or downgrading to M4. Also, it would be nice if it somehow worked with BeforeAll so heavy immutable fixtures don't get generated and destroyed at each test method. |
Any news on this? Just migrated to JUnit 5 for the improved parameterized tests. "Forwarding parameters to a setup function in each test case" feels more like a step back, though. :-/ |
IMHO a setup function that gets parameterized test arguments only makes sense when all test methods in a class are parameterized and use at least similar parameters. Thus, I think it would make sense to support this when the class is parameterized instead of the method (as we plan to do in #878). |
I agree with @marcphilipp's analysis. |
Reproduces on 5.1.1 and on the latest 5.2.0-M1
@ParameterizedTest(name = "meaningful name and argument {3} based on argument {0} and argument {1}")
@CsvSource({
"6", "10", "40",
"1", "10", "90"
})
public void meaningfulTestName(String meaningfulArg1, String meaningfulArg2, String meaningfulArg3) {
} The same with long, int |
@Tradunsky, your stack trace is incomplete: Which executable? If it's for a |
@Tradunsky |
Indeed, good catch, @geo-m! |
It might be worth returning a custom interface so we can add convenience lookup methods to it later rather than adding more methods to interface ExtensionContext {
// ...
Optional<ResolvedArguments> getResolvedArguments();
}
interface ResolvedArguments {
List<?> getRawArguments();
// we can add lookup methods here later
} |
@t1 What was your use case for this enhancement? In which lifecycle methods or extension methods would you want to access the resolved parameters? Resolution happens just before the invocation of the test method so no lifecycle methods or callbacks that are invoked before the invocation would be able to access them. |
Team epiphany: There's already a way to access the arguments of test methods. An extension can implement |
The main reason I would want this is to be able to use extensions (like @ExtendWith(CreateDatabase.class)
class SchemaTest {
@Before
public void setup(javax.sql.Datasource db) {
// Create data here
}
@Test
public void test(javax.sql.Datasource db) {
// Verify data in the database
}
} |
@marcphilipp I have moved on to the next project and already completely forgot about this issue and the use-case behind it... my memory is terrible 😳. But I went back and created a reproducer, just to find out that... you are completely right: instead of using a |
Just in case anybody is interested, my reproducer/demo is here: https://github.com/t1/junit-testtemplate-invocationinterceptor-demo |
@danielhodder That's already possible by implementing a |
@t1 You mean |
@marcphilipp: yes. sorry for the confusion. |
To summarize, it's already possible to access the arguments of any test method, including parameterized ones, from an extension by implementing In general, parameter resolution happens immediately before invoking the method. Therefore, arguments cannot be provided to lifecycle callback methods that are invoked before (for example, However, in the specific case of |
👍 Minor addition: the same is also true for |
Overview
Simple parameter resolution for strings, ints, doubles, longs works in M4, but no longer works in M5 and throws the following exception.
Was previously using M4, upgraded to M5 and issue began occurring.
Example:
Do not get a build exception, get a runtime exception with jUnit5 with gradle 3.5 running in Jenkins.
Related Issues
@ParameterizedTest
available to test author #1668Deliverables
The text was updated successfully, but these errors were encountered: