jck28-lucio-junit5测试套件

Suite套件

  • JUnit5通过Suite注解使用套件
  • 不需要再声明RunWith(JUnitPlatform.class)

pom.xml

对应依赖导入

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>11</java.version>
    <maven.compiler.version>3.3</maven.compiler.version>
    <maven.surefire.version>3.0.0-M5</maven.surefire.version>
    <junit.version>5.8.1</junit.version>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.junit</groupId>
            <artifactId>junit-bom</artifactId>
            <version>${junit.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>


    </dependencies>
</dependencyManagement>


<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-suite</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${maven.surefire.version}</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${maven.compiler.version}</version>
            <configuration>
                <encoding>UTF-8</encoding>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>
    </plugins>
</build>

套件Suite

  • 如果项目是从Junit4进行迁移,仍然需要RunWith(JUnitPlatform.class)
  • 如果是一个全新的JUnit5项目,直接使用Suite注解

套件@Suite

测试类准备

project
│ README.md


└───class1
│ │ OneTest.java
│ │ TwoTest.java

└───class2
│ OneTest.java
│ TwoTest.java
│ TreeTest.java
│ TestFour.java

测试类准备-class1

package com.ceshiren.class1;


import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

public class OneTest {

    @Test
    @Tag("SuiteTag")
    @Tag("SuiteTag1")
    void test1(){
        System.out.println("com.class5.packageA-----OneTest");

    }

}

package com.ceshiren.class1;


import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

public class TwoTest {
    @Test
    @Tag("SuiteTag")
    void test1(){
        System.out.println("com.class5.packageA-----TwoTest");

    }
}

测试类准备-class2

package com.ceshiren.class2;


import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

public class OneTest {

    @Test
    @Tag("SuiteTag")
    @Tag("SuiteTag1")
    void test1(){
        System.out.println("com.class5.packageB-----OneTest");

    }
}

package com.ceshiren.class2;


import org.junit.jupiter.api.Test;

public class TwoTest {

    @Test
    void test1(){
        System.out.println("com.class5.packageB-----TwoTest");

    }
}

## 测试类准备-class2
package com.ceshiren.class2;


import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

public class TreeTest {
    @Test
    @Tag("SuiteTag1")
    void test1(){
        System.out.println("com.class5.packageB-----TreeTest1");

    }
}

package com.ceshiren.class2;


import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

public class TestFour {

    @Test
    @Tag("SuiteTag1")
    void test1(){
        System.out.println("com.class5.packageB-----TestFour");

    }

    @Test
    void test2(){
        System.out.println("com.class5.packageB-----TestFour---test2");

    }
    @Test
    void test3(){
        System.out.println("com.class5.packageB-----TestFour---test3");

    }
    @Test
    void test4(){
        System.out.println("com.class5.packageB-----TestFour---test4");

    }
}

测试套件组合

执行单个测试类

  • @Suite+@SelectClasses
  • 过滤出需要执行的单个测试类
package com.ceshiren.testcases;


import com.ceshiren.class1.OneTest;
import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;
//测试套件基本注解
@Suite
//选择需要执行的测试类 参数:OneTest.class
@SelectClasses(OneTest.class )
public class RunSuite01Test {
}

执行多个测试类

  • @Suite+@SelectClasses
  • 过滤出需要执行的多个测试类
package com.ceshiren.testcases;

import com.ceshiren.class1.TwoTest;
import com.ceshiren.class2.OneTest;
import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;

@Suite
//选择需要执行的测试类 
//参数类型为集合:{OneTest.class,TwoTest.class}
@SelectClasses({
        OneTest.class,
        TwoTest.class
})
public class RunSuite02Test {
}

执行单个测试包

  • @Suite+@SelectPackages
  • 过滤出需要执行的单个测试包
package com.ceshiren.testcases;

import org.junit.platform.suite.api.SelectPackages;
import org.junit.platform.suite.api.Suite;

@Suite
//选择需要执行的测试包
//参数类型为String:com.ceshiren.class1
@SelectPackages("com.ceshiren.class1")
public class RunSuite03Test {
}

执行多个测试包

  • @Suite+@SelectPackages
  • 过滤出需要执行的多个测试包
package com.ceshiren.testcases;

import org.junit.platform.suite.api.SelectPackages;
import org.junit.platform.suite.api.Suite;

@Suite
//选择需要执行的测试包 
//参数类型为集合:{"com.ceshiren.class1","com.ceshiren.class2"}
//
@SelectPackages({
        "com.ceshiren.class1",
        "com.ceshiren.class2"
})
public class RunSuite04Test {
}

自定义Suite套件的显示名称

  • @Suite+@SuiteDisplayName+*
  • 自定义Suite套件的显示名称
package com.ceshiren.testcases;

import org.junit.platform.suite.api.SelectPackages;
import org.junit.platform.suite.api.Suite;
import org.junit.platform.suite.api.SuiteDisplayName;

@Suite
//对应测试套件显示的名称是Test Suite
@SuiteDisplayName("Test Suite")
@SelectPackages({
        "com.ceshiren.class1",
        "com.ceshiren.class2"
})
public class RunSuite05Test {
}

过滤需执行的测试包

  • @Suite+@SelectPackages+@IncludePackages
  • 过滤需执行的测试包
package com.ceshiren.testcases;

import org.junit.platform.suite.api.IncludePackages;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.platform.suite.api.Suite;
import org.junit.platform.suite.api.SuiteDisplayName;

@Suite
@SuiteDisplayName("Test Suite06")
@SelectPackages({
        "com.ceshiren"
})
//对应的满足SelectPackages的包名 
//参数类型 String :"com.ceshiren.class1"
@IncludePackages("com.ceshiren.class1")
public class RunSuite06Test {
}

过滤不需要执行的测试包

  • @Suite+@SelectPackages+@ExcludePackages
  • 过滤不需要执行的测试包
package com.ceshiren.testcases;

import org.junit.platform.suite.api.ExcludePackages;
import org.junit.platform.suite.api.SelectPackages;
import org.junit.platform.suite.api.Suite;
import org.junit.platform.suite.api.SuiteDisplayName;


@Suite
@SuiteDisplayName("Test Suite07")
@SelectPackages({
        "com.ceshiren.class1",
        "com.ceshiren.class2"
})
//满足SelectPackages下的包且不执行"com.ceshiren.class1"下的所有类
//参数是String : "com.ceshiren.class1"
@ExcludePackages("com.ceshiren.class1")
public class RunSuite07Test {
}

正则匹配Test

  • @Suite+@SelectPackages+@IncludeClassNamePatterns
  • 根据正则匹配,对应class执行
package com.ceshiren.testcases;

import org.junit.platform.suite.api.*;

@Suite
@SuiteDisplayName("Test Suite08")
@SelectPackages({
        "com"
})
//对应正则表达式匹配执行的相关class
//参数类型为集合:{"com.ceshiren.class2.*Test"}
@IncludeClassNamePatterns({
        "com.ceshiren.class2.*Test"
//        "com.ceshiren.class2.*"
})
public class RunSuite08Test {
}

正则匹配Test不被执行

  • @Suite+@SelectPackages+ @ExcludeClassNamePatterns
  • 根据正则匹配 对应class不被执行
package com.ceshiren.testcases;

import org.junit.platform.suite.api.*;

@Suite
@SuiteDisplayName("Test Suite09")
@SelectPackages({
        "com.ceshiren.class2"
})
//对应正则表达式匹配不被执行的相关class
//参数类型为集合:{"com.ceshiren.class2.*Test$"}
@ExcludeClassNamePatterns("com.ceshiren.class2.*Test$")
public class RunSuite09Test {
}

执行包内的标签

  • @Suite+@SelectPackages+@IncludeTags
  • 执行包内包含标签的方法或者类
package com.ceshiren.testcases;

import org.junit.platform.suite.api.*;

@Suite
@SuiteDisplayName("Test Suite10")
@SelectPackages({
        "com.ceshiren.class2"
})
//对应包含的标签
//参数是集合:{"SuiteTag1"}
@IncludeTags({
//        "SuiteTag"
        "SuiteTag1"
})
public class RunSuite10Test {
}

不执行包内标签的方法/类

  • @Suite+@SelectPackages+@ExcludeTags
  • 不执行包内标签的方法/类
package com.ceshiren.testcases;

import org.junit.platform.suite.api.*;

@Suite
@SuiteDisplayName("Test Suite11")
@SelectPackages({
        "com.ceshiren.class2"
})
//对应不执行包含的标签
//参数是集合:{"SuiteTag1"}
@ExcludeTags({
        "SuiteTag1"
})
public class RunSuite11Test {
}