本文共 3256 字,大约阅读时间需要 10 分钟。
单元测试说明文档
Unit testing is a type of testing which is found at the bottom of the software testing pyramid. It involves breaking the codebase down into smaller parts (or units) and testing those in isolation.
单元测试是一种测试类型,位于软件测试金字塔的底部。 它涉及将代码库分解为较小的部分(或单元),并进行独立测试。
Depending on the type of programming language (or paradigm) these can be against anything you define as a unit, although the most common practice is against functions.
根据编程语言(或范例)的类型,这些可能会与您定义为一个单元的任何内容发生冲突,尽管最常见的做法是针对函数。
Protection - Unit testing protects against introducing new or old bugs for defensive programming
保护 -单元测试可防止为防御性编程引入新的或旧的错误
Confidence - You can add changes, or reuse or refactor code (both very common) and be sure you haven’t added a bug
信心 -您可以添加更改,重用或重构代码(这两种都很常见),并确保您没有添加错误
Documentation - Unit testing documents the behavior and flow of code so its easy for someone new to the code to understand it
文档 -单元测试记录了代码的行为和流程,因此对于新手来说很容易理解
Isolation - It isolates a module from the entire feature. This approach forces you to think of a module by itself, and ask what is its job?
隔离 -将模块与整个功能隔离。 这种方法迫使您自己考虑一个模块,并询问它的工作是什么?
Quality - As unit testing forces you to think about and use your own API, it enforces good/extendable interfaces and patterns. It can point out any tight coupling or over-complexity which should be addressed. Bad code is usually much harder to test
质量 -单元测试迫使您考虑和使用自己的API,因此它会强制执行良好/可扩展的接口和模式。 它可以指出应解决的任何紧密耦合或过于复杂的情况。 错误的代码通常更难测试
Industry Standard - Unit testing is a common discipline these days, and is a requirement for a large portion of software companies
行业标准 -如今,单元测试已成为一门常见的学科,并且是大部分软件公司的要求
Fewer bugs - Substantial research suggests that applying testing to an application can reduce production bug density by 40% — 80%.
错误更少 -大量研究表明,对应用程序进行测试可以将生产错误的密度降低40%-80%。
Suppose there is a function written in file add.js
假设有一个写在文件add.js中的函数
var add = function(number1, number2){ return number1 + number2;}
Now, in order to write unit test of this particular function we can use testing tools like :
现在,为了编写此特定功能的单元测试,我们可以使用诸如类的测试工具:
const mocha = require('mocha')const chai = require('chai') // It is an assertion librarydescribe('Test to check add function', function(){ it('should add two numbers', function(){ (add(2,3)).should.equal(5) //Checking that 2+3 should equal 5 using the given add function });});
Unit testing is a key feature of the test-driven development (TDD) approach to software development.
单元测试是软件开发的测试驱动开发(TDD)方法的关键功能。
In this approach, code for specific features or functions is written through the repeated use of a very short cycle.
在这种方法中,通过重复使用非常短的周期来编写用于特定功能的代码。
First, the developer writes a set of automated unit tests and ensures that they fail initially. Next, the developer implements the bare minimum amount of code required to pass the test cases.
首先,开发人员编写了一套自动化的单元测试,并确保它们最初会失败。 接下来,开发人员实施通过测试用例所需的最少代码。
Once it has been validated that the code is behaving as expected, the developer then goes back and refactors code to adhere to any relevant coding standards.
一旦确认代码的行为符合预期,开发人员便返回并重构代码以遵守任何相关的编码标准。
翻译自:
单元测试说明文档
转载地址:http://ltzzd.baihongyu.com/