Skip to content
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

Add ParameterizedTest#argumentCountValidation #4045

Merged

Conversation

JonasJebing
Copy link
Contributor

@JonasJebing JonasJebing commented Oct 4, 2024

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 to strict or ParameterizedTest#argumentCountValidation is set to ArgumentCountValidationMode.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 on org.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

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`.
@JonasJebing JonasJebing force-pushed the argument-count-validation-mode branch from 2b42de8 to 2e56d56 Compare October 5, 2024 20:30
Comment on lines 151 to 152
logger.warn(() -> String.format(
"Ignored invalid configuration '%s' set via the '%s' configuration parameter.", value, key));
Copy link
Member

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.

Copy link
Contributor Author

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.

@marcphilipp
Copy link
Member

  • Should these additions be declared as experimental or stable?

Our guideline is to introduce new features as "experimental".

  • Should this feature be documented in the User Guide and Release Notes, given that it is declared as experimental?

I think we should add a small sample to the User Guide otherwise the chance to get feedback is slim.

  • Should the new precondition be documented on org.junit.jupiter.params.ParameterizedTestExtension#provideTestTemplateInvocationContexts, even though it is just an interface override and none of the existing preconditions are documented there?

Documenting preconditions is mainly interesting for public API so we can omit it here since ParameterizedTestExtension is package-private.

# 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
@JonasJebing
Copy link
Contributor Author

Thank you for all the pointers and comments on this PR @marcphilipp. I've addressed all your comments. Could you have another look?

@JonasJebing JonasJebing marked this pull request as ready for review October 23, 2024 15:59
Copy link
Member

@marcphilipp marcphilipp left a 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.

@JonasJebing JonasJebing force-pushed the argument-count-validation-mode branch from 65cd018 to 0bf0b05 Compare October 24, 2024 08:43
@JonasJebing
Copy link
Contributor Author

@marcphilipp could you review again? I applied all suggestions and added a release note.

@marcphilipp marcphilipp self-requested a review October 29, 2024 09:23
Copy link
Member

@marcphilipp marcphilipp left a 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.

This should result in invocations with valid argument counts still
being executed and only invocations with invalid argument counts fail.

Issue junit-team#3708
@JonasJebing
Copy link
Contributor Author

@marcphilipp do you know how to fix the DiskLruCache issue on the Windows check? I see some PRs, like #4120 are having the same issue.

@marcphilipp
Copy link
Member

@marcphilipp do you know how to fix the DiskLruCache issue on the Windows check? I see some PRs, like #4120 are having the same issue.

Not sure. It might be a problem that started after updating Gradle to 8.11. 🤔

@marcphilipp
Copy link
Member

A rerun succeeded. It does seem to happen since yesterday's Gradle update. I'll keep an eye on it.

@JonasJebing
Copy link
Contributor Author

JonasJebing commented Nov 13, 2024

@marcphilipp could you review the latest changes? Like you suggested, the ArgumentCountValidator now only fails the invocations that have too many arguments and still runs the other invocations that have the correct number of arguments and I also added an integration test for that case.

Copy link
Member

@marcphilipp marcphilipp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! I've only two small suggestions. Please let me know if you have time to do those or whether I should take over.

}

private ExtensionContext.Store getStore(ExtensionContext context) {
return context.getRoot().getStore(ExtensionContext.Namespace.create(getClass()));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please create a constant for ExtensionContext.Namespace.create(ArgumentCountValidator.class).

Comment on lines 71 to 73
ParameterizedTest parameterizedTest = findAnnotation(//
extensionContext.getRequiredTestMethod(), ParameterizedTest.class//
).orElseThrow(NoSuchElementException::new);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of looking up the annotation, we should pass ParameterizedTestMethodContext to the constructor

@JonasJebing
Copy link
Contributor Author

Looks good! I've only two small suggestions. Please let me know if you have time to do those or whether I should take over.

Thanks! I'll make those changes you suggested today or tomorrow :)

@JonasJebing
Copy link
Contributor Author

@marcphilipp could you merge the PR? And of course feel free to review the small change you suggested.

@marcphilipp marcphilipp changed the title Issue: #3708 add ParameterizedTest#argumentCountValidation Add ParameterizedTest#argumentCountValidation Nov 17, 2024
@marcphilipp marcphilipp merged commit 6082336 into junit-team:main Nov 17, 2024
15 checks passed
@marcphilipp
Copy link
Member

@JonasJebing Thank you for your contribution! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

@ParameterizedTest ignores additional arguments beyond formal parameter list
3 participants