JUnit5 run tests by tags

junit 5 tags
junit 5 test suite
junit 5 tag(integration test)
junit 5 parameterized test
junit 4 tags
junit 5 maven
junit vintage
junit 5 gradle

I have a setup Maven+JUnit5+Selenium, my pom.xml https://github.com/13Dima13/G/blob/master/pom.xml

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.0</version>
            <configuration>
                <forkCount>4</forkCount>
                <reuseForks>false</reuseForks>
                <properties>
                    <includeTags>${tag}</includeTags>
                </properties>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.junit.platform</groupId>
                    <artifactId>junit-platform-surefire-provider</artifactId>
                    <version>${junit.platform.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                    <version>${junit.jupiter.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.apache.maven.surefire</groupId>
                    <artifactId>surefire-logger-api</artifactId>
                    <version>${surefire-logger-api}</version>
                </dependency>
            </dependencies>
        </plugin>

First i thought that @Tag works fine, because when i put an annotation to my class https://github.com/13Dima13/G/blob/master/src/test/java/com/example/project/TestThreads2.java

@Test
@Tag("smoke")
public void test1() {
    open("https://mvnrepository.com/artifact/log4j/log4j/1.2.17");
    $("#maincontent > table > tbody > tr:nth-child(1) > th").waitUntil(Condition.appears, 120000);
    $("#maincontent > table > tbody > tr:nth-child(1) > th").shouldBe(Condition.text("License"));
    assertEquals("Maven Repository: log4j » log4j » 1.2.17", title());
    out.println("test1 Passed");
}

and then ran from terminal

mvn test -Dtag=smoke

it executed only the tests which were marked as @Tag("smoke"), so in my case only one test and this made me happy.

But when I started to use it on my real project I realized that it does not work sometimes.

For example, if my Test.class with tests methods is not placed in the parent project folder (for example inside child of project folder) https://github.com/13Dima13/G/blob/master/src/test/java/com/example/project/test2/GoogleSearch.java, annotation will not work at all.

So annotation @Tag doesn't work for the whole project or did I miss something?

Good example https://ibb.co/gjbSZJ

First guess: The name of your test class (test3.java) does not match maven‘s default pattern for test classes and it will therefore be ignored.

Tagging and Filtering JUnit Tests, JUnit 5 @Tag can be used to filter testcases from test plans. It can help in create multiple different test plans for different environments, different use-cases or any specific requirement. You can execute set of tests by including only those tagged tests in test plan OR by excluding other tests from test plan. Run Junit 5 tag specific tests Starting with Gradle version 4.6, Gradle provides native support for executing tests on the JUnit Platform. To include or exclude tag specific Junit 5 tests you need to include includeTags and excludeTags in build.gradle like following example.

The support of JUnit 5 is contained in Maven Surefire Plugin Version 2.22.0

<dependencies>
    [...]
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.2.0</version>
        <scope>test</scope>
    </dependency>
    [...]
</dependencies>

and maybe you need to define maven-surefire-plugin like this:

<build>
    <plugins>
        ...
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.0</version>
            <configuration>
                <excludes>
                    <exclude>some test to exclude here</exclude>
                </excludes>
            </configuration>
        </plugin>
    </plugins>
</build>

So your configuration can be simplified.

JUnit 5 @Tag Annotation Example, Maven filtering tests. 2.1 In Maven, we can run tests based on tags via the configuration parameters of the maven-surefire-plugin. pom.xml. JUnit5 run tests by tags. Ask Question Asked 1 year, 5 months ago. Active 1 month ago. Viewed 1k times 0. I have a setup Maven+JUnit5+Selenium, my pom.xml

Give

mvn test -D groups=smoke

a try...

Or, if you like to see results pretty fast (and don't need to have your code recompiled), go for:

mvn surefire:test -D groups=smoke

Note that the space after -D is optional and purely for cosmetic reasons.

N.B. i am also rather new to the java/maven world, but for what it's worth: i never got it to work with -D tag in the first place so far. Would love to know what's up with that (comments linking to an explanation?)

JUnit 5 Tagging and Filtering, @Tag examples, First guess: The name of your test class ( test3.java ) does not match maven's default pattern for test classes and it will therefore be ignored. # Run tests which tagged with `integration, slow, feature-168` $ mvn -Dgroups="integration, fast, feature-168" # Exclude tests which tagged with 'slow' $ mvn -DexcludedGroups="slow" 3. Gradle filtering tests. 3.1 In Gradle, we can filter the tags like this:

JUnit5 run tests by tags, Learn how to tag tests with `@Tag` annotation. Also learn how to selectively run certain Duration: 4:18 Posted: Mar 3, 2019 JUnit 5 allows various Tag Expressions which can be used to filter the tags. For example, to run everything but the integration tests, we could use !IntegrationTest as the Tag Expression. Or for executing both UnitTest and IntegrationTest, we can use UnitTest | IntegrationTest.

JUnit 5 Basics 25 - Tagging tests with @Tag, Junit 5 Filter Or discover and execute tagged tests. 3.1. Maven filtering Junit 5 @ Tag tests using surefire plugin. You can use JUnit5 Tags and� 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

Junit 5 @Tag and Filtering tagged tests with Examples, If you want to write and execute JUnit 3 or 4 tests via the JUnit Platform You can use JUnit5 Tags and filter tests by tags or tag expressions. By default any class name is accepted, and thus all classes with tests are included. -t, --include-tag Provide a tag to be included in the test run. This option can be repeated. -T, --exclude-tag Provide a tag to be excluded from the test run.

Comments