关于单元测试

I am pretty sure every software engineers have heard of unit tests. However, it is undeniable that making unit tests for some people is quite a headache, same as creating technical documentation: time-consuming, and other things. Even so, it is highly recommended to make a unit test because it has many benefits!

我很确定每个软件工程师都听说过单元测试。 但是,不可否认的是,对于某些人而言,进行单元测试与创建技术文档一样费时费力。 即使这样,仍强烈建议进行单元测试,因为它有很多好处!

前言 (PREFACE)

Okay! So, what is the unit test? A unit test is the process of writing code to test the behaviour and functionality of the system. From this unit test, there is a typical pattern called AAA. Have you ever heard the word of AAA? It is the acronym of Arrange, Act and Assert. AAA is a way to structure your unit tests, so they are easier to read, maintain, and enhance. So, how to use it? Please, be patient! I will show you how to do it in the last section.

好的! 那么,什么是单元测试? 单元测试是编写代码以测试系统行为和功能的过程。 通过此单元测试,有一个称为AAA的典型模式。 您听说过AAA吗? 它是ArrangeActAssert的首字母缩写。 AAA是一种构建单元测试的方法,因此更易于阅读,维护和增强。 那么,如何使用呢? 请耐心等待! 在上一节中,我将向您展示如何做。

From now on, let’s talk about the benefit of the unit test. The benefits are:

从现在开始,让我们谈谈单元测试的好处。 好处是:

  1. Make it easier to change the technical implementation while making sure there is no broken behaviour, for example, when doing refactoring.

    例如,在进行重构时,可以更轻松地更改技术实现,同时确保没有坏的行为。
  2. Give developers confidence when adding behaviour or making fixes.

    在添加行为或进行修复时使开发人员充满信心。
  3. Debugging process.

    调试过程。
  4. Providing technical documentation for other engineers.

    为其他工程师提供技术文档。

To make it clear about the unit test, we can follow the Pyramid of Testing concept, and there is a good explanation about it. Click here for more detail.

为了使单元测试更清楚,我们可以遵循“测试金字塔”的概念,并且对此有很好的解释。 单击此处了解更多信息。

It would be best if you even understood what you have to test. You can consider a normal function (every requirement is fulfilled. e.g.: complete parameters), a function without parameters (should be giving error, except parameter-less feature), and also several edges cases.

最好是,即使您不了解必须测试的内容。 您可以考虑一个普通函数 (满足所有要求,例如:完整参数), 一个不带参数的函数 (除无参数特征外应给出错误)以及几种边缘情况

Key to Success

成功的关键

When you have to write a unit test, it should be considered as GOOD. What Makes a Good Unit Test?

当您必须编写单元测试时,应将其视为“ 良好”。 什么是好的单元测试?

- Easy to write. Developers typically write lots of unit tests to cover different cases and aspects of the application’s behavior, so it should be easy to code all of those test routines without enormous effort.

-易于编写。 开发人员通常编写大量的单元测试来涵盖应用程序行为的不同情况和方面,因此应该容易地编写所有这些测试例程,而无需付出很大的努力。

- Readable. The intent of a unit test should be clear.

-可读。 单元测试的意图应该明确。

- Reliable. Unit tests should fail if there’s a bug in the system under test.

-可靠。 如果被测系统中存在错误,则单元测试应该失败。

- Fast. Developers write unit tests so they can repeatedly run them and check that no bugs have been introduced.

开发人员编写单元测试,以便他们可以重复运行它们并检查是否未引入错误。

- Truly unit, not integration. Should not access the network resources, databases, file system, etc., to eliminate the influence of external factors.

-真正的单位,而不是整合。 不应访问网络资源,数据库,文件系统等,以消除外部因素的影响。

模组 (MODULES)

Now, Let’s talk about what kind of tools which will be used for creating a unit test. In this case, I am using Node.js as the primary language. There are four tools I usually use when starting to develop a unit test:

现在,让我们讨论一下将用于创建单元测试的工具。 在这种情况下,我使用Node.js作为主要语言。 开始开发单元测试时,通常使用四种工具:

  • Test runner: mocha

    测试选手摩卡

  • Assertion library: chai (for asserting)

    断言库chai (用于断言)

  • Test spies, stubs and mocks: sinon (for test setup)

    测试间谍,存根和 模拟sinon (用于测试设置)

  • Code coverage report: instanbul

    代码覆盖率报告伊斯坦布尔

For your information, when you jump into this unit test, there are some vocabularies you should remember in your mind, such as:

供您参考,当您跳入该单元测试时,您应该记住一些词汇,例如:

  • Spies: The main purpose is to get information on function calls, like how many times they were called, or what arguments were passed to them.

    间谍:主要目的是获取有关函数调用的信息,例如调用多少次或向其传递了哪些参数。

  • Stubs & Mocks: Those two are quite similar, but there is a difference between them. According to this article, Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside what’s programmed in for the test. Stubs may also record information about calls, such as an email gateway stub that remembers the messages it ‘sent’, or maybe only how many messages it ‘sent’. Mocks are what we are talking about here: objects pre-programmed with expectations which form a specification of the calls they are expected to receive.

    Stubs&Mocks:这两个很相似,但是它们之间是有区别的。 根据本文的介绍存根为测试期间进行的调用提供了固定的答案,通常不会对测试中编写的内容做出任何响应。 存根还可以记录有关呼叫的信息,例如,电子邮件网关存根可以记住“已发送”的消息,或者仅记住“已发送”的消息数量。 嘲笑是我们在这里谈论的:带有期望的预编程对象,这些对象形成了期望接收的呼叫的规范。

  • Code Coverage: To get a better idea of how well your codebase is covered with tests. This report will include metrics on line coverage, statement coverage, branch coverage, and function coverage.

    代码覆盖率:更好地了解测试对代码库的覆盖程度。 该报告将包括有关行覆盖率,语句覆盖率,分支覆盖率和功能覆盖率的度量。

Please keep in mind, when you create a unit test, mocking too much will reduces test quality. Then, if you want to use real data or mocking is okay, but you shouldn’t use both in a single test. Last but not least, each test case must not depend on each other.

请记住,创建单元测试时, 模拟太多会降低测试质量 。 然后, 如果您想使用真实数据或模拟就可以了,但是您不应该在一个测试中同时使用两者 。 最后但并非最不重要的一点是, 每个测试用例都不得相互依赖

代码会议 (CODE SESSION)

Let’s jump into the code session. In this part, I will guide you through creating a unit test. Here are some guidelines you can follow when writing a unit test:

让我们进入代码会话。 在这一部分中,我将指导您完成创建单元测试的过程。 这是编写单元测试时可以遵循的一些准则:

  1. describe() is merely for grouping, which you can nest as deep

    describe()仅用于分组,您可以嵌套得很深

  2. it() is a test case

    it()是一个测试用例

  3. before(), beforeEach(), after(), afterEach() are hooks to run

    before(),beforeEach(),after(),afterEach()是要运行的钩子

For a better understanding, let’s do the implementation step by step on the controller, for example :

为了更好的理解,让我们逐步在控制器上进行实现 ,例如:

  • Inspecting the code:

    检查代码:
  • Setup the mocked modules:

    设置模拟模块:
  • Prepare all test setup requirement, then define the main test scenario and it’s children with using describe and it. But, you have to remember that nested describe is allowed to use and nested it cannot be undone. See the guidance below:

    准备所有测试设置需求,然后定义主要测试场景,并使用describe及其它作为子类。 但是,您必须记住,嵌套描述允许使用,嵌套描述不能撤消。 请参阅以下指南:

So, that’s it. Thanks for reading, I hope this gives you an idea of what is a unit test and how to create a simple unit test with Node.js.

就是这样了。 感谢您的阅读,我希望这可以使您了解什么是单元测试以及如何使用Node.js创建简单的单元测试。

References and Further Resources:

参考资料和其他资源:

  1. https://samwize.com/2014/02/08/a-guide-to-mochas-describe-it-and-setup-hooks/

    https://samwize.com/2014/02/08/a-guide-to-mochas-describe-it-and-setup-hooks/

  2. https://mochajs.org/

    https://mochajs.org/

翻译自: https://medium.com/@edharmowongso/all-about-unit-test-7227db50ce1a

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
©️2021 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值