使用 Mocha.js 进行自动化 Javascript 测试
众所周知,代码容易出错,有时即使我们知道某个工作流程在某些情况下可以正常工作,也很有可能忘记其他一些情况。
简单来说,当我们手动测试代码时,我们可能会错过一些东西。例如,假设我们有两个函数,func1() 和 func2(),我们知道 func1() 适用于我们在测试中定义的情况,但我们发现 func2() 不起作用。然后我们修复了 func2(),但后来忘记检查 func1() 在我们对 func2() 进行更改后是否适用于整个流程。此过程可能会导致错误,这是经常发生几次的典型情况。
现在,我们知道以手动方式运行测试不是一个非常理想的选择,因此建议除了我们可能编写的代码之外,还运行单独编写的测试。这就是所谓的自动化测试
在本教程中,我们将探讨如何在 JavaScript 中使用 Mocha 进行自动化测试。
第一步是能够在我们的代码中同时使用 Mocha。为此,我们可以利用 mocha.js 为我们提供的 CDN 链接。在本教程中,我们还将使用 Chai.js 和 Expect.js,当我们想要检查我们可能编写的不同函数的确切行为时,它们可以很好地与 Mocha 配合使用。
以下是您需要在 index.html 文件中导入的所有 CDN。
Expect.js
https://cdn.rawgit.com/Automattic/expect.js/0.3.1/index.js
Chai.js
https://cdn.rawgit.com/chaijs/chai/3.5.0/chai.js
Mocha.js
https://cdn.rawgit.com/mochajs/mocha/2.2.5/mocha.js
下一步是在您最喜欢的 IDE 或代码编辑器中的简单项目目录中创建三个文件。
- index.html
- index.js
- tests.js
您也可以使用下面显示的 命令。
touch index.html index.js tests.js
注意 - touch 命令可能不适用于您的本地计算机,在这种情况下,请使用 vi 命令而不是。
index.html
现在我们创建了所有文件,是时候编写代码了。打开 index.html 文件并粘贴以下几行。
示例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Automated Testing With Mocha</title> <link href="https://cdn.rawgit.com/mochajs/mocha/2.2.5/mocha.css" rel="stylesheet" /> </head> <body> <div id="mocha"></div> <script src="https://cdn.rawgit.com/Automattic/expect.js/0.3.1/index.js"></script> <script src="https://cdn.rawgit.com/chaijs/chai/3.5.0/chai.js"></script> <script src="https://cdn.rawgit.com/mochajs/mocha/2.2.5/mocha.js"></script> <script src="index.js"></script> <script> const mocha = window.mocha; mocha.setup('bdd'); </script> <script src="tests.js"></script> <script> mocha.checkLeaks(); mocha.run(); </script> </body> </html>
解释
在上面的 HTML 代码中,我通过 CDN 导入了所有依赖项,如 Mocha、Chai 和 Expect。
然后,我以同步顺序导入了两个不同的 js 文件,即 index.js 和 tests.js,这意味着,首先运行 index.js 文件,然后运行一个脚本,在其中设置 window.mocha() 和 bdd。
考虑下面显示的代码片段。
const mocha = window.mocha; mocha.setup('bdd');
在上述代码之后,我调用 tests.js 文件,然后调用 Mocha 的不同方法。请考虑下面显示的代码片段。
mocha.checkLeaks(); mocha.run();
现在是时候编写一些函数或函数以自动方式使用 Mocha 进行测试了。请考虑下面显示的 index.js 代码。
function addNumbers(a, b) { return a + b; }
上面的函数是一个非常简单的函数,其中我们有两个参数,然后简单地在响应中返回这两个数字的总和。
tests.js
现在到了有趣的部分,我们将借助自动化测试来测试上述函数是否按预期工作。考虑下面显示的 tests.js 代码。
const chai = window.chai const expect = chai.expect describe('addNumbers', () => { it('should be able to add two numbers and give proper result', () => { expect(addNumbers(1, 3)).to.deep.equal(4) expect(addNumbers(1, 5)).to.deep.equal(6) expect(addNumbers(-9, -10)).to.deep.equal(-19) }) })
在上面的代码中,我导入了 Chai 和 Expect 包,这两个包通过 index.html 文件中的 CDN 链接提供给我们。
此外,我们使用 describe 函数,其中传递的第一个参数是我们选择的字符串。接下来,我们创建一个匿名函数,在其中调用 it() 函数,该函数依次将字符串作为第一个参数,将匿名箭头函数作为第二个参数。
我们使用 expect 函数,在其中调用我们要测试的实际函数作为参数,然后使用 deep.equal() 方法检查是否相等。
输出
运行 HTML 代码并在浏览器中打开代码后,一切都应按预期工作。您应该会看到浏览器中打印出一段文本,与下面显示的文本有些相似。
>addNumbers should be able to add two numbers and give proper result
让我们添加第二个函数
在上面的示例中,我们测试了一个名为 addNumbers 的简单 JavaScript 函数。现在,让我们尝试添加另一个函数,但这次,我们将使用箭头函数。请考虑下面显示的代码。
index.js
let multiplyNumber = (a, b) => { return a * b; }
tests.js
现在,让我们在 tests.js 文件中为上述函数编写一个自动化测试。请考虑下面显示的代码片段。
describe('multiplyNumber', () => { it('should be able to multiply two numbers and give proper result',() => { expect(multiplyNumber(1, 3)).to.deep.equal(3) expect(multiplyNumber(1, 5)).to.deep.equal(5) expect(multiplyNumber(-9, -10)).to.deep.equal(90) }) })
输出
运行 HTML 代码,这次您将在浏览器中获得两个函数的名称。
addNumbers should be able to add two numbers and give proper result‣ multiplyNumber should be able to multiply two numbers and give proper result
如果函数没有返回预期的输出怎么办?
在我们为其编写自动化测试的两个函数中,我们实际上期望得到正确的值。如果我们对函数的核心逻辑进行更改以返回错误的值怎么办?
考虑一个名为 multiplyNumber 的函数,它存在于 index.js 文件中。让我们对函数进行更改,使其不会给我们预期的输出。
multiplyNumber
let multiplyNumber = (a, b) => { return a * b; }
输出
现在,如果我们在浏览器中运行 HTML 代码,我们将在浏览器中获得以下输出。
multiplyNumber should be able to multiply two numbers and give proper result‣ AssertionError: expected 0.3333333333333333 to deeply equal 3
使用多个 Describe 函数进行自动测试
在上面的两个例子中,我们使用了一个 describe 函数和一些简单函数。现在假设我们要使用一个函数来计算数字的幂。
考虑下面显示的 index.js 代码
function power(x, n) { let res = 1; for (let i = 0; i < n; i++) { res *= x; } return res; }
在上面的函数中,我们采用两个参数,然后将数字的幂提升到 n 次。
tests.js
现在让我们为该函数编写一个自动化测试。
describe("power", function () { describe("raises x to power 2", function () { function checkPower(x) { let expected = x * x; it(`${x} in the power 2 is ${expected}`, function () { expect(power(x, 2)).to.deep.equal(expected); }); } for (let x = 1; x <= 5; x++) { checkPower(x); } }); });
我们可以看到,在自动化测试函数中,我们使用了嵌套的 describe 函数。在这里,我们检查在 index.js 中编写的 power() 函数是否按预期运行。
输出
power raises x to power 2 1 in the power 2 is 1‣ 2 in the power 2 is 4‣ 3 in the power 2 is 9‣ 4 in the power 2 is 16‣ 5 in the power 2 is 25
结论
在本教程中,我们通过示例解释了如何使用 Mocha.js 与 Chai.js 和 Expect.js 在 JavaScript 中执行自动化测试。