mocha Easy Tutorial Unit Testing

mocha Easy Tutorial Unit Testing

EDITORIAL

In fact, mocha unit tests there are many online tutorials, but also very easy to understand, but everyone will also have a different understanding of the same tutorial, like me probably traveled all detours before reaching the end people want by to share your own practice, I hope someone from this and enjoy the benefits

Reference Tutorial

installation

The following installation file sizes are between 10-20M, you can complete the installation process fast

  • Node.js
    because mocha install dependencies Node, so before downloading mocha to the official website to download Node.js, Node download and install almost all the way to fool next, If you want to ensure the correctness of the installation, refer to nodejs detailed installation steps .
  • mocha
    mocha installation go testing framework Mocha tutorial examples , there is provided a few demo, you can make sure you are familiar with mocha cells in a test sample in the test procedure step by step

    unit test

  • Before the test unit we need: a js file and a file test.js.

js files stored function that you need to test, test.js store your test js file. Using the above mentioned here a demo of the sample do explain.
Suppose you want to achieve the following addition operation, you can function in the present add.js

function add(x, y) {
  return x + y;
}

Unit testing to be added before the function tail module.exports = add; the function to be tested is exposed to the foregoing "use strict"; standard is provided

"use strict";
function add(x, y) {
  return x + y;
}
module.exports = add;

Then create add.test.js file that can be placed with js files in a directory, you can then create a test folder in the directory where the js file, add.test.js stored in the test folder. The last two forms are run mocha in the directory where js.

The following is a simplified test content add.test.js, wherein the first parameter to describe a description of the test, it is the first parameter is a description of test results

/*以下为固定格式,其中require中内容及变量按照您要测试的函数命名*/
"use strict";
var add = require('./add.js');//根据函数名命名参数
var expect = require('chai').expect;//固定参数
/*以下为测试段*/
describe('加法函数的测试', function() {
  it('1 加 1 应该等于 2', function() {
    expect(add(1, 1)).to.be.equal(2);
  });
});
  • Unit testing
    open cmd window, enter the directory add.js
    input
npm install mocha -g
npm install --save-dev chai

The second particular attention, you may have already installed the following global chai, but to conduct mocha chai local unit tests need to be installed in the directory where js, otherwise it will error Error: Can not find module 'chai '

npm install chai -g

Then there is also a note that point, if you will js and test.js files in a directory, require written as follows test.js

var add = require('./add.js');//根据函数名命名参数

Cmd in the input unit testing as mocha add.test.js

If the test test.js file into the folder, the test.js require written as follows, except that the front add.js "." Number

var add = require('../add.js');//根据函数名命名参数

cmd direct input operation as mocha

Unit testing is complete

Guess you like

Origin www.cnblogs.com/-yyl/p/11704945.html