junit5junit5-extension-model

JUnit 5 Jupiter tags using extension


All my DB tests are annotated with a custom DbExtension extension. I want to also tag these tests so I can run them separately in the CI. Is there anything I can add on the extension class so that it will tag all extending tests?

just to illustrate (in kotlin):

    class DatabaseExtension : ParameterResolver, AfterEachCallback {
       //setup connection
    }

    @ExtendWith(DatabaseExtension::class)
    @Tag("db) //Can we have this tag in the extension class instead of each test
    class MyDbTest {

    } 

Solution

  • Not at/by the extension, but that would be a neat feature. Do you mind opening a feature request at https://github.com/junit-team/junit5/issues/new/choose ?

    You may create your own composed annotation, though. Something like the following:

    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    import org.junit.jupiter.api.Tag;
    import org.junit.jupiter.api.extension.ExtendWith;
    
    @ExtendWith(DatabaseExtension.class)
    @Tag("db")
    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface Databased {}
    

    Usage:

    @Databased
    class MyDbTest {} 
    

    See https://junit.org/junit5/docs/current/user-guide/#writing-tests-meta-annotations for more details.