Building NODEJS Automation Unit Test framework based on Grunt&mocha (including Code coverage statistics)

Source: Internet
Author: User

Introduction

Grunt is a tool for building a task-based JavaScript world

Mocha is a rich-character JavaScript testing framework that can be run in node. js and in the browser, making asynchronous testing simpler and more interesting. Mocha can run tests continuously, support flexible and accurate reports, and go to the correct test samples when mapping to uncaught exceptions.

Prerequisite

The Nodejs project file directory structure is as follows

├──config├──controllers├──models├──lib├──node_modules├──test├──tasks│   ├──mochacli.js│   ├── sonarrunner.js│   └──mocha_istanbul.js    │──package.json└──gruntfile.js        

The Test folder holds all unit test *.js files

The Tasks folder puts some task files for grunt execution

The dependencies used in Package.json are as follows

"Devdependencies": {    "chai": "^3.0.0",    "chai-as-promised": "^5.1.0",    " Grunt ":" ^0.4.1 ",    " Grunt-config-dir ":" ^0.3.2 ",    " grunt-mocha-cli ":" ^1.5.0 ",    "Grunt-mocha-istanbul": "^2.4.0",    "Grunt-sonar-runner": "^2.4.3",    " Istanbul ":" ^0.3.14 ",    " load-grunt-tasks ":" ~0.2 ",    " Mocha ":" ^1.18.0 ",    "Sinon": "^1.15.4",    "SuperTest": "^0.9.0"  }

These packages are installed via the command npm install XXX--save-dev

Grunt Task

The gruntfile.js is configured as follows

' Use strict 'function  (grunt) {    //  Load The project's grunt tasks from a Direc Tory    require (' Grunt-config-dir ') (grunt, {        configdir:require (' path '). Resolve (' tasks ')    });     // Register Tasks    Grunt.registertask (' Test ', [' mochacli ' ]);    Grunt.registertask (' coverage ', [' mocha_istanbul:coverage ', ' sonarrunner:analysis ']);

Task-' mochacli ' is as follows

' Use strict 'function  mochacli (grunt) {    //  Load task    Grunt.loadnpmtasks (' grunt-mocha-cli ');     // Options    return {        src: [' Test/**/*.js '],        options: {            6000,            true ,            ' BDD ',            ' spec '}    ;};

Use this plugin to run the Mocha test.

Task-' Mocha_istanbul ' is as follows

' Use strict '; Module.exports=functionClean (grunt) {//Load TaskGrunt.loadnpmtasks (' Grunt-mocha-istanbul '); //Options    return{coverage: {src:' Test ',//a folder works nicelyoptions: {coverage:true, reportformats: [' Lcov ', ' lcovonly '], Root:‘.‘, Recursive:true                }              }  };};

Istanbul This plugin is used to calculate code coverage, and can generate a report in Lcov format for integration with the next plug-in Sonarrunner, showing reports

Task-' sonarrunner.js ' is as follows

' Use strict '; Module.exports=functionClean (grunt) {//Load TaskGrunt.loadnpmtasks (' Grunt-sonar-runner '); //Options    return{analysis: {options: {debug:true, Separator:' \ n ', Sonar: {host: {URL:' http://10.101.46.20:9000 '}, jdbc: {URL:' Jdbc:mysql://10.101.46.20:3306/sonar ', Username:' Sonar ', Password:' Sonar '}, javascript: {lcov: {reportpath:' Coverage/lcov.info '}}, Projectkey:' demo:0.1.0 ', ProjectName:' Demo project ', Projectversion:' 0.1.0 ', sources: [' Controllers ', ' models ', ' Lib '].join (', '), Language:' JS ', sourceencoding:' UTF-8 '          }        }      }    };};

The results of running the Grunt test sample are as follows (there are many use cases where you run one for good presentation of the image)

After running the grunt coverage

Here are two tasks, after running the first mocha_istanbul:coverage, a coverage directory will be generated under the root directory

└──coverage    ├──coverage.json    ├──lcov.info    └──lcov-report        ├──index.html        ├──xxx        └──xxx

Lcov.info formatted files are for Sonnar display reports

Open lcov-report/index.html as

The second plug-in is used to integrate with sonar, and these grunt commands I put in Jenkins after daily build.

I don't know why the code coverage numbers that sonar parsed out are quite different from the Istanbul plugin.

Reference

grunt:http://gruntjs.com/

mocha:http://mochajs.org/

Grunt-mocha-cli

Grunt-mocha-istanbul

Grunt-sonar-runner

Thank you for reading, if you think the content of this article is helpful to your study, you can click the Recommendation button at the bottom right, your encouragement is the motive force of my creation.

# #转载注明出处: http://www.cnblogs.com/wade-xu/p/4710968.html

Building NODEJS Automation Unit Test framework based on Grunt&mocha (including Code coverage statistics)

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.