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

相关文章

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

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

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

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

Python中列表的高级索引技巧分享

《Python中列表的高级索引技巧分享》列表是Python中最常用的数据结构之一,它允许你存储多个元素,并且可以通过索引来访问这些元素,本文将带你深入了解Python列表的高级索引技巧,希望对... 目录1.基本索引2.切片3.负数索引切片4.步长5.多维列表6.列表解析7.切片赋值8.删除元素9.反转列表

正则表达式高级应用与性能优化记录

《正则表达式高级应用与性能优化记录》本文介绍了正则表达式的高级应用和性能优化技巧,包括文本拆分、合并、XML/HTML解析、数据分析、以及性能优化方法,通过这些技巧,可以更高效地利用正则表达式进行复杂... 目录第6章:正则表达式的高级应用6.1 模式匹配与文本处理6.1.1 文本拆分6.1.2 文本合并6

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin

数论入门整理(updating)

一、gcd lcm 基础中的基础,一般用来处理计算第一步什么的,分数化简之类。 LL gcd(LL a, LL b) { return b ? gcd(b, a % b) : a; } <pre name="code" class="cpp">LL lcm(LL a, LL b){LL c = gcd(a, b);return a / c * b;} 例题:

Java 创建图形用户界面(GUI)入门指南(Swing库 JFrame 类)概述

概述 基本概念 Java Swing 的架构 Java Swing 是一个为 Java 设计的 GUI 工具包,是 JAVA 基础类的一部分,基于 Java AWT 构建,提供了一系列轻量级、可定制的图形用户界面(GUI)组件。 与 AWT 相比,Swing 提供了许多比 AWT 更好的屏幕显示元素,更加灵活和可定制,具有更好的跨平台性能。 组件和容器 Java Swing 提供了许多

【IPV6从入门到起飞】5-1 IPV6+Home Assistant(搭建基本环境)

【IPV6从入门到起飞】5-1 IPV6+Home Assistant #搭建基本环境 1 背景2 docker下载 hass3 创建容器4 浏览器访问 hass5 手机APP远程访问hass6 更多玩法 1 背景 既然电脑可以IPV6入站,手机流量可以访问IPV6网络的服务,为什么不在电脑搭建Home Assistant(hass),来控制你的设备呢?@智能家居 @万物互联

系统架构师考试学习笔记第三篇——架构设计高级知识(20)通信系统架构设计理论与实践

本章知识考点:         第20课时主要学习通信系统架构设计的理论和工作中的实践。根据新版考试大纲,本课时知识点会涉及案例分析题(25分),而在历年考试中,案例题对该部分内容的考查并不多,虽在综合知识选择题目中经常考查,但分值也不高。本课时内容侧重于对知识点的记忆和理解,按照以往的出题规律,通信系统架构设计基础知识点多来源于教材内的基础网络设备、网络架构和教材外最新时事热点技术。本课时知识

poj 2104 and hdu 2665 划分树模板入门题

题意: 给一个数组n(1e5)个数,给一个范围(fr, to, k),求这个范围中第k大的数。 解析: 划分树入门。 bing神的模板。 坑爹的地方是把-l 看成了-1........ 一直re。 代码: poj 2104: #include <iostream>#include <cstdio>#include <cstdlib>#include <al