当前位置:网站首页>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方法场景经典测试的案例,敬请期待!。
边栏推荐
- [TA frost wolf \u may- hundred people plan] 1.3 secret of texture
- Jenkins自动清理构建历史
- Access denied for user ‘ODBC‘@‘localhost‘ (using password: NO)
- Go learning --- unit test subtest
- 基于Unet的环路滤波
- Libevent Library Learning
- Unexpected token o in JSON at position 1 ,JSON解析问题
- 242. 有效的字母异位词
- Unexpected token o in JSON at position 1, JSON parsing problem
- [TA frost wolf \u may- hundred people plan] 2.4 traditional empirical lighting model
猜你喜欢

Web components series (VIII) -- custom component style settings

Network metering - application layer

Unexpected token o in JSON at position 1, JSON parsing problem

[TA frost wolf \u may - "hundred people plan"] 2.1 color space

静态库使用MFC和共享库使用MFC的区别

Pytorch training deep learning network settings CUDA specified GPU visible

Are you still wasting brain cells for self-study? This interview note is definitely the ceiling of station C
![[TA frost wolf \u may- hundred people plan] 1.3 secret of texture](/img/dc/ce2819258632cdcf2e130a4c05600e.png)
[TA frost wolf \u may- hundred people plan] 1.3 secret of texture

Jenkins自动清理构建历史

Error: plug ins declaring extensions or extension points must set the singleton directive to true
随机推荐
【TA-霜狼_may-《百人计划》】1.2.3 MVP矩阵运算
[TA frost wolf _may - "hundred people plan"] 1.4 introduction to PC mobile phone graphics API
165. 比较版本号
快速筛选打卡时间日期等数据:EXCEL筛选查找某一时间点是否在某一时间段内
[TA frost wolf \u may- hundred talents plan] 1.2.3 MVP matrix operation
Addition without addition, subtraction, multiplication and division
205. isomorphic string
Blueprism registration, download and install -rpa Chapter 1
Complete knapsack problem
Sort linked list (merge sort)
[JPCs publication] the Third International Conference on control theory and application in 2022 (icocta 2022)
MFC window scroll bar usage
Unexpected token o in JSON at position 1 ,JSON解析问题
You cannot right-click F12 to view the source code solution on the web page
Implement pow (x, n) function
[reach out to Party welfare] developer reload system sequence
241. 为运算表达式设计优先级
程序员女友给我做了一个疲劳驾驶检测
陈宇(Aqua)-安全-&gt;云安全-&gt;多云安全
【TA-霜狼_may-《百人计划》】1.1 渲染流水线