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

Support @ParameterizedTest in conjunction with @TestFactory #1192

Closed
reitzmichnicht opened this issue Nov 29, 2017 · 11 comments
Closed

Support @ParameterizedTest in conjunction with @TestFactory #1192

reitzmichnicht opened this issue Nov 29, 2017 · 11 comments

Comments

@reitzmichnicht
Copy link

reitzmichnicht commented Nov 29, 2017

Overview

I started to write some dynamic tests, and now it would be handy to reuse some @ParameterizedTest functionality I already implemented.

Take a look at this example:

  @ParameterizedTest(name = "{0}")
  @ArgumentsSource(ClassArgumentsProvider.class)
  @TestFactory
  Stream<DynamicTest> apiGuards(Class<?> c) {
    return ApiGuardHelper.checkGuards(() -> EnhancedRandom.random(c));
  }

This should generate some dynamic tests for each class found in the package, but sadly the @ParameterizedTest annotation is not supported with a @TestFactory method.

Kind regards,
Michael

Related Issues

@sbrannen sbrannen changed the title Support ParametrizedTest for TestFactories Support @ParameterizedTest in conjunction with @TestFactory Nov 29, 2017
@sbrannen
Copy link
Member

sbrannen commented Nov 29, 2017

Why don't you just extract the logic from your ClassArgumentsProvider that generates the stream of classes and use that stream directly within your @TestFactory method to generate the Stream<DynamicTest> (instead of the Stream<Arguments> created in your provider)?

You could put that in a common utility and use the utility (e.g., static factory method) both from your provider as well as in test factories.

@sbrannen
Copy link
Member

FWIW, it is not feasible for JUnit Jupiter to treat a single method as two containers simultaneously: it just doesn't make sense.

Note that a @ParameterizedTest and a @TestFactory are both technically containers as opposed to tests (in terms of org.junit.platform.engine.TestDescriptor.Type).

@sbrannen
Copy link
Member

Relates to #871 and #878.

@sbrannen sbrannen added this to the 5.1 Backlog milestone Nov 29, 2017
@sbrannen
Copy link
Member

Assigned to 5.1 backlog for team discussion.

@sbrannen
Copy link
Member

ApiGuardHelper

Is this related to the API Guardian project created by the JUnit 5 team?

@reitzmichnicht
Copy link
Author

This is my junit5 implementation of guard assertions as described here:
https://madstt.dk/verify-your-guards-with-autofixture/

It takes all public methods of a class and checks if null arguments are safely handled. So it generates for each parameter of each method a dynamic test. Now I want combine this to get these tests generated for each class in the package. So I want nested Testcontainers like nested classes:
GuardTest
-- Class A
-- -- Method B Param1
-- Class C
...

Combining a TestFactory with ParametrizedTest was the most obvious for me.

@sbrannen
Copy link
Member

I want nested Testcontainers like nested classes

We actually already have support for nested dynamic containers! 😉

In the Dynamic Test Examples section of the User Guide you'll find a test method named dynamicTestsWithContainers() which demonstrates this feature.

I think that will cover your needs, but please reply here to let us know in any case.

Cheers!

Sam

@sormuras
Copy link
Member

sormuras commented Nov 30, 2017

Here, at ConsoleDetailsTests is another example of dynamic container usage which produces a tree like:

consoledetailsteststree

@reitzmichnicht
Copy link
Author

When it is supported by code to have nested dynamic containers it should also be possible by annotation.
It's always better to have tests maintainable and understandable which is more given by annotations instead of creating dynamic containers in code.

@marcphilipp
Copy link
Member

I'm closing this issue as "declined".

@prdoyle
Copy link

prdoyle commented Aug 8, 2020

Re @sbrannen "Why don't you just extract the logic from your ClassArgumentsProvider" - because I'm already using it with other @Test methods, and I want to use the same set of arguments in a @TestFactory.

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

No branches or pull requests

5 participants