javascript-Test Framework: Mocha

Source: Internet
Author: User
Tags install node

Reference: http://www.ruanyifeng.com/blog/2015/12/a-mocha-tutorial-of-examples.html

First, installation environment: 工欲善其事, its prerequisite!

1. Node. JS Installation:

(1) windons environment: Download and install node. JS installation package;

Command Line window: node--version check for installation

2. Installation of NPM:

(1) NPM Install

(2) After the 0node.js Package management tool NPM installation module, the command line cannot be executed, prompting ' xxx ' is not a workaround for internal or external commands: This problem occurs because of a problem with NPM installation, and the Global module catalog is not added to the system environment variable.

(3) The Windows user checks to see if the NPM directory is added to the system variable path, and if it does not exist, it needs to be added manually, and then the CMD console will need to be restarted.

(4) Nodejs module Global Directory environment variable, NPM directory can use NPM command to find: NPM config get prefix

(5) Installation error: NPM warn Package.json @1.0.0 no repository field. The literal meaning is probably missing the repository field in Package.json, which means that the warehouse field for the project is missing.

{    ...    "Repository": {        "type":"git",        "URL":"http://baidu.com"    },    ...} But as a test project or practice, just do the following configuration in Package.json: {..."Private":true,    ...} Declare the project as private in this way. 

3. Mocha global Variable Installation:

(1) npm Install--global Mocha

Second, Mocha test framework demo Run

1. The so-called test framework, the "tool for running tests".

2. Mocha is one of the JavaScript test frameworks that run test scripts, the so-called "test scripts," which are scripts used to test the source code.

Third, the test script to the wording

The role of 1.Mocha is to run the test script, the following is the code of an addition module

Add.jsfunction Add (x, y) {   return x + y;} Modul.exports = add;

(1) To test whether the addition module is correct, write a test script.

(2) Typically, the test script has the same name as the source script to be tested, but the suffix is. test.js (for test) or. Spec.js (for specification). For example, Add.js's test script name is: Add.test.js.

Add.test.jsvar add = require ('./add.js '); var expect = require (' Chai '). Expect;describe (' Test of addition function ', function () {    it (' 1 plus 1 should be equal to 2 ', function () {       expect (Add. to.be.equal (2));});    });

(3) The above code, is the test script, can run independently.

(4) The test script should include one or more describe blocks, and each describe block should include one or more it blocks.

(5) The describe block, called a test suite, represents a set of related tests. It is a function, the first parameter is the name of the test suite ("test of the addition function"), and the second argument is a function that is actually executed.

(6) The IT block is called the test case, which represents a single test, which is the smallest unit of testing. It is also a function, the first parameter is the name that the test uses ("1 plus 1 should be equal to 2"), and the second argument is a function that is actually executed.

Iv. usage of the assertion library

Expect (add). To.be.equal (2));//Assertion

(1) The so-called "assertion", is to determine the actual execution of the source code and the expected results are consistent, if inconsistent throws an error. The above assertion means that the call to add (1, 1), the result should be equal to 2.

(2) All tests used (it blocks) should contain assertions with one or more sentences. It is the key to writing test cases. The assertion function is implemented by the assertion library, and the mocha itself does not have an assertion library, so the assertion library must be introduced first.

var expect = require (' Chai '). Expect;

(3) There are many kinds of assertion libraries, and Mocha does not restrict which one to use. The assertion library introduced by the above code is chai, and specifies the expect assertion style that uses it.

(4) The advantage of expect assertion is that it is close to natural language, here are some examples:

Equal or do not want to wait expect (4 + 5). To.be.equal (9); expect (4 + 5). To.be.not.equal, Expect (foo). To.be.deep.equal ({bar: ' Baz '}); The/Boolean value is Trueexpect (' everything '). To.be.ok;expect (False). To.not.be.ok;//typeofexpect (' Test '). To.be.a (' string '); Expect ({foo: ' Bar '}). to.be.an (' object '); expect (foo). to.be.an.instanceof (foo);//includeexpect ([]). To.include (2); expect (' Foobar '). To.contain (' foo '); expect ({foo: ' bar ', Hello: ' Universe '}). To.include.kes (' foo ');// Emptyexpect ([]). To.be.empty;expect ("). To.be.empty;expect ({}). To.be.empty;//matchexpect (' Foobar '). To.be.match ( /^foo/);

(5) Basically, expect assertions are written in the same way. The head is the expect method, the tail is the method of assertion, such as equal, A/an, OK, match and so on. Between the two is to use to or to.be link;

(6) If the expect assertion is not true, an error will be thrown. In fact, as long as the error is not thrown, the test case passes.

It (' 1 plus 1 should be equal to 2 ', function () {});

(7) The above test case, there is no code inside, because there is no error thrown, so still pass;

V. Basic usage of Mocha

Once you have the test script, you can run it with Mocha.

$ Mocha Add.test.js  addition function test    ? 1 plus 1 should be equal to 2  1 passing (8ms)

(1) The above results indicate that the test script passed the test, there is only one test case, the time is 8 milliseconds.

(2) The Mocha command, followed by the path and file name of the test script, allows you to specify multiple test scripts:

$ mocha File1 file2 File3

(3) Mocha test script is run by default. Therefore, the test script will generally be prevented from testing directory inside, and then do not need parameters to execute mocha, enter the subdirectory, enter the mocha command to run;

(4) Open the test subdirectory, you will find a test/dir subdirectory, there is a test script multiple.test.js, and did not get executed. Because Mocha defaults to only the first-tier test cases below the test subdirectory, the lower-level use cases are not executed. In order to change this behavior, the--recursive parameter must be added, and all test cases underneath the test subdirectory (regardless of the layer) will be executed.

javascript-Test Framework: Mocha

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.