当前位置:网站首页>Spock单元测试框架介绍及在美团优选的实践___第一章
Spock单元测试框架介绍及在美团优选的实践___第一章
2022-07-01 03:51:00 【灰太狼yyds】
Spock单元测试框架介绍及在美团优选的实践
文章目录
前言
什么是单元测试,其实单元测试是在我们开发完功能之后,对于业务逻辑用到了第三方的调用,或者对数据库的操作等动作不能够支持的情况下,对业务功能进行以方法为颗粒度进行测试业务逻辑是否与预期的效果数据相等。在很多的一线互联网公司研发人员开发完功能之后都要进行单元测试的,保证行覆盖率、分支覆盖率符合标注之后,才可以上线,以减少线上事故发生。
提示:以下是本篇文章正文内容,下面案例可供参考
一、什么是Spock单元测试框架?
帮助大家提高开发测试效率。
Spock是什么?和JUnit、jMock有什么区别?
Spock是国外一款优秀的测试框架,基于BDD(行为驱动开发) 思想,功能强大,能够让我们的单元测试代码规范化,结构层次清晰。Spock结合Groovy动态语言的特点以及自身提供的各种标签
种通用、简单、结构化的描述语言,让编写测试代码更加高效和简洁,
官方的介绍如下:https://spockframework.org
二、使用步骤
1.引入maven依赖
<!-- spock单元测试框架 -->
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>2.0-groovy-3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
<version>2.4.15</version>
</dependency>
2.创建GoodsService订单业务类进行单测
说明:
creativeManagementTunnel.queryCreateUser()是查询数据库订单用户,但是数据库表还没建不支持查询,代码会走这段逻辑但是mock之后不会触发数据库操作。
orgService.batchQueryUserInfoByMis()是查询数据库订单用户详细信息,但是数据库表还没建不支持查询,代码会走这段逻辑但是mock之后不会触发数据库操作。
3.创建Groovy.class类
new—>Groovy Class类
4.创建GoodsServiceSpec.java
创建GoodsServiceSpec类对,queryUserInfo()方法进行单元测试,命名严格要求:要测的类名+Spec

说明:
1、创建类之后需要继承extends Specification类
2、对不支持业务逻辑的操作方法进行mock。比如:OrgService orgService = Mock()
3、创建业务类的实例,进行方法调用。比如: def goodsService = new GoodsService()
4、创建测试方法格式如下
/** * 订单用户查询单元测试 */
class GoodsServiceSpec extends Specification{
CreativeManagementTunnel creativeManagementTunnel = Mock()
OrgService orgService = Mock()
def goodsService = new GoodsService(
creativeManagementTunnel: creativeManagementTunnel,
orgService: orgService,
)
def "createCampaignOpData success"() {
given:"准备数据"
def user = "小新新"
def user1 = "三少爷"
def userList = [new Employee("mis": "xiaoxinxin", "name": "小新新", "userId": "001"),
new Employee("mis": "sanshaoye", "name": "三少爷", "userId": "002"),];
//代表查全部
def userid = 1
and:"mock方法"
//[] 含义是代表一个list集合 (_) 表示代表方法入参任意类型
creativeManagementTunnel.queryCreateUser(_)>>[user,user1]
orgService.batchQueryUserInfoByMis(_)>>userList
//when 标签代表要测试方法的入口,只能又一个方法
when:"测试方法"
def response = goodsService.queryUserInfo(userid)
//then 对结果进行验证
then:"校验结果"
response.size() == 2
response.get(0).getUserId() == "001"
response.get(0).getName() == "小新新"
response.get(0).getMis() == "xiaoxinxin"
response.get(1).getUserId() == "002"
response.get(1).getName() == "三少爷"
response.get(1).getMis() == "sanshaoye"
}
}
实际效果与预期效果一致,表示测试通过。
实际效果与预期效果不一致,表示测试失败
5.groowy简单语法
where以标准格式开使提供集合
when调用指定方法函数
then: 故出断高裝达式
expect 期站的行为,shen-then的特点
giveni mock单测中指定mock数信
throwin:如果在when方法中地出现异常,则在这个子句中会捕玩到异常并返回
def setupo{}:每个训过运行前的启动方法
**der cleanupo{}**每个加武运行居的清理万法
**def setupSpec{}**个测试适祈前的启动方法
**def cleanupSpec{}**服后一介测试运行后的消理方法
总结
以上就是今天要讲的内容,本文仅仅简单介绍了spock的使用,而spock提供了大量能使我们快速便捷地处理数据的函数和方法,后续我将Spock对于异常、void方法,static方法场景经典测试的案例,敬请期待!。
边栏推荐
- Redis(七)优化建议
- 72. 编辑距离
- 盘点华为云GaussDB(for Redis)六大秒级能力
- 171. Excel 表列序号
- 访问阿里云存储的图片URL实现在网页直接预览略缩图而不直接下载
- Millet College wechat scanning code login process record and bug resolution
- Libevent Library Learning
- 166. 分数到小数
- Appium automation test foundation -- supplement: c/s architecture and b/s architecture description
- Download and installation configuration of cygwin
猜你喜欢

Download and installation configuration of cygwin

互联网行业最佳产品开发流程 推荐!

使用selenium自动化测试工具爬取高考相关院校专业招生分数线及排名情况
![[TA frost wolf \u may- hundred people plan] 2.4 traditional empirical lighting model](/img/05/85c004e4fbfc8d4984ac04ddb1190b.png)
[TA frost wolf \u may- hundred people plan] 2.4 traditional empirical lighting model

【TA-霜狼_may-《百人計劃》】1.2.1 向量基礎

【TA-霜狼_may-《百人计划》】2.4 传统经验光照模型

Usage of AfxMessageBox and MessageBox

Pytorch training deep learning network settings CUDA specified GPU visible

Review column - message queue
![4. [WebGIS practice] software operation chapter - data import and processing](/img/5a/b86e0538660f27c809cf429053a06c.png)
4. [WebGIS practice] software operation chapter - data import and processing
随机推荐
171. excel table column No
Access denied for user ‘ODBC‘@‘localhost‘ (using password: NO)
187. 重复的DNA序列
MFC window scroll bar usage
【TA-霜狼_may-《百人计划》】1.2.2 矩阵计算
4. [WebGIS practice] software operation chapter - data import and processing
有效的 @SuppressWarnings 警告名称
Redis(七)优化建议
在 C 中声明函数之前调用函数会发生什么?
[party benefits] jsonobject to string, leave blank
SEM of C language_ Tvariable type
How to display scrollbars on the right side of the background system and how to solve the problem of double scrollbars
LeetCode 1399. Count the maximum number of groups
Appium automation test foundation -- supplement: c/s architecture and b/s architecture description
[JPCs publication] the Third International Conference on control theory and application in 2022 (icocta 2022)
How keil displays Chinese annotations (simple with pictures)
Addition without addition, subtraction, multiplication and division
LetCode 1829. Maximum XOR value per query
30. 串联所有单词的子串
盘点华为云GaussDB(for Redis)六大秒级能力