Tags are
used to filter which tests are executed for a given test plan. For example, you
may want to run only performance tests on every release, you can do that by
tagging all the performance related tests.
In this
post, I am going to create three types of test cases.
a.
functional
b.
non-functional
c.
performance
FunctionalTest.java
package com.sample.app;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
public class FunctionalTest {
@Test
@Tag("functional")
public void functionalTest1() {
System.out.println("Executing functionalTest1");
}
@Test
@Tag("functional")
public void functionalTest2() {
System.out.println("Executing functionalTest2");
}
}
NonFunctionalTest.java
package com.sample.app;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
public class NonFunctionalTest {
@Test
@Tag("non-functional")
public void NonFunctionalTest1() {
System.out.println("Executing Non-functionalTest1");
}
@Test
@Tag("non-functional")
public void NonFunctionalTest2() {
System.out.println("Executing Non-functionalTest2");
}
}
PerformanceTest.java
package com.sample.app;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
public class PerformanceTest {
@Test
@Tag("performance")
public void perfTest1() {
System.out.println("Executing perfTest1");
}
@Test
@Tag("performance")
public void perfTest2() {
System.out.println("Executing perfTest2");
}
}
Now, we
can use ‘maven-surefire-plugin’ to execute tests based on a tag.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>${junit-platform.version}</version>
</dependency>
</dependencies>
<configuration>
<properties>
<includeTags>functional,performance</includeTags>
</properties>
</configuration>
</plugin>
</plugins>
</build>
Above
snippet executes only functional and performance tests.
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sample.app</groupId>
<artifactId>junit5-tag-filtering</artifactId>
<version>1</version>
<properties>
<junit-jupiter.version>5.6.1</junit-jupiter.version>
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
<junit-platform.version>1.3.2</junit-platform.version>
<java.version>1.8</java.version>
<maven.compiler.target>${java.version}</maven.compiler.target>
<maven.compiler.source>${java.version}</maven.compiler.source>
<project.encondig>UTF-8</project.encondig>
<project.build.sourceEncoding>${project.encondig}</project.build.sourceEncoding>
<project.reporting.outputEncoding>${project.encondig}</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${maven-surefire-plugin.version}</version>
<dependencies>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-surefire-provider</artifactId>
<version>${junit-platform.version}</version>
</dependency>
</dependencies>
<configuration>
<properties>
<includeTags>functional,performance</includeTags>
</properties>
</configuration>
</plugin>
</plugins>
</build>
</project>
How to
run the tests?
Right
click on pom.xml -> Run As -> Maven Test.
In the
console, you can confirm that functional and performance tests get executed.
[INFO] Running com.sample.app.FunctionalTest
Executing functionalTest1
Executing functionalTest2
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.024 s - in com.sample.app.FunctionalTest
[INFO] Running com.sample.app.PerformanceTest
Executing perfTest1
Executing perfTest2
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 s - in com.sample.app.PerformanceTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0
You can
download complete working application from this link.
https://github.com/harikrishna553/junit5/tree/master/junit5-tag-filtering
Previous
Next
Home