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

  1. Tag names cannot be left or right with Spaces. This will be done during teststrimProcessing;
  2. The label name cannot contain the following characters:(,),&,|,!,..

Two, the basic method of use

  1. Define a unit test class and add notes to the classtest-1, where two test methods are defined and labeled respectivelylevel-1andlevel-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
  1. Define another unit test class and add labels to the classtest-2, also define two test methods and add labelslevel-1andlevel-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
  1. 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
  1. In addition to using the Maven command to perform tag filtering tests, you can also configure tag filtering testssurefireThe plug-in performs tag filtering tests. (surefireIt’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

  1. 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
  1. will@TagAnnotations are replaced with custom annotations
@Test
@Level1
public void test1() {
    System.out.println("Test 1-1");
}
Copy the code
  1. And we can simplify it even further@TestAdd 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
  1. The test method only uses custom tag annotations.
@Level1
public void test1() {
    System.out.println("Test 1-1");
}
Copy the code