-
-
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
Issue: #3708 add ParameterizedTest#argumentCountValidation #4045
base: main
Are you sure you want to change the base?
Issue: #3708 add ParameterizedTest#argumentCountValidation #4045
Conversation
This allows parameterized tests to fail when there are more arguments provided than declared by the test method. This is done in a backwards compatible way by only enabling that validation when the new `junit.jupiter.params.argumentCountValidation` is set to `strict` or `ParameterizedTest#argumentCountValidation` is set to `ArgumentCountValidationMode.STRICT`.
2b42de8
to
2e56d56
Compare
junit-jupiter-params/src/main/java/org/junit/jupiter/params/ParameterizedTestExtension.java
Outdated
Show resolved
Hide resolved
junit-jupiter-params/src/main/java/org/junit/jupiter/params/ParameterizedTestExtension.java
Outdated
Show resolved
Hide resolved
logger.warn(() -> String.format( | ||
"Ignored invalid configuration '%s' set via the '%s' configuration parameter.", value, key)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message should be the same as in EnumConfigurationParameterConverter
. Unfortunately, we can't (easily) reuse that here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there some way that we could make it more easily reusable across the project? Maybe I could add another PR to address that after merging this one.
For now, I basically copied the messages from EnumConfigurationParameterConverter
.
jupiter-tests/src/test/java/org/junit/jupiter/params/ParameterizedTestIntegrationTests.java
Outdated
Show resolved
Hide resolved
jupiter-tests/src/test/java/org/junit/jupiter/params/ParameterizedTestIntegrationTests.java
Show resolved
Hide resolved
Our guideline is to introduce new features as "experimental".
I think we should add a small sample to the User Guide otherwise the chance to get feedback is slim.
Documenting preconditions is mainly interesting for public API so we can omit it here since |
# Conflicts: # junit-jupiter-params/src/main/java/org/junit/jupiter/params/ParameterizedTest.java # junit-jupiter-params/src/main/java/org/junit/jupiter/params/ParameterizedTestExtension.java
- add `writing-tests-parameterized-tests-argument-count-validation` section to the user guide. - cache configuration parameter in `ExtensionContext.Store` - adjust log and error messages Issue junit-team#3708
Thank you for all the pointers and comments on this PR @marcphilipp. I've addressed all your comments. Could you have another look? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for making those changes! I think we're very close now. Please also add an entry to the 5.12.0-M1 release notes.
junit-jupiter-params/src/main/java/org/junit/jupiter/params/ArgumentCountValidationMode.java
Outdated
Show resolved
Hide resolved
junit-jupiter-params/src/main/java/org/junit/jupiter/params/ParameterizedTest.java
Outdated
Show resolved
Hide resolved
junit-jupiter-params/src/main/java/org/junit/jupiter/params/ParameterizedTestExtension.java
Outdated
Show resolved
Hide resolved
junit-jupiter-params/src/main/java/org/junit/jupiter/params/ParameterizedTestExtension.java
Outdated
Show resolved
Hide resolved
release-notes-5.12.0-M1-junit-jupiter-new-features-and-improvements to be exact Issue junit-team#3708
Co-authored-by: Marc Philipp <mail@marcphilipp.de>
Co-authored-by: Marc Philipp <mail@marcphilipp.de>
Co-authored-by: Marc Philipp <mail@marcphilipp.de>
Co-authored-by: Marc Philipp <mail@marcphilipp.de>
65cd018
to
0bf0b05
Compare
documentation/src/docs/asciidoc/release-notes/release-notes-5.12.0-M1.adoc
Outdated
Show resolved
Hide resolved
@marcphilipp could you review again? I applied all suggestions and added a release note. |
documentation/src/docs/asciidoc/release-notes/release-notes-5.12.0-M1.adoc
Outdated
Show resolved
Hide resolved
documentation/src/docs/asciidoc/release-notes/release-notes-5.12.0-M1.adoc
Outdated
Show resolved
Hide resolved
@ParameterizedTest(argumentCountValidation = ArgumentCountValidationMode.STRICT) | ||
@CsvSource({ "42, -666" }) | ||
void testWithArgumentCountValidation(int number) { | ||
assertTrue(number > 0); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move this snippet to ParameterizedTestDemo
(and annotate it with @ExpectToFail
; see other usages on how to avoid that annotation from being included in the user guide). We include them from there to ensure they compile and pass or fail as expected.
Co-authored-by: Marc Philipp <mail@marcphilipp.de>
Co-authored-by: Marc Philipp <mail@marcphilipp.de>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like @ExpectToFail
doesn't work as I had expected. 😅 I'll take a closer look.
junit-jupiter-params/src/main/java/org/junit/jupiter/params/ParameterizedTestExtension.java
Outdated
Show resolved
Hide resolved
@@ -82,6 +90,7 @@ public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContex | |||
.map(provider -> AnnotationConsumerInitializer.initialize(methodContext.method, provider)) | |||
.flatMap(provider -> arguments(provider, extensionContext)) | |||
.map(arguments -> { | |||
validateArgumentCount(extensionContext, arguments); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Playing around with this made me realize that we need to defer this validation until the invocation is being executed. Otherwise, all invocations will fail rather than just the offending ones. For example, annotating the added test in ParameterizedTestDemo
with @CsvSource({ "42, -666", "1", "2", "3" })
should result in the first invocation being reported as failed due to the new validation but all others should pass.
I think the cleanest way to do this is to return another extension from ParameterizedTestInvocationContext#getAdditionalExtensions
, say ArgumentCountValidator
, that implements InvocationInterceptor
and overrides interceptTestTemplateMethod
with the validation before calling invocation.proceed()
.
Please let me know whether that's enough information for you to proceed. Alternatively, I'd also be willing to take over if you'd prefer that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I also thought something wasn't quite right after playing around with ParameterizedTestDemo. Thanks for the pointers with ParameterizedTestInvocationContext#getAdditionalExtensions
. That definitely seems doable to me.
This should result in invocations with valid argument counts still being executed and only invocations with invalid argument counts fail. Issue junit-team#3708
Overview
This allows parameterized tests to fail
when there are more arguments provided than declared by the test method. This is done in a backwards compatible way
by only enabling that validation when the new
junit.jupiter.params.argumentCountValidation
is set tostrict
orParameterizedTest#argumentCountValidation
is set toArgumentCountValidationMode.STRICT
.Open Questions
Should these additions be declared as experimental or stable?Should this feature be documented in the User Guide and Release Notes, given that it is declared as experimental?Should the new precondition be documented onorg.junit.jupiter.params.ParameterizedTestExtension#provideTestTemplateInvocationContexts
, even though it is just an interface override and none of the existing preconditions are documented there?I hereby agree to the terms of the JUnit Contributor License Agreement.
Definition of Done
@API
annotations