How to choose which JUnit5 Tags to execute with Maven

15,348

Solution 1

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).

Solution 2

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>

Solution 3

You can omit profile and just use properties, it is more elastic way.

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

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

Then you can runt fast tests by typing mvn test, all test by typing mvn test -Dtests=fast | slow or only slow by typing mvn test -Dtests=slow. When you have more test tags then you can also run all of them except the selected type by typing mvn test -Dtests="! contract".

Share:
15,348
Dan Ciborowski - MSFT
Author by

Dan Ciborowski - MSFT

Software Engineer at Microsoft, Azure IMML http://area51.stackexchange.com/proposals/82757/cloud-computing-platforms?referrer=I67NTAGkhSwgCqb_Ka-8vA2

Updated on June 06, 2022

Comments

  • Dan Ciborowski - MSFT
    Dan Ciborowski - MSFT almost 2 years

    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>