NodeJS test framework MOCHA entry tutorial

The most commonly used test framework in nodejs is Mocha. It supports multiple Node’s Assert Libs, and supports asynchronous and synchronous tests, supporting multiple ways to export results, also supporting JavaScript code testing directly on Browser.

This example is mostly from the official website example, and some examples have changed in conjunction with them. For more introduction, please see the official website: Mocha on GitHub

When you successfully install NodeJS V0.10 and NPM, execute the following command.

# npm install -g mocha

PS Ubuntu’s Note APT source in the NodeJS version will be old, some module does not support, please NodeJS official website for source installation.

VAR assert = Require (“assert”) Describe (‘Array’, Function () {Describe (‘# indexof (), function () {it (‘ shop return -1 when the value is not present “, function () {assert.equal (-1, [1, 2, 3] .indexof (5)); assert.equal (-1, [1, 2, 3] .indexof (0));})})});
   
Describe (Modulename, TestDetails) by the above codeIt is seen that Describe is a nest, such as two DESCRIBEs, such as the code nested, can understand the testers want to test the #IndexOf () submodule under the Array module. Module_name can be taken casually, the key is to read well. IT (Info, Function) The specific test statement is placed in the IT callback function, in general, the INFO string will write a brief writing description of the desired correct output. When the console will print out the details when the Test Failed within the IT Block is. It is generally output from the MODULE_NAME of the outermost Describe (which can be understood along the path or recursive chain or the callback chain), and finally output INFO, indicating that the desired INFO content is not satisfied. An IT corresponds to an actual test case
assert.equal (exp1, eXP2) assertion to determine whether the EXP1 results are equal to EXP2, which is equal to judging here is == rather than ===. That is, Assert.equal (1, ‘1’) is considered to be true. This is just an assertion form of Assert.js in NodeJs, which will mention the same usual use of Should.js.

If both EXP1 and EXP2 are strings, the string compares the error with the colors that are differentially identical.

Asynchronous

The code in Frist Step is obviously a SYNCHRONUS code, so what should I do for asynchronous code? Very simple, add DONE () representation in your deepest callback function.

FS = Require (‘fs’); Describe (‘ File ‘, Function () {Describe (‘ # readfile () ‘, function () {i (‘ Should Read Test.ls withoutError ‘, Function (DONE) {fs.readfile (‘ Test.ls’, Function (Err) {IF (ERR) Throw Err; DONE ();});}}}})

DONE ()
 Follow the waterfall flow program habits, name Done is the deepest place in your callback, that is, end writing nested callback functions. But for the callback chain, DONE actually means telling MoCha to start testing from here, and a layer is called back.   
The above code is Test Pass, we try to change Test.ls to Test.as that does not exist. The specific error location will be returned.
There may be a question here. If I have two asynchronous functions (two branked callback chains), where should I add DONE ()? In fact, there should be two functions to test in an IT, in fact, IT can only call DONE, when you call multiple Done, Mocha will throw an error. So it should be like this:



FS = Require (‘fs’); Describe (‘ file ‘, function () {Describe (‘ # readfile (), function ) {IT (‘Should Read Test.ls without Error’, Function (DONE) {fs.readfile (‘Test.ls’, Function (Err) {IF (ERR) Throw Err; DONE ();});}) IT (‘Should Read Test.js without Error’, Function (DONE) {fs.readfile (‘Test.js’, Function “R) {IF (ERR) Throw Err; DONE ();});})})})

PENDING
 即 省 省 去 测试 测试 细 细 保 只 保 只. General applicable, for example, the write frame of the test framework allows the team to achieve details, or the test details have not fully realized the first annotations to avoid affecting global testing. This time MOCHA will default to PASS.  The role is a bit like Python's PASS. 
Describe (‘Array’, Function () {Describe (‘# indexof () “, function () {i (‘ Should Return -1 When the Value IS Not Present ‘, function () {})}});

Exclusive && Inclusive


It is actually very well understood, respectively Corresponding to the ONLY and SKIP functions.

FS = Require (‘fs’); Describe (‘ File ‘, Function () {Describe (‘ # iedfile (), function () {it.skip (‘Should Read Test.ls without Error’, Function (DONE) {fs.readfile (‘Test.ls’, Function (ERR) {IF (ERR) throw Err; DONE ();});}) IT (‘ Should Read Test.js without Error ‘, FunctION (DONE) {})} (123)
  The above code will only have a Test Complete, and only only ONLY will be executed, and the other will be ignored. There can be only one ONLY in each function. If it is IT.skip, then the case will be ignored.  
Only and Skip have no practical significance, because the role of ONLY will shield the SKIP.

FS = Require (‘fs’); Describe (‘ File ‘, Function () {Describe (‘ # iedfile (), function () {it.skip (‘Should Read Test.ls without Error’, Function (DONE) {fs.readfile (‘Test.as’, Function (ERR) {IF (Err) Throw Err; DONE ();});}) IT (‘ Should Read Test.js without Error ‘, Function (DONE) {})}}) The above code although Test.as does not exist, but due to SKIP, still Test Complete will be displayed.

Before && After

The BEFORE and AFTER are often used in unit tests. Mocha also offers BeForeeach () and AfteReach (). Here is convenient to read Livescript,! -> Understand () {}. The details do not need to be readily read, just use the framework how these functions can be used.
   
!! Require assertrequire fscan = itdescribe ‘Array’, -!> BeforeEach -!> Console.log ‘beforeEach Array’ before -!> Console.log ‘before Array’ before -!> Console.log ‘before Array second time’ after ! -> Console.log ‘after array’ describe ‘#indexof (),! -> can’ shop return -1 when the value is not present ‘,! -> assert.equal -1, [1, 2, 3 ] .indexof 0 Can ‘SHOULD RETURN 1 WHEN,! -> Describe’ File ‘,! -> Beforeeach! -> Console.log’ Beforeeach File Test! ‘Afteeach! -> Console.log’ Aftereach File test! ‘Describe’ #readfile () ‘,! -> can’ Should Read Test.ls without error ‘,! (Done) -> fs.readfile’ test.ls’,! (Err) -> if Err throw Err Done! Can ‘Should Read Test.js without Error’,! (DONE) -> fs.readfile ‘Test.js’,! (ERR) -> IFErr throw Err DONE!
By the result, it is known (the use of After and Before),

BeForeeach will take effect on all sub-CASEs under the current describe .

Before and after the code does not have special order requirements.

There can be multiple Before in the same Describe, the execution order is the same as the code order.
The execution order under the same Describe is Before, Beforeeach, AfteReach, after When an IT has multiple Before, the execution order begins from the outermost Describe Before, the rest.
   Test Driven Develop (TDD) 
MOCHA default mode is Behavior Driven Development (BDD), you need to add Test to TDD. Parameters, such as

MoCha -u TDD Test.js The describe, IT, Before, After, etc., which are mentioned, are all in the category of BDD, for TDD, we use Suite, Test, Setup, TEARDOWN. Sample code is as follows:


Suite ‘Array’,! -> setup! -> console.log ‘setup’ teardown! -> console.log ‘Teardown’ Suite ‘ #indexof () ‘,! -> Test’ Should Return -1 When Not Present ‘,! -> assert.equal -1, [1,2, 3] .indexof 4

                     
© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment