Unit Test and Integration Test
Unit Test and Integration Test
Background
It is the first time that I try to write an article in English.
In the past, I didn’t write test code. Just thinking QA is responsible for testing.
As a developer, I don’t need to care about tests.
Although I know tests are essential, I can’t be aware of their importance.
After I joined my current team, it is required for developers to write tests.
So I have to write tests code.
After 8 months, I realize that tests are critical. I need to enforce my ability of testing.
therefore, I recently read a book Vladimir Khorikov - Unit Testing Principles Practices and Patterns
.
Learned a lot of unit test and integration best practice and code design.
So I want to share some knowledge with you.
Test
There are two kinds of tests.
- Unit Test
- Integration Test
- end to end test
Test Coverage
Do we need a 100% test coverage?
No. We don’t.
For trivial code, we can ignore that. Because they aren’t worth it.
We should focus on our business logic.
If a metric shows that there’s too little coverage in your code base—say, only 10%—
that’s a good indication that you are not testing enough.
But the reverse isn’t true:
even 100% coverage isn’t a guarantee that you have a good-quality test suite. A test
suite that provides high coverage can still be of poor quality.
Unit Test
The goal of unit test is to enable sustainable project growth.
Just as all tests are not created equal, not all parts of your code base are worth the
same attention in terms of unit testing.
Unit test should test a unit of behavior rather than code.
The four pillars of a good unit test
- Protection against regressions
- Resistance to refactoring
- Fast feedback
- Maintainability
Protection against regressions
The more features you develop, the more chances there are that you’ll break one of those features with a new release.
Sometimes We don’t awake that the new release will break one existing feature. Even with QA regression test, it also happens many times.
If we have a good automation test, can reduce these situations.
So good tests should protect against regressions.
To maximize the metric of protection against regressions, the test needs to aim at exercising as much code as possible.
Resistance to refactoring
Resistance to refactoring — the degree to which a test can sustain a refactoring of the underlying application code without turning red (failing).
This attribute can give us confidence to refactor.
How can we do?
We shouldn’t test the details of code. We should test the observable behavior.
Aim at the end result instead of implementation details
There is a example:
# we shouldn't care what's detail that we get from the data
test "get user by id" doassert "select * from users where id = 1" == User.get(1).to_sql
end# we just need to verify the data
test "get user by id" doassert User.get(1).name == "Steven"assert User.get(1).id == 1
end
Fast feedback
Fast feedback brings a excellent experience.
Picture the situation, we run a test, it takes 1 minute to show you result.
I can’t stand it.
Our project has hundreds of thousands of code. Run slowly and waste my time to wait for result.
How to run faster?
With less communications of out of process.
Async and parallel execution.
Maintainability
Using plain English as test titles.
Simple phrases in plain English do a much better job: they are more expressive
and don’t box you in a rigid naming structure. With simple phrases, you can describe
the system behavior in a way that’s meaningful to a customer or a domain expert.
how to write code to easy test
Split business logic and out of process communications (side effects).
def check doif User.admin? do:errorelse:ok end
enddef check_with_side_effect doif User.admin? do# side effectAuditLog.record():errorelse:ok end
end
With side effects, it is hard to compose code and hard to test.
Pure function is easiest to test.
Integration Test
Using integration tests to verify the behavior of the system as a whole.
Mock
Integration test verifies database, third-party api, mq and so on.
All out-of-process dependencies fall into two categories:
- Managed dependencies (out-of-process dependencies you have full control over)—These
dependencies are only accessible through your application; interactions with
them aren’t visible to the external world. A typical example is a database. External systems normally don’t access your database directly; they do that through
the API your application provides. - Unmanaged dependencies (out-of-process dependencies you don’t have full control over)—
Interactions with such dependencies are observable externally. Examples include
an SMTP server and a message bus: both produce side effects visible to other
applications.
For those out-of-process dependencies, we should mock unmanaged dependencies.
Roles of Test
- Trivial code: we shouldn’t test, it isn’t worth it.
- Domain model , algorithms: Unit test carefully test
- Controllers: Integration test, but doesn’t need to test all situation. Happy path and edge cases are enough.
- Overcomplicated code: We should reduce these code. Split it.
Test Pyramid
Recap
It simply summary Unit Testing.
Talk is cheap, we should write more code.
相关文章:
![](https://img-blog.csdnimg.cn/456fbaa295e14dfeb024b7a3ba897db7.png)
Unit Test and Integration Test
Unit Test and Integration Test Background It is the first time that I try to write an article in English. In the past, I didn’t write test code. Just thinking QA is responsible for testing. As a developer, I don’t need to care about tests. Although I …...
![](https://img-blog.csdnimg.cn/548f2e576c52434fb738812a0406fb99.png)
2022年全国职业院校技能大赛(中职组)网络安全竞赛试题(3)
目录 模块A 基础设施设置与安全加固 (本模块20分) 一、项目和任务描述: 假定你是某企业的网络安全工程师,对于企业的服务器系统,根据任务要求确保各服务正常运行,并通过综合运用用户安全管理与密码策略、…...
![](https://img-blog.csdnimg.cn/img_convert/49690a20f3de0bf376a465c8b392f85a.jpeg)
智慧城市应急指挥中心数字化及城市驾驶舱建设方案
目 录 第一章 项目概述 1.1 项目背景 1.2 项目范围 第二章 建设内容 2.1 三维可视化平台 2.1.1 多源数据接入 2.1.2 可视化编排 2.1.3 三维可视化编辑 2.1.4 空间数据可视化 2.1.5 集成框架支持 2.2 可视化场景定制开发 2.2.1 城市驾驶总舱 2.2.2 城市安全分舱 2.…...
![](https://img-blog.csdnimg.cn/img_convert/506773c22b5cecfa33f72f4d4097840e.png)
HSCSEC 2023 个人练习
😋 大家好,我是YAy_17,是一枚爱好网安的小白。本人水平有限,欢迎各位大佬指点,欢迎关注😁,一起学习 💗 ,一起进步 ⭐ 。⭐ 此后如竟没有炬火,我便是唯一的光。…...
![](https://img-blog.csdnimg.cn/ef4c3a858d1d42caaa4248b8d9f4f187.png)
Android 基础知识4-2.7 RelativeLayout(相对布局)
一、RelativeLayout的概述 RelativeLayout(相对布局)是一种根据父容器和兄弟控件作为参照来确定控件位置的布局方式。在很多时候,线性布局还不能满足我们的需求,比如,我们在一行(列)上显示多个控…...
![](https://img-blog.csdnimg.cn/img_convert/677306a2a454a2f5ced9e63209041857.png)
关于云计算,我们问了ChatGPT 10个问题
ChatGPT懂云计算吗?前些天,我们问了ChatGPT(非Plus收费版)一些问题。1. 什么是云计算?2. 云计算行业的护城河是什么?3. 什么是云原生?4. 微软Azure与亚马逊AWS的主要区别是什么?5. 为…...
![](https://www.ngui.cc/images/no-images.jpg)
Netty学习笔记1
Netty学习笔记(一) 在的互联网环境下,分布式系统大行其道,而分布式系统的根基在于网络编程,而 Netty 恰恰是 Java 领域网络编程的王者。如果要致力于开发高性能的服务器程序、高性能的客户端程序,必须掌握…...
![](https://www.ngui.cc/images/no-images.jpg)
RISK-V品牌的中国化历程(中)
目录 1.技术优势 出道即巅峰 2.生态布道 品牌根植中国 3.应用场景 加速品牌的商业化运作 生态布道 品牌根植中国 2015年成立非盈利组织RISC-V基金会,目前已吸引全球28个国家327家会员,包括英伟达、联发科、苹果、特斯拉、谷歌、高通、IBM、三星、麻省理…...
![](https://img-blog.csdnimg.cn/49520c6d42644988930a2419a8c4664d.png#pic_center)
2023.02.19 学习周报
文章目录摘要文献阅读1.题目2.摘要3.介绍4.本文贡献5.方法5.1 Local Representation Learning5.2 Global Representation Learning5.3 Item Similarity Gating6.实验6.1 数据集6.2 结果7.结论深度学习1.对偶问题1.1 拉格朗日乘数法1.2 强对偶性2.SVM优化3.软间隔3.1 解决问题3.…...
![](https://img-blog.csdnimg.cn/20190602114730587.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MzYxMDY5OA==,size_16,color_FFFFFF,t_70)
枚举类的使用方法
一、理解枚举类型 枚举类型是Java 5中新增特性的一部分,它是一种特殊的数据类型,之所以特殊是因为它既是一种类(class)类型却又比类类型多了些特殊的约束,但是这些约束的存在也造就了枚举类型的简洁性、安全性以及便捷性。下面先来看看如何写…...
![](https://img-blog.csdnimg.cn/7da65dfd059d4f13b61d0f76a085d078.png)
.NET3.5安装步骤及相关问题。
.NET3.5全称 Microsoft.NETFramework3.5 最新版本-.NET4.8 第一步打开控制面板 windows系统打开控制面板 选择程序 选择.NET3.5安装。 可能会出现问题。 解决方案: 报错代码80240438的常用解决办法: 方法一:检测windows update servic…...
![](https://img-blog.csdnimg.cn/9c23891469734777bb4bd278d097739c.png)
联想M7268激光打印机开机红绿灯双闪报错不打印
故障现象: 一台联想M7268激光打印机开机后电源键、复印键一起双闪,电源键闪红灯、复印键闪绿灯; 检测维修: 根据闪灯故障判断如果无卡纸异常情况下可能是激光器故障,因为以前曾经维修过一台一模一样的机器故障基本相同,先打开机器吧,把硒鼓拿出来先看看有没有卡纸,进纸…...
![](https://www.ngui.cc/images/no-images.jpg)
产品经理知识体系:7.web和app产品需求设计
web和app产品需求设计 思考 笔记 web产品设计 一、交互设计 1.以用户为中心的设计:功能、体验、用户; 将产品功能转化成用户的体验,功能和体验的结合。 2.交互设计模式 交互逻辑 信息结构 信息内容 界面结构 导航设计 二、视觉设计 元素的…...
![](https://www.ngui.cc/images/no-images.jpg)
强化学习概述
一、Modelfree 和 Modelbased Modelfree:不需要理解环境 Modelbased:需要理解环境,并且为环境建立模型 Model-free 中, 机器人只能按部就班, 一步一步等待真实世界的反馈, 再根据反馈采取下一步行动. 而 model-based, 他能通过想象来预判断接…...
![](https://img-blog.csdnimg.cn/0048b8399d4b47478591b768bc265078.png)
NO.1嵌入式入门笔记:常用命令记录
一、前言 Linux文件目录: Linux Shell: 它负责接收用户的输入,根据用户的输入找到其它程序并运行。比如我们输入“ls”并回车时,shell 程序找到“ls”程序并运行,把结果打印出来。Shell有多种实现,我们常用…...
![](https://www.ngui.cc/images/no-images.jpg)
Shell编程
typora-copy-images-to: pictures typora-root-url: pictures 文章目录typora-copy-images-to: pictures typora-root-url: pictures本节课程目标语法和选项语法和选项3. sort工具语法和选项5.tee工具6.diff工具语法和选项7. paste工具8. tr工具语法和选项小试牛刀二、bash的特…...
![](https://img-blog.csdnimg.cn/897a87036a1a4c0b9f6ff502095f7052.png)
网络模型OSI
网络模型OSI定义模型分布数据封装、解封过程数据链路层1.LLC逻辑链路控制子层(Logic Link Control Sub Layer)2.MAC媒介访问控制子层(Medium Acess Control Sub Layer)CSMA/CARST-CST原理OSI定义 OSI:Open Systems Interconnection Reference Model,开放…...
![](https://img-blog.csdnimg.cn/83e4d47ccbd34909b60aba1aa2fa374a.png)
RT-Thread初识学习-01
1. RT-Thread 简介 1.1 RT-Thread 是什么 据不完全统计,世界有成千上万个 RTOS(Real-time operating system,实时操作系统),RT-Thread 就是其中一个优秀的作品。 RT-Thread 内核的第一个版本是熊谱翔先生在 2006 年…...
![](https://img-blog.csdnimg.cn/7b367f235d1d442ba64b73abfb767e18.png#pic_center)
二阶段提交事务的实现和缺点
背景 说起分布式事务,我们最绕不开的一个话题就是该不该使用分布式事务,而要理解为什么做出使用与否的决定,就必须要提到分布式事务中的最经典的实现:两阶段提交事务,本文我们就简答介绍下这个两阶段提交事务以及它的优缺点 技术…...
![](https://img-blog.csdnimg.cn/8e4ae11075024c19b119cbf21f7113ea.png)
定点数的表示和运算
文章目录真值(有正负号)和机器数(0正1负)原码整数小数补码负数的补数正数的补数[y]~补~ > [-y]~补~反码小结移码移位运算加减法运算溢出判断真值(有正负号)和机器数(0正1负) 无符…...
![](https://img-blog.csdnimg.cn/765b18b290c24d088a442e01f0da2d0a.png#pic_center)
java虚拟机内存分布
java虚拟机内存分布 Java虚拟机在执行java程序的过程中会把它所管理的内存划分为如下若干个不同的数据区域。 1.程序计数器 程序计数器是线程私有的,它占用的空间相对较小,用来记录当前线程字节码执行到哪一步。字节码解释器通过改变这个计数器的值来获…...
![](https://img-blog.csdnimg.cn/img_convert/8b5118c14c000da6395854d015821d72.png)
最完整的小红书带货笔记——垂直模式
最完整的小红书带货笔记——垂直模式,小红书直播复盘怎么做?#直播带货笔记 第1篇,带你解锁直播复盘5大要点! #小红书店铺#小红书运营 小红书怎么发带货笔记? 做小红书带货的同学注意了,我们带货一定要发笔…...
![](https://img-blog.csdnimg.cn/11cc9714dc7f43b4864d3495fd5823c6.png)
SpringBoot实战——个人博客项目
目录 一、项目简介 二、项目整体架构 数据库模块 后端模块 前端模块 三、项目具体展示 四、项目的具体实现 1、一些准备工作 🍎数据库、数据表的创建 🍎设置数据库和MyBatis的配置 🍎将前端项目引入到当前项目中 2、登录注册模块 &…...
![](https://img-blog.csdnimg.cn/4ca79dc93b774435af6bedf66a7b7b0c.png)
浅谈Spring中事务管理器
由于事务部分代码在设计上整体比较简单,我自己觉得它在设计上没有什么特别让我眼前一亮的东西,所以下文更多的是侧重执行流程,能理解事务管理器等一众概念以及相关的变量含义,真正遇到Bug会调试,知道在什么地方打断点就…...
![](https://img-blog.csdnimg.cn/d1481332314d490e8895e410b0456b86.png)
Python练习系统
用python给自己做个练习系统刷题吧! #免费源码在文末公众号哈# 选择题 def xuanze():global flag2if flag21:def insert():numvar1.get()questionvar2.get()choicevar3.get()answervar4.get()with open(d:\\练习系统\\练习三3.1.pickle,rb) as file:lst1pickle.lo…...
![](https://img-blog.csdnimg.cn/c9f379d2622f4d0884323026dae256bf.png)
Transformer学习笔记1
Transformer学习笔记1 翻译中,每个词翻译时更看重哪个原词? ## 注意力机制的一般性原理 典型的注意力机制 hard one-hot形式,但是太过专一 soft 都分布,但是太过泛滥 local attention 自注意力机制:self-atte…...
![](https://img-blog.csdnimg.cn/7526463afb1343beb3c0eb5bbca5ffb0.webp)
软件测试简历个人技能和项目经验怎么写?(附项目资料)
目录 前言 个人技能 项目实战经验 项目名称:苏州银行项目(webapp) 项目描述: 项目名称:中国平安项目(webapp) 项目描述: 项目名称:苏宁易购项目(webapp&a…...
![](https://img-blog.csdnimg.cn/57305991ed814108be8c34fee6f7221d.png)
C语言运算符优先级和结合性一览表
所谓优先级就是当一个表达式中有多个运算符时,先计算谁,后计算谁。 运算符的优先级从高到低大致是:单目运算符、算术运算符、关系运算符、逻辑运算符、条件运算符、赋值运算符()和逗号运算符 简单记就是:! > 算术运…...
![](https://www.ngui.cc/images/no-images.jpg)
Java8介绍
Java 8 Java 8 把函数式编程里的一些思想融入到 Java 的语法中,让我们可以用更少的时间写出高效的代码。 Java 8的优势: 代码行更少Lambda表达式Stream API便于并行减少空指针JVM优化兼容老版本 Lambda Lambda表达式是一段可以传递的代码࿰…...
![](https://www.ngui.cc/images/no-images.jpg)
Java 基准测试
Java 基准测试maven依赖简单使用执行多个函数BenchmarkMode(Mode.AverageTime) 运行模式OutputTimeUnit(TimeUnit.NANOSECONDS) 报告结果时间单位Warmup(iterations 5,time 1,timeUnit TimeUnit.SECONDS) 预热参数Measurement(iterations 5,time 1,timeUnit TimeUnit.SEC…...
![](https://img-blog.csdnimg.cn/img_convert/160aa5f671410fc330ef22e661d61a94.png)
门户网站建设注意事项/国内ip地址 免费
一、背景 公司的项目前段时间发版上线后,测试反馈用户的批量删除功能报错。正常情况下看起来应该是个小BUG,可怪就怪在上个版本正常,且此次发版未涉及用户功能的改动。因为这个看似小BUG我了解到不少未知的东西,在这里和你们分享下…...
![](https://images0.cnblogs.com/i/587057/201404/190025051825721.png)
公司网站管理制度/百度学术论文查重官网入口
打算要写一个公开课网站,缺少数据,就决定去网易公开课去抓取一些数据。 前一阵子看过一段时间的Node.js,而且Node.js也比较适合做这个事情,就打算用Node.js去抓取数据。 关键是抓取到网页之后如何获取到想要的数据呢?然…...
![](https://img-blog.csdnimg.cn/img_convert/62e2ea2099874f0f5944d7f8996bd78a.png)
临汾市住房城乡建设局网站/浙江网站建设营销
Docker数据管理在容器中管理数据主要有两种方式:数据卷(Volumes)挂载主机目录(Bind mounts)数据卷数据卷是一个可供一个或多个容器使用的特殊目录,它绕过UFS,可以提供很多有用的特性:数据卷可以在容器之间共享和重用对数据卷的修改会立马生效…...
![](https://img-blog.csdnimg.cn/img_convert/4d91c43bfc72ca913299809b07b4968f.gif)
做擦边球网站赚钱么/在线h5免费制作网站
计算机应用基础(Windows XPOffice 2003)、素材和习题答案-王向慧 计算机应用基础 (447页)本资源提供全文预览,点击全文预览即可全文预览,如果喜欢文档就下载吧,查找使用更方便哦!9.90 积分第1章 上页 下页 目录 退出 目 目 录 录 第1章 计算机…...
![](/images/no-images.jpg)
怎么免费下wordpress/国外网站seo免费
vue弹窗加校验点击添加专区的按钮出现添加专区的弹窗根据elementui里面的dialog和form表单写的<div slot"setting"><el-button type"primary" click"addSub true">添加专区</el-button> </div>addSub是控制弹窗的显示隐…...
![](/images/no-images.jpg)
大学生做网站怎么赚钱/杭州千锋教育地址
作者很直男,兄弟们直接复制代码看效果吧!!! 一些小细节,大家可以自己动手修改。 <!DOCTYPE html> <html lang"en"><head><meta charset"UTF-8"><meta http-equiv"…...