Spring Boot labels test classes or methods using the @tag annotation provided by JUnit5 to facilitate filtering by Tag when testing is executed.
First, label naming specification
- Tag names cannot be left or right with Spaces. This will be done during tests
trim
Processing; - The label name cannot contain the following characters:
(
,)
,&
,|
,!
,.
.
Two, the basic method of use
- Define a unit test class and add notes to the class
test-1
, where two test methods are defined and labeled respectivelylevel-1
andlevel-2
.
package com.example.demo; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; @Tag("test-1") public class UnitTest1 { @Test @Tag("level-1") public void test1() { System.out.println("Test 1-1"); } @Test @Tag("level-2") public void test2() { System.out.println("Test 1-2"); }}Copy the code
- Define another unit test class and add labels to the class
test-2
, also define two test methods and add labelslevel-1
andlevel-2
.
package com.example.demo; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; @Tag("test-2") public class UnitTest2 { @Test @Tag("level-1") public void test1() { System.out.println("Test 2-1"); } @Test @Tag("level-2") public void test2() { System.out.println("Test 2-2"); }}Copy the code
- Run the following Maven command to observe the test execution process.
mvn clean test -Dgroups=test-1
mvn clean test -Dgroups=test-2
mvn clean test -Dgroups=level-1
mvn clean test -Dgroups=level-2
Copy the code
- In addition to using the Maven command to perform tag filtering tests, you can also configure tag filtering tests
surefire
The plug-in performs tag filtering tests. (surefire
It’s a test engine.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<groups>level-1</groups>
<includes>*Test*.java</includes>
</configuration>
</plugin>
Copy the code
Tag expression
About the basic way of using the label above, each test can only specify a label, if you want to realize more complex filter tag will use label expression, namely with (&), or (|), not (!) Three operators concatenate labels.
Maven command format:
mvn clean test -Dgroups='test-1 & level-1'
Copy the code
Surefire configuration format:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<groups>test-1 | level-2</groups>
<includes>*Test*.java</includes>
</configuration>
</plugin>
Copy the code
Four, custom label annotation
- Custom tag annotations
package com.example.demo;
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;
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Tag("level-1")
public @interface Level1 {
}
Copy the code
- will
@Tag
Annotations are replaced with custom annotations
@Test
@Level1
public void test1() {
System.out.println("Test 1-1");
}
Copy the code
- And we can simplify it even further
@Test
Add custom tag annotations.
package com.example.demo;
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.Test;
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Test
@Tag("level-1")
public @interface Level1 {
}
Copy the code
- The test method only uses custom tag annotations.
@Level1
public void test1() {
System.out.println("Test 1-1");
}
Copy the code