Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to choose which JUnit5 Tags to execute with Maven

I have just upgraded my solution to use JUnit5. Now trying to create tags for my tests that have two tags: @Fast and @Slow. To start off I have used the below maven entry to configure which test to run with my default build. This means that when I execute mvn test only my fast tests will execute. I assume I can override this using the command line. But I can not figure out what I would enter to run my slow tests....

I assumed something like.... mvn test -Dmaven.IncludeTags=fast,slow which does not work

<plugin>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19.1</version>
    <configuration>
        <properties>
            <includeTags>fast</includeTags>
            <excludeTags>slow</excludeTags>
        </properties>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.0.0-M3</version>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-surefire-provider</artifactId>
            <version>1.0.0-M3</version>
        </dependency>
    </dependencies>
</plugin>
like image 255
Dan Ciborowski - MSFT Avatar asked Feb 23 '17 16:02

Dan Ciborowski - MSFT


2 Answers

You can use this way:

<properties>
    <tests>fast</tests>
</properties>

<profiles>
    <profile>
        <id>allTests</id>
        <properties>
            <tests>fast,slow</tests>
        </properties>
    </profile>
</profiles>

<build>
    <plugins>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>3.0.0-M5</version>
            <configuration>
                <groups>${tests}</groups>
            </configuration>
        </plugin>
    </plugins>
</build>

This way you can start with mvn -PallTests test all tests (or even with mvn -Dtests=fast,slow test).

like image 145
Niklas P Avatar answered Sep 22 '22 08:09

Niklas P


Using a profile is a possibility but it is not mandatory as groups and excludedGroups are user properties defined in the maven surefire plugin to respectively include and exclude any JUnit 5 tags (and it also works with JUnit 4 and TestNG test filtering mechanism).
So to execute tests tagged with slow or fast you can run :

mvn test -Dgroups=fast,slow

If you want to define the excluded and/or included tags in a Maven profile you don't need to declare a new property to convey them and to make the association of them in the maven surefire plugin. Just use groups and or excludedGroups defined and expected by the maven surefire plugin :

<profiles>
    <profile>
        <id>allTests</id>
        <properties>
            <groups>fast,slow</groups>
        </properties>
    </profile>
</profiles>
like image 35
davidxxx Avatar answered Sep 20 '22 08:09

davidxxx