Suppose you have a large number of unit test cases and you don’t want them to be executed all at the same time during Maven build. You can simply achieve it via annotation @Category.
(1) Create empty class FastTests and SlowTests. (2) In your test case class, categorize your test method using @Category annotation:
(3) Append the following code to your pom.xml:
<profiles>
<profile>
<id>SlowTests</id>
<properties>
<testcase.groups>com.sap.SlowTests</testcase.groups>
</properties>
</profile>
<profile>
<id>FastTests</id>
<properties>
<testcase.groups>com.sap.FastTests</testcase.groups>
</properties>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.13</version>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>2.13</version>
</dependency>
</dependencies>
<configuration>
<groups>${testcase.groups}</groups>
</configuration>
</plugin>
</plugins>
</build>
(4)In my project, by default all 7 test methods will be executed during Maven build:
Suppose you only want to execute unit test belonging to category “SlowTests”, use the following command line:
Since now I only marked one method with annotation SlowTests, only one test method is executed:
If you would like to execute all unit tests EXCEPT @SlowTests, simply add another profile in pom.xml:
<profile>
<id>NonSlowTests</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludedGroups>com.sap.SlowTests</excludedGroups>
</configuration>
</plugin>
</plugins>
</build>
</profile>
Before test, in order to prove that Slow method is NOT really executed, I add a system.out.println in each method:
Use command line: mvn test -P NonSlowTests From console output, I can ensure that the method with @Category(SlowTests.class) is NOT executed at all.
要获取更多Jerry的原创文章,请关注公众号"汪子熙":
相关推荐
JUnit 类别 JUnit 4.9 引入了一个很好的注释来对您的测试进行分类。 而 TestNG 选择为使用字符串 JUnit 类别使用类。 这比字符串更灵活,这在使用多模块 maven 项目时可能会很痛苦(有关更多详细信息,请参阅 JUnit ...
1.junit 常用注解 @Before 初始化方法,每次测试方法调用前都执行一次。 @After 释放资源:每次测试方法调用后都执行一次 @Test 测试方法:在这里可以测试期望异常和超时时间 @ignore 忽略的测试方法 @BeforeClass ...
我们应该能够通过 JUnit @Category包含组 我们应该能够通过 JUnit @Category排除测试 Maven 故障安全插件 当必须创建或部署工件以进行测试时,将使用故障安全插件 故障安全插件具有默认文件模式,它寻找作为集成...
Junit是java中测试的必备工具,Junit_test这个程序是更好的实现对Junit的了解。帮助大家学习
介绍如何利用反射和注解去简单的模拟JUnit4单元测试的使用,之所以选择JUnit4是...需要注意的是这里并不是完全的模拟,只是简单实现了一下Runner类和JUnit注解相关的工作流程。所以本文的主要目是介绍反射和注解的使用
Junit java单元测试内部机制解析
junit-extension 背景 原生的Junit无法满足我们在自动化测试实践过程中的碰到一些需求,比如 ...以上三种注解和Junit build-in的注解@Test配合使用,示例如下 import org.junit.Test; import org.sdet.junit.extens
今天小编就为大家分享一篇关于Junit 5中@ParameterizedTest与@EnumSource结合使用,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
Junit中的基本注解(教学视频) Junit中的基本注解,是必须掌握的。 @BeforeClass – 表示在类中的任意public static void方法执行之前执行 @AfterClass – 表示在类中的任意public static void方法执行之后执行 ...
java自定义 注解 annotation、标签库tag、监听listener、junit简单测试代码
在JUnit中使用@Rule测试文件和目录Java开发Java经验技巧共3页.pdf.zip
1 Junit是什么 JUnit 是一个 Java 编程语言的单元测试框架。JUnit 在测试驱动的开发方面有很重要的发展,是起源于 JUnit 的一个统称为 xUnit 的单元测试框架之一。 2 单元测试概念 ...必须使注解@Test修饰
JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage,包含依赖包:junit-jupiter-5.5.1.jar,junit-jupiter-engine-5.5.1.jar,junit-jupiter-params-5.5.1.jar,junit-platform-launcher-1.5.1.jar,junit-...
JUnit4通过注解的方式来识别测试方法。目前支持的主要注解有(下列注解标示了单元测试的不同运行阶段执行的代码): @BeforeClass 全局只会执行一次,而且是第一个运行 @Before 在测试方法运行之前运行(每个测试...
org.junit.experimental.categories.Category.class org.junit.experimental.max.CouldNotReadCoreException.class org.junit.experimental.max.MaxCore.class org.junit.experimental.max.MaxHistory.class org....
junit4.1junit4.1junit4.1junit4.1junit4.1
使用 tk-mybatis 的 demo 测试了 Spring 的事务 包含 @Transactional 注解所有的功能测试。
最新junit4,完整压缩文件,支持注解
Junit是一个Java语言的单元测试框架,简单理解为可以用于取代java的main方法。Junit属于第三方工具,一般情况下需要导入jar包。...注解相当一种标记,是类的组成部分,可以给类携带一些额外的信息。