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

Introduce zero invocations in test templates and parameterized tests #3890

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

nskvortsov
Copy link
Contributor

@nskvortsov nskvortsov commented Jul 17, 2024

Add a flag to ParameterizedTest to control arguments requirement. This allows users to explicitly opt out from validation of arguments set count and silently skip a test if no arguments are provided

In general, support TestTemplateInvocationContextProvider returning zero invocation contexts. Such providers must override new interface method to indicate that the framework should expect "no context returned"

Resolves #1477

Overview

Implemented the team decision described in this comment


I hereby agree to the terms of the JUnit Contributor License Agreement.


Definition of Done

Add a flag to ParameterizedTest to control arguments requirement. This
allows users to explicitly opt out from validation of arguments set
count and silently skip a test if no arguments are provided

In general, support TestTemplateInvocationContextProvider returning zero
invocation contexts. Such providers must override new interface method
to indicate that the framework should expect "no context returned"

Resolves junit-team#1477
@pain64
Copy link

pain64 commented Jul 24, 2024

Need this feature a lot! Is there a forecast for when it will be ready?

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 very promising! 👍

* @since 5.11
*/
@API(status = EXPERIMENTAL, since = "5.11")
default boolean mayReturnEmptyInvocationContext(ExtensionContext extensionContext) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
default boolean mayReturnEmptyInvocationContext(ExtensionContext extensionContext) {
default boolean mayReturnZeroInvocationContexts(ExtensionContext context) {

() -> "None of the supporting " + TestTemplateInvocationContextProvider.class.getSimpleName() + "s "
+ providers.stream().map(provider -> provider.getClass().getSimpleName()).collect(
joining(", ", "[", "]"))
+ providers.stream().filter(p -> !p.mayReturnEmptyInvocationContext(extensionContext)).map(
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we should filter them out here.

* to be invoked; never {@code null}
* @return {@code true} to allow zero contexts, {@code false} (default) to fail execution in case of zero contexts.
*
* @since 5.11
Copy link
Member

Choose a reason for hiding this comment

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

Should be 5.12 now

Comment on lines +105 to +107
return findAnnotation(templateMethod, ParameterizedTest.class).map(
parameterizedTest -> !parameterizedTest.requireArguments()).orElse(
TestTemplateInvocationContextProvider.super.mayReturnEmptyInvocationContext(extensionContext));
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
return findAnnotation(templateMethod, ParameterizedTest.class).map(
parameterizedTest -> !parameterizedTest.requireArguments()).orElse(
TestTemplateInvocationContextProvider.super.mayReturnEmptyInvocationContext(extensionContext));
return findAnnotation(templateMethod, ParameterizedTest.class) //
.map(parameterizedTest -> !parameterizedTest.requireArguments()) //
.orElse(false);

Comment on lines +91 to +92
* Signals that in the given {@linkplain ExtensionContext extension context} the provider may return zero
* {@linkplain TestTemplateInvocationContext invocation contexts}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* Signals that in the given {@linkplain ExtensionContext extension context} the provider may return zero
* {@linkplain TestTemplateInvocationContext invocation contexts}
* Signals that in the supplied {@linkplain ExtensionContext context} the provider may return zero
* {@linkplain TestTemplateInvocationContext invocation contexts}.

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.

Introduce support for zero invocations in test templates and parameterized tests
3 participants