Unit Test and Integration Test

news2024/9/21 22:35:16

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" do
  assert "select * from users where id = 1" == User.get(1).to_sql
end

# we just need to verify the data
test "get user by id" do
  assert 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 do
  if User.admin? do
    :error
  else
    :ok  
  end
end


def check_with_side_effect do
  if User.admin? do
    # side effect
    AuditLog.record()
    :error
  else
    :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.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/357708.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Robust Self-Augmentation for Named Entity Recognition with Meta Reweighting

摘要 近年来,自我增强成为在低资源场景下提升命名实体识别性能的研究热点。Token substitution and mixup (token替换和表征混合)是两种有效提升NER性能的自增强方法。明显,自增强方法得到的增强数据可能由潜在的噪声。先前的研究…

【Vue3源码】第四章 实现isReadonly和isReactive

【Vue3源码】第四章 实现isReadonly和isReactive 前言 上一章节我们实现readonly API,并且优化之前写的reactive API。这一章我们实现isReadonly和isReactive两个API。 1、实现isReactive 官网是这么介绍的:检查一个对象是否是由 reactive() 或 shallo…

WebView2 (Chromium) 右键菜单大全

在开发WebView2浏览器时,需要定制右键上下文菜单,但是微软的官方网站上没有详细的菜单,这里整理出来,以方便大家使用。以下是在win11上测试获取的,经过测试win7上稍有不同。一、网页上空白处右键 Target:Pa…

Mybatis Notes

文章目录1 Mybatis 介绍1.1 快速入门2 JDBC2.1 JDBC介绍2.3 JDBC问题分析2.4 Mybatis与JDBC技术对比3 数据库连接池3.1 数据库连接池介绍3.2 数据库连接池产品产品3.3 Druid引入项目4lombok4.1 lombok介绍4.2 lombok使用4.2.1 在pom.xml文件中引入依赖4.2.2 pojo类代码引入1 My…

强化学习 Reinforcement Learning(1) ~ 介绍

1. 强化学习概念和分类 强化学习,Reinforcement Learning 通过价值选行为: Q LearningSarsaDeep Q Network 直接选行为: Policy Gradients 想象环境并从众学习: Model Based RL 1.1 通过环境分类 1.1.1 不理解环境 Model-Fr…

4.5 习题(王晓云 主编)

一、选择题1. 下面( ) 是错误的if 语句&#xff08;设int x,a,b;&#xff09;BA&#xff09;if (ab) x; B&#xff09;if (a<b) x;C&#xff09;if (a-b) x; D&#xff09;if (x ) x;2. 以下程序片段( )。Dvoid main ( ){int x0,y0,z0;if (xyz) printf(“***”);else printf…

RT-Thread SP使用教程

RT-Thread SPI 使用教程 实验环境使用的是正点原子的潘多拉开发板。 SPI从机设备使用的是BMP280温湿度大气压传感器。 使用RT-Thread Studio搭建基础功能。 1. 创建工程 使用RT-Thread Studio IDE创建芯片级的工程。创建完成后&#xff0c;可以直接编译下载进行测试。 2.…

JVM学习篇垃圾收集器ParNewCMS与底层三色标记算法详解

1. 垃圾收集算法 2. 分代收集理论 当前虚拟机的垃圾收集都采用分代收集算法&#xff0c;这种算法没有什么新的思想&#xff0c;只是根据对象存活周期的不同将内存分为几块。一般将java堆分为新生代和老年代&#xff0c;这样我们就可以根据各个年代的特点选择合适的垃圾收集算法…

因果推断7--深度因果模型综述(个人笔记)

目录 0摘要 1介绍 2预习 3治疗和指标 4深层因果模型的发展 4.1发展时间表 4.2模型分类 5典型的深层因果模型 6实验指南 6.1数据集 6.2code 6.3实验 7结论 参考 编码 1.自编码器(AE)&#xff1a; 2.去噪自编码器(DAE) 3.变分自编码器VAE 4.去耦变分自编码 文章…

复旦MBA父女“接力”,圆梦复旦|校友故事

父亲&#xff1a;钱一&#xff0c;2006级复旦MBA校友      女儿&#xff1a;钱盈&#xff0c;2017级财务管理本科&#xff0c;金融硕二年级在读      在管院的众多校友中&#xff0c;有这样一对父女&#xff1a;2006年&#xff0c;父亲钱一考上了复旦MBA&#xff0c;每…

node基于springboot 口腔卫生防护口腔牙科诊所管理系统

目录 1 绪论 1 1.1课题背景 1 1.2课题研究现状 1 1.3初步设计方法与实施方案 2 1.4本文研究内容 2 2 系统开发环境 4 2.1 JAVA简介 4 2.2MyEclipse环境配置 4 2.3 B/S结构简介 4 2.4MySQL数据库 5 2.5 SPRINGBOOT框架 5 3 系统分析 6 3.1系统可行性分析 6 3.1.1经济可行性 6 3.…

Hadoop配置文件常用配置-Yarn容器调度策略配置

Yarn资源调度 当同时向Yarn集群提交多个Job任务时&#xff0c;Yarn可以对任务进行资源&#xff08;CPU、MEMORY&#xff09;隔离。 容器调度策略是Yarn默认的调度策略&#xff0c;容器调度策略把整个集群资源划分成队列来管理&#xff0c;默认有一个root根队列&#xff0c;下…

聚类-理论补充

目录 一。聚类的定义 二。相似度/距离计算方法总结 1.闵可夫斯基距离Minkowski/欧式距离 2.杰卡德相似系数(Jaccard) 3.余弦相似度(cosine similarity) 4.Pearson相似系数 5.相对熵(K-L距离) 6.Hellinger距离 三。聚类的基本思想 四。k-Means算法 五。对k-Means的思…

图像显著性目标检测

一、概述 1、定义 图像显著性检测(Saliency Detection,SD)&#xff0c; 指通过智能算法模拟人的视觉系统特点&#xff0c;预测人类的视觉凝视点和眼动&#xff0c;提取图像中的显著区域(即人类感兴趣的区域)&#xff0c;可以广泛用于目标识别、图像编辑以及图像检索等领域&am…

从0到1一步一步玩转openEuler--19 openEuler 管理服务-特性说明

文章目录19 管理服务-特性说明19.1 更快的启动速度19.2 提供按需启动能力19.3 采用cgroup特性跟踪和管理进程的生命周期19.4 启动挂载点和自动挂载的管理19.5 实现事务性依赖关系管理19.6 与SysV初始化脚本兼容19.7 能够对系统进行快照和恢复19 管理服务-特性说明 19.1 更快的…

结合实例,直观理解正态分布、卡方分布、t分布、F分布和对应的Z检验、卡方检验、t检验、F检验

1 正态分布与Z检验 1.1 理论 Z检验的目的是为了验证&#xff1a;已知一个总体服从均值&#xff0c;方差的正态分布&#xff0c;现在有一些样本&#xff0c;这些样本所代表的总体的均值是否为。 则构建一个统计量Z&#xff0c; &#xff08;1&#xff09; 式中&#xff0c;为…

2023第10届生物发酵展3月30-4月1号山东济南开展,参观路线来了

2023第10届生物发酵展3月30-4月1号山东济南开展&#xff0c;参观路线来了&#xff01;展会时间&#xff1a;2023年3月30日-4月1日展馆地址&#xff1a;山东国际会展中心&#xff08;济南市槐荫区日照路1号&#xff09;展馆&#xff1a;4号馆、5号馆BIO CHINA生物发酵展&#xf…

Python|每日一练|栈|递归|散列表|数组|回溯|单选记录:重排链表|编写Python程序实现素数处理的功能| 全排列

1、重排链表&#xff08;栈&#xff0c;递归&#xff09; 给定一个单链表 L 的头节点 head &#xff0c;单链表 L 表示为&#xff1a; L0 → L1 → … → Ln-1 → Ln 请将其重新排列后变为&#xff1a; L0 → Ln → L1 → Ln-1 → L2 → Ln-2 → … 不能只是单纯的改变节点内…

vue中debug调试node_modules的代码

适用于想调试webpack-dev-server&#xff08;第三方模块&#xff09;里的代码&#xff0c;在代码里打印console.log无效的情况。 我用的idea&#xff0c;配置如下&#xff1a; 一、idea配置vue项目启动 1、 主入口js配置&#xff1a;node_modules\vue\cli-service\bin\vue-c…

wine学习笔记

目前 wine 版本为较为稳定的 8.0 版本&#xff0c;本文也是重点围绕 wine 8.0 安装、使用以及 wine 工具介绍等方面进行了学习和整理。 一、安装 wine 1. 如果你使用的是 64 位系统&#xff0c;需要先开启 32 bit 架构支持&#xff1a; $ sudo dpkg --add-architecture i386…