junit-team / junit5

✅ The 5th major version of the programmer-friendly testing framework for Java and the JVM
https://junit.org
Eclipse Public License 2.0
6.44k stars 1.5k forks source link

Inheritance support for conditional annotations #4147

Open scordio opened 4 days ago

scordio commented 4 days ago

I faced a use case where inheritance for @EnabledIfSystemProperty would have helped for less boilerplate.

Specifically, it's a project with many test classes for integration tests that should be executed only on the CI infrastructure and specific runners. Such fine-grained control is currently achieved combining the maven-failsafe-plugin to exclude the local environment and @EnabledIfSystemProperty(named = "it.test", matches = ".+") on each test class to ensure that they are executed only under the appropriate CI jobs (the it.test property is set to different values by each CI job to tune the failsafe plugin execution further).

As the test classes already share common parts in a parent class, pulling @EnabledIfSystemProperty to the parent class would help reduce the boilerplate.

Relates to https://github.com/junit-team/junit5/issues/3462#issuecomment-1721053528.

Deliverables

marcphilipp commented 2 days ago

Thinking out loud here, I think we have at least the following options:

  1. Make the existing annotations @Inherited
  2. Add a variant for each annotation that is @Inherited
  3. Change the annotation search algorithm for test classes to check parent classes as well

Both (1) and (3) would be breaking changes. For (2), I think we could generate the @Inherited variant, and add some glue code that uses a java.lang.reflect.Proxy so the majority of the code wouldn't have to change.

Any other ideas?