Junit入门到掌握-10-JUnit高级-Categories

2024-06-11 10:48

本文主要是介绍Junit入门到掌握-10-JUnit高级-Categories,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前面介绍了测试套件TestSuite的概念和基本功能是把需要测试的测试类加进来管理,像一个容器一样。这样管理方式有些简单粗暴,为了达到更精细和灵活管理,我们这篇来学习一个Categories的概念,字面意思就分类。

 

1.Categories

这个分类和测试套件很像。

第一个特点,在TestSuite原来的注解变了,第二个我们需要在原来的TestClass里面是有Catogories注解。category既可以作用在方法,也可以作用在类上,我们习惯写在class 名称前一行,写在方法前一行不常用,一般一个class,我们做一个类型测试,一个业务场景类型。

 

2.Category demo练习

我们有两个测试类,我们把HelloJunitTest.java分类成A.class, 把PreteinTrackingTest.java分类成A.class, 只把前面文章写过的Timeout这个用例注释为B.class, A.class表示成功的用例集合,B.class表示失败的,A B都是接口。

 

搜先,我们需要提前创建一个Catogory的一个接口,用来后面分类使用,这个就是A.class

 

注意,这是一个接口,目前我们这个接口什么代码都不写。

package test;public interface GoodCategoryTest {}

然后创建一个BadCatoryTest接口

package test;public interface BadCategoryTest {}

然后开始使用Category注解在测试类中标注

package test;import static org.junit.Assert.assertEquals;import org.junit.Test;
import org.junit.experimental.categories.Category;@Category(GoodCategoryTest.class)
public class HelloJunitTest {@Testpublic void test() {assertEquals(5, "Hello".length());}
}

来看第二个测试类,由于我需要把里面一个方法标注成B.class,所以只能每个方法上都添加Category注解。

package test;
import static org.junit.Assert.*;import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;import com.anthony.protein.InvalidGoalException;
import com.anthony.protein.TrackingService;public class TrackingServiceTests {private TrackingService ts;@BeforeClasspublic static void before() {System.out.println("Before class, Onln Once");}@AfterClasspublic static void after() {System.out.println("After class, only once");}@Beforepublic void setup() {System.out.println("Before Method");ts = new TrackingService();}@Afterpublic void tearDown() {System.out.println("After Method");}@Test@Category(GoodCategoryTest.class)public void newTrackingServiceTotalIsZero() {assertEquals("Tracking service total was not zero", 0, ts.getTotal());}@Test@Category(GoodCategoryTest.class)public void whenAddingProteinTotalIsIncreaseByAmount() {ts.addProtein(10);assertEquals(10, ts.getTotal());}@Test@Category(GoodCategoryTest.class)public void whenRemovingProteinTotalRemainsZero() {ts.removeProtein(5);assertEquals(0, ts.getTotal());}@Test(expected=InvalidGoalException.class)@Category(GoodCategoryTest.class)public void testExceptionThrow() throws InvalidGoalException {ts.setGoal(-5);}@Test(timeout=20)@Category(BadCategoryTest.class)public void badTest() throws InvalidGoalException {for(int i=0; i < 1000000000; i++) {ts.setGoal(1);}
}}

 

这个时候TestSuite这样写

package test;import org.junit.experimental.categories.Categories;
import org.junit.experimental.categories.Categories.IncludeCategory;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;@RunWith(Categories.class)
@IncludeCategory(GoodCategoryTest.class)
@Suite.SuiteClasses({ HelloJunitTest.class, TrackingServiceTests.class // 接着写其他被测单元测试类
})public class ProteinTrackerSuite {}

执行下这个TestSuite文件

这种场景,我们还可以利用取反策略来跑用例,这里我们的场景是只执行Good的测试用例,取反就是除Bad之外的,我们需要利用注解@IncludeCategory相反的作用的,叫做@ExcludeCategory,就是除...之外,测试套件注解这么写,也是可以达到上面一样的运行结果。

package test;import org.junit.experimental.categories.Categories;
import org.junit.experimental.categories.Categories.ExcludeCategory;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;@RunWith(Categories.class)
@ExcludeCategory(BadCategoryTest.class)
@Suite.SuiteClasses({ HelloJunitTest.class, TrackingServiceTests.class // 接着写其他被测单元测试类
})public class ProteinTrackerSuite {}

 

也就是只执行类GoodCategoryTest这个分类的用例,我把测试套件改成只执行BadCategoryTest.class

package test;import org.junit.experimental.categories.Categories;
import org.junit.experimental.categories.Categories.IncludeCategory;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;@RunWith(Categories.class)
@IncludeCategory(BadCategoryTest.class)
@Suite.SuiteClasses({ HelloJunitTest.class, TrackingServiceTests.class // 接着写其他被测单元测试类
})public class ProteinTrackerSuite {}

运行下

只执行这个标注为BadCategoryTest.class的用例,这个是期待的效果。

 

这篇关于Junit入门到掌握-10-JUnit高级-Categories的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1050957

相关文章

Python中模块graphviz使用入门

《Python中模块graphviz使用入门》graphviz是一个用于创建和操作图形的Python库,本文主要介绍了Python中模块graphviz使用入门,具有一定的参考价值,感兴趣的可以了解一... 目录1.安装2. 基本用法2.1 输出图像格式2.2 图像style设置2.3 属性2.4 子图和聚

Spring Boot 整合 SSE的高级实践(Server-Sent Events)

《SpringBoot整合SSE的高级实践(Server-SentEvents)》SSE(Server-SentEvents)是一种基于HTTP协议的单向通信机制,允许服务器向浏览器持续发送实... 目录1、简述2、Spring Boot 中的SSE实现2.1 添加依赖2.2 实现后端接口2.3 配置超时时

mysql中的group by高级用法

《mysql中的groupby高级用法》MySQL中的GROUPBY是数据聚合分析的核心功能,主要用于将结果集按指定列分组,并结合聚合函数进行统计计算,下面给大家介绍mysql中的groupby用法... 目录一、基本语法与核心功能二、基础用法示例1. 单列分组统计2. 多列组合分组3. 与WHERE结合使

MySQL高级查询之JOIN、子查询、窗口函数实际案例

《MySQL高级查询之JOIN、子查询、窗口函数实际案例》:本文主要介绍MySQL高级查询之JOIN、子查询、窗口函数实际案例的相关资料,JOIN用于多表关联查询,子查询用于数据筛选和过滤,窗口函... 目录前言1. JOIN(连接查询)1.1 内连接(INNER JOIN)1.2 左连接(LEFT JOI

前端高级CSS用法示例详解

《前端高级CSS用法示例详解》在前端开发中,CSS(层叠样式表)不仅是用来控制网页的外观和布局,更是实现复杂交互和动态效果的关键技术之一,随着前端技术的不断发展,CSS的用法也日益丰富和高级,本文将深... 前端高级css用法在前端开发中,CSS(层叠样式表)不仅是用来控制网页的外观和布局,更是实现复杂交

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis

kotlin中的行为组件及高级用法

《kotlin中的行为组件及高级用法》Jetpack中的四大行为组件:WorkManager、DataBinding、Coroutines和Lifecycle,分别解决了后台任务调度、数据驱动UI、异... 目录WorkManager工作原理最佳实践Data Binding工作原理进阶技巧Coroutine

Python FastAPI入门安装使用

《PythonFastAPI入门安装使用》FastAPI是一个现代、快速的PythonWeb框架,用于构建API,它基于Python3.6+的类型提示特性,使得代码更加简洁且易于绶护,这篇文章主要介... 目录第一节:FastAPI入门一、FastAPI框架介绍什么是ASGI服务(WSGI)二、FastAP

深入解析Spring TransactionTemplate 高级用法(示例代码)

《深入解析SpringTransactionTemplate高级用法(示例代码)》TransactionTemplate是Spring框架中一个强大的工具,它允许开发者以编程方式控制事务,通过... 目录1. TransactionTemplate 的核心概念2. 核心接口和类3. TransactionT

轻松掌握python的dataclass让你的代码更简洁优雅

《轻松掌握python的dataclass让你的代码更简洁优雅》本文总结了几个我在使用Python的dataclass时常用的技巧,dataclass装饰器可以帮助我们简化数据类的定义过程,包括设置默... 目录1. 传统的类定义方式2. dataclass装饰器定义类2.1. 默认值2.2. 隐藏敏感信息