当前位置:网站首页>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方法场景经典测试的案例,敬请期待!。
边栏推荐
- 定了!2022京东全球科技探索者大会之京东云峰会7月13日北京见
- Addition without addition, subtraction, multiplication and division
- 392. judgment subsequence
- 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.1 rendering pipeline
- 389. 找不同
- Sort linked list (merge sort)
- 206.反转链表
- 168. Excel表列名称
- 使用selenium自动化测试工具爬取高考相关院校专业招生分数线及排名情况
猜你喜欢
![Online public network security case nanny level tutorial [reaching out for Party welfare]](/img/66/d9c848a7888e547b7cb28d84aabc24.png)
Online public network security case nanny level tutorial [reaching out for Party welfare]

DO280管理应用部署--RC

Appium fundamentals of automated testing - basic principles of appium

Edge浏览器的小技巧:Enter+Ctrl可以自动将地址栏转换为网址

How keil displays Chinese annotations (simple with pictures)

The programmer's girlfriend gave me a fatigue driving test

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

The method to measure the similarity of two vectors: cosine similarity, pytorch calculate cosine similarity: torch nn. CosineSimilarity(dim=1, eps=1e-08)

“目标检测“+“视觉理解“实现对输入图像的理解

程序员女友给我做了一个疲劳驾驶检测
随机推荐
陈宇(Aqua)-安全-&gt;云安全-&gt;多云安全
318. 最大单词长度乘积
674. longest continuous increasing sequence force buckle JS
互联网行业最佳产品开发流程 推荐!
快速筛选打卡时间日期等数据:EXCEL筛选查找某一时间点是否在某一时间段内
166. 分数到小数
30. 串联所有单词的子串
[ta - Frost Wolf May - 100 people plan] 2.3 Introduction aux fonctions communes
Download and installation configuration of cygwin
Analysis and case of pageobject mode
【快捷键】
NFT:使用 EIP-2981 开启 NFT 版税之旅
Addition without addition, subtraction, multiplication and division
205. 同构字符串
[deep learning] activation function (sigmoid, etc.), forward propagation, back propagation and gradient optimization; optimizer. zero_ grad(), loss. backward(), optimizer. Function and principle of st
[TA frost wolf \u may- hundred people plan] 1.2.1 vector basis
Random seed torch in deep learning manual_ seed(number)、torch. cuda. manual_ seed(number)
【EI检索】2022年第六届材料工程与先进制造技术国际会议(MEAMT 2022)重要信息会议网址:www.meamt.org会议时间:2022年9月23-25日召开地点:中国南京截稿时间:2
【JPCS出版】2022年第三届控制理论与应用国际会议(ICoCTA 2022)
[TA frost wolf \u may- hundred people plan] 2.3 introduction to common functions