最近在研究对应的suite套件,发现官网上有了对应的直接@Suite注解配合就可以使用套件,不需要再声明@RunWith(JUnitPlatform.class)
由于官网的解释不是很详细,在这里给大家写一篇比较详细的文章
- 1)创建 maven 工程 XUnit,pom.xml中添加Junit5的Suite相关依赖
pom.xml
UTF-8 11 3.3 3.0.0-M5 5.8.1 org.junit junit-bom${junit.version} pom import org.junit.jupiter junit-jupitertest org.junit.platform junit-platform-suitetest org.apache.maven.plugins maven-surefire-plugin${maven.surefire.version} org.apache.maven.plugins maven-compiler-plugin${maven.compiler.version} UTF-8 ${java.version}
下面开始介绍对应套件执行的相关注解,如果项目是从Junit4进行迁移,那还是需要@RunWith(JUnitPlatform.class);如果是一个全新的junit5项目,建议直接使用@Suite注解进行,下面对应的所有注解进行一个示例
@Suitepackage com.packageA; 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"); } }TwoTest
package com.packageA; 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"); } }com.packageB OneTest
package com.packageB; 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"); } }TwoTest
package com.packageB; import org.junit.jupiter.api.Test; public class TwoTest { @Test void test1(){ System.out.println("com.class5.packageB-----TwoTest"); } }TreeTest
package com.packageB; 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"); } }TestFour
package com.packageB; 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 class5.packageC; import com.packageA.OneTest; import org.junit.platform.suite.api.SelectClasses; import org.junit.platform.suite.api.Suite; @Suite @SelectClasses(OneTest.class ) public class RunSuite01Test { }过滤出需要执行的多个测试类
package class5.packageC; import com.packageA.OneTest; import com.packageB.TwoTest; import org.junit.platform.suite.api.SelectClasses; import org.junit.platform.suite.api.Suite; @Suite @SelectClasses({ OneTest.class, TwoTest.class }) public class RunSuite02Test { }@Suite+@SelectPackages 过滤出需要执行的单个测试包
package class5.packageC; import org.junit.platform.suite.api.SelectPackages; import org.junit.platform.suite.api.Suite; import org.junit.platform.suite.api.SuiteDisplayName; @Suite @SuiteDisplayName("Test Suite") @SelectPackages("com.packageA") public class RunSuite03Test { }过滤出需要执行的多个测试包
package class5.packageC; import org.junit.platform.suite.api.SelectPackages; import org.junit.platform.suite.api.Suite; import org.junit.platform.suite.api.SuiteDisplayName; @Suite @SuiteDisplayName("Test Suite04") @SelectPackages({ "com.packageA", "com.packageB" }) public class RunSuite04Test { }@Suite+@SuiteDisplayName+* 自定义Suite套件的显示名称
在上面对应用例中已经生效
@Suite+@SelectPackages+@IncludePackages 配合使用过滤出需要执行的测试包package class5.packageC; 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 Suite05") @SelectPackages({ "com" }) @IncludePackages("com.packageB") public class RunSuite05Test { }@Suite+@SelectPackages+@ExcludePackages 配合使用过滤出不需要执行的测试包
package class5.packageC; 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 Suite06") @SelectPackages({ "com" }) @ExcludePackages({ "com.packageB" }) public class RunSuite06Test { }@Suite+@SelectPackages+@IncludeClassNamePatterns 用正则匹配出来具体的class执行
package class5.packageC; import org.junit.platform.suite.api.IncludeClassNamePatterns; 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" }) @IncludeClassNamePatterns({ "com.packageB.*Test" // "com.packageB.*" }) public class RunSuite07Test { }@Suite+@SelectPackages+@IncludeClassNamePatterns 用正则匹配出来具体的class哪些不被执行
package class5.packageC; import org.junit.platform.suite.api.*; @Suite @SuiteDisplayName("Test Suite08") @SelectPackages({ "com" }) @ExcludeClassNamePatterns("com.packageB.*Test$") public class RunSuite08Test { }@Suite+@SelectPackages+@IncludeTags 运行指定的包下的指定标签的方法
package class5.packageC; import org.junit.platform.suite.api.IncludeTags; import org.junit.platform.suite.api.SelectPackages; import org.junit.platform.suite.api.Suite; import org.junit.platform.suite.api.SuiteDisplayName; @Suite @SuiteDisplayName("Test Suite Tag") @SelectPackages({ "com" }) @IncludeTags({ // "SuiteTag" "SuiteTag1" }) public class RunSuite09Test { }@Suite+@SelectPackages+@ExcludeTags 运行指定的包下并且不包含指定标签的方法
package class5.packageC; import org.junit.platform.suite.api.*; @Suite @SuiteDisplayName("Test Suite") @SelectPackages({ "com" }) @ExcludeTags({ "SuiteTag1" }) public class RunSuite10Test { }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)