Mocha Vs Qunit

Mocha VS Qunit

Assess Category
Assess Items

Mocha Qunit
Setup
  
 
Installation
Require to install node.js and Mocha.js

$ npm install mocha.js

Don‘t need install
Dependency Management Manual handle Manual handle
Support AMD Support AMD/No-AMD style Support AMD/No-AMD style
Support test jQuery‘s features Need to import mocha.js, mocha.css and mocha.setup(‘bdd‘), mocha.run();in the html Need to import qunit.js, qunit.css
Support server-side JavaScript
Run the test in command directly,

so more fast

Run the test in browser,not frequently-used
Test Structure

Test Case Organization
Support modules nested

describe(‘ ‘,function(){

describe(‘ ‘,function(){

});

});


Don‘t support modules nested

Qunit.module(‘ ‘, function(){

} );

Support async test Use function done()
Use function async()

Readability Use describe,it Use module, test
Work with Sinon.js Require to install sinon.js import sinon.js
Assertion Require to install chai.js or expect.js No need to install other assertion library
Test Case Execution
1 .Use Timeouts function set a test/tests pass or not

2.Use skip(),only() to run or not run a test/tests


Use skip(),only() to run or not run a test/tests

Reports Test Result Report Simple,don‘t display assertion result of each case Better appreciation,display assertion result of each case
Test Coverage Report   Use istanbul to create coverage report
Integration and configuration Integrate with Grunt
Install grunt-mocha

set some configuration in Gruntfiles.js


Install grunt-qunit-junit

Install grunt-qunit-istanbul

set some configuration in Gruntfiles.js

Compare

Install

Mocha need to install but Qunit needn‘t.

Mocha:

    • install node.js(Download::https://nodejs.org/en/)
    • install mocha
    • global install: $ npm install --global mocha
    • Install in your development project
    • :Cd to your project directory :
    • $ npm install mocha

Qunit:
       Import qunit.js

Test Grammar 

  Mocha has difference interfaces so it has more test style,BDD, TDD, QUnit, Export and Require-style.

  1. BDD

    The BDD interface provides describe(), context(), it(), specify(), before(), after(), beforeEach(), and afterEach().
    context() is just an alias for describe(), and behaves the same way; it just provides a way to keep tests easier to read and organized. Similarly, specify() is an alias for it().

    describe(‘Array‘, function() {

           it(‘ ‘, function(){

    });

      });

  2. TDD

    The TDD interface provides suite()test()suiteSetup()suiteTeardown()setup(), and teardown():

    suite(‘#indexOf()‘, function() {
    test(‘should return -1 when not present‘, function() {
               assert.equal(-1, [1,2,3].indexOf(4));
          });
      });

  3. Qunit

    The QUnit-inspired interface matches the “flat” look of QUnit, where the test suite title is simply defined before the test-cases. Like TDD, it uses suite() and test(), but resembling BDD, it also contains before()after()beforeEach(), and afterEach().

    suite(‘Array‘);
    test(‘#length‘, function() {
                var arr = [1,2,3];
                ok(arr.length == 3);
          }

  4. Exports

The Exports interface is much like Mocha’s predecessor expresso. The keys beforeafterbeforeEach, and afterEach are special-cased, object values are suites, and function values are test-cases:

module.exports = {
      ‘Array‘: {
           ‘#indexOf()‘: {
                 ‘should return -1 when not present‘: function() {
                       [1,2,3].indexOf(4).should.equal(-1);
                  }
            }
      };
    };

  Qunit is not so flexible only use Qunit.module()  and Qunit.test()

Assertion

Mocha: According to different require mocha could install different assertion, like chai.js, shoud.js, expect.js, best-assert,…

Mocha allows you to use any assertion library you wish,  we’re using Node.js’ built-in it. you can use libraries such as:

  1. should.js - BDD style shown throughout these docs

    Click here to expand...

    $ npm install should
    var should = require(‘should‘);
    (5).should.be.exactly(5).and.be.a.Number();
    var should = require(‘should/as-function‘);
    should(10).be.exactly(5).and.be.a.Number()

  2. expect.js expect() style assertions

    Click here to expand...

    $ npm install chai.js
    var expect = require(‘expect.js‘);
    ok
    expect().to.be.ok();
    expect().to.not.be.ok();
    be/equal
    expect().to.be();

  3. chai - expect()assert() and should-style assertions

    Click here to expand...

    $ npm install chai
    var chai = require(‘chai‘)
    , expect = chai.expect
    , should = chai.should();
    , assert = chai.assert;
    Var foo = ‘bar‘;
    assert.equal(foo, ‘bar‘, ‘foo equal bar‘);
    expect(foo).to.equal(‘bar‘);
    foo.should.equal(‘bar‘);

  4. better-assert - C-style self-documenting assert()
  5. unexpected - “the extensible BDD assertion toolkit”

Qunit use itself assert

- ok(state, message)

- equal(actual, expected, message)

- notEqual (actual, expected, message) 
          - deepEqual (actual, expected, message) 
          - notDeepEqual(actual, expected, message) 
          - strictEqual (actual, expected, message) 
          - notStrictEqual(actual, expected, message)

Report  

Mocha: The browse report of mocha test is relative simple and not so beautiful.

Qunit test report is relative more beautiful and clear, we could through the select box run the test case which we want. Another, the report will list all the assert of each case.

Asynchronous code

Testing asynchronous code with Mocha could not be simpler! Simply invoke the callback when your test is complete. By adding a callback (usually named done) to it(), Mocha will know that it should wait for this function to be called to complete the test.

describe(‘User‘, function() {
  describe(‘#save()‘, function() {
    it(‘should save without error‘, function(done) {
      var user = new User(‘Luna‘);
      user.save(done);
    });
  });
});

Qunit use async to wait for asynchronous operation and it often use with setTimeout function.

QUnit.test( "assert.async() test", function( assert ) {var done = assert.async();var input = $( "#test-input" ).focus();    setTimeout(function() {        assert.equal( document.activeElement, input[0], "Input was focused" ); done();    });});

Test jQuery‘s features

Mocha: Run mocha test in browse need set below

1. Create test folder

2. $ mocha init test

Will create four file:index.html, test.js, mocha.js and mocha.css

The index.html will include:
       
     3. Import the files which we needs like the tested js, the dependence,etc.

Qunit: Just need to import qunit.js, qunit.css, the tested js file and other dependence.

Test server-side JavaScript

Mocha: No need browse and could  run the test with command and run very fast, $ npm test

Qunit: Run test with browse, so need more time.

Test control

Mocha could use the Timeouts function to control a test case run time and if the time out of the time,it will failed.But qunit don‘t have this function.

describe(‘a suite of tests‘, function() {
      this.timeout(500);
      it(‘should take less than 500ms‘, function (done) {
            //if the case take more than 500ms it will fail
       });
});

Qunit don‘t have this function.

Integrate with Grunt

Mocha:grunt-mocha: To run mocha test in grunt; grunt-mocha-istanbul: To code coverage for JavaScript Code.

Gruntfile.js

  grunt.initConfig({

       pkg: pkgJson,

  mocha:{

              test:{                 src:[‘<%= pkg.projectFiles.mochaTests %>‘]              }          },
      grunt.loadNpmTasks(‘grunt-mocha‘);
      grunt.registerTask(‘test‘, mocha);
  }

Qunit: grunt-qunit-junit: To run qunit test in grunt; grunt-qunit-istanbul: To code coverage for JavaScript Code.

Gruntfile.js

grunt.initConfig({

        pkg: pkgJson,        qunit_junit: {            options: {                   dest: qunitJunitReportPath  }        },        qunit: {            options: {                 console: false,                 timeout: 10000,                 coverage: {                    disposeCollector: true,                    src: ‘<%= pkg.projectFiles.javascript %>‘,                    htmlReport: qunitHtmlReportPath,                    coberturaReport: qunitCoberturaReportPath,                    cloverReport: qunitCloverReportPath,                    jsonSummaryReport: qunitJsonSummaryReportPath,                    instrumentedFiles: tempPath,                    reportOnFail: true,                    coverageTempPath: coverageTempPath,                    linesThresholdPct: ‘<%= pkg.coverageThreshold %>‘,                    branchesThresholdPct: ‘<%= pkg.branchCoverageThreshold %>‘ }             },             all: ‘<%= pkg.projectFiles.qunitTests %>‘ }

grunt.loadNpmTasks(‘grunt-qunit-istanbul‘);

grunt.loadNpmTasks(‘grunt-qunit-junit‘);

}

Summary

Mocha test server-side common and run with command, so it faster

Qunit test ui more common and need less dependence and plug-in.

For reservation js is tendency ui and the two frame run broswer test has no obvious difference of speed, another our framework has complete configuration of Qunit,

so I support to use Qunit to test our javascript. 

What is mocha?

Mocha is a feature-rich JavaScript test framework running on Node.js(:https://nodejs.org/en/) and in the browser, making asynchronous testing simple and fun. Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases. Detail:https://mochajs.org

Feature

- Open Source Framework 
- Started in Node 
- Supports both client-side and server-side testing 
- Supports both BDD and TDD style tests 
- Supports both command line and browser 
- Supports any JavaScript assertion library (YUI Port, expect.js, should.js, jshould.js, assert.js, chai.js) 
- Supports asynchronous testing 
- Requires an assertion library

What is QUnit?

A JavaScript Unit Testing framework. QUnit is a powerful, easy-to-use JavaScript unit testing framework. It‘s used by the jQuery, jQuery UI and jQuery Mobile projects and is capable of testing any generic JavaScript code.Detail:http://qunitjs.com/

Feature

- Similar to server-side frameworks(JUnit, Nunit)
- Built by the jQuery team
- Used to test jQuery‘s features
- No dependencies
- Can test server-side JavaScript

 
时间: 2024-10-12 23:30:41

Mocha Vs Qunit的相关文章

单元测试——Qunit

为什么需要单元测试 正确性:测试可以验证代码的正确性,在上线前做到心里有底 自动化:当然手工也可以测试,通过console可以打印出内部信息,但是这是一次性的事情,下次测试还需要从头来过,效率不能得到保证.通过编写测试用例,可以做到一次编写,多次运行 解释性:测试用例用于测试接口.模块的重要性,那么在测试用例中就会涉及如何使用这些API.其他开发人员如果要使用这些API,那阅读测试用例是一种很好地途径,有时比文档说明更清晰 驱动开发,指导设计:代码被测试的前提是代码本身的可测试性,那么要保证代码

2017最好的JavaScript框架、库和工具 — SitePoint

与开发者数量相比,可能有更多的JavaScript框架.库和工具.截止到2017年5月,在GitHub上快速搜索能搜到超过110万的JavaScript项目. 在npmjs上有50万的可用包,并且这些包每个月的下载量将近100亿次. 2017.05.29: 更新了本文,旨在能正确的描述当前JavaScript生态的状态. 本文着重讲述目前最流行的客户端JavaScript框架.库和工具之间的基本差异和他们的基本介绍.至于是不是你要寻找的最佳实践那是另外一个问题.你可以选择一个,并坚持使用一段时间

浅谈“sublime中”babel的配置

Babel是一个广泛使用的转码器,可以将ES6代码转为ES5代码,从而在现有环境执行. 这意味着,你可以现在就用ES6编写程序,而不用担心现有环境是否支持.下面是一个例子. // 转码前 input.map(item => item + 1); // 转码后 input.map(function (item) { return item + 1; }); 上面的原始代码用了箭头函数,这个特性还没有得到广泛支持,Babel将其转为普通函数,就能在现有的JavaScript环境执行了. 一.配置文件

任务五十一:多功能相册

面向人群: 有一定的JavaScript基础 难度: 难 重要说明 百度前端技术学院的课程任务是由百度前端工程师专为对前端不同掌握程度的同学设计.我们尽力保证课程内容的质量以及学习难度的合理性,但即使如此,真正决定课程效果的,还是你的每一次思考和实践. 课程多数题目的解决方案都不是唯一的,这和我们在实际工作中的情况也是一致的.因此,我们的要求不仅仅是实现设计稿的效果,更是要多去思考不同的解决方案,评估不同方案的优劣,然后使用在该场景下最优雅的方式去实现.那些最终没有被我们采纳的方案,同样也可以帮

你的第一个AngularJS应用--教程二:基架、建立和测试的工具

介绍有很多可用的工具可以帮助你开发AngularJS 应用,那些非常复杂的框架不在我的讨论范围之中,这也是我开始这系列教程的原因.在第一部分,我们掌握了AngularJS框架的基本结构,开发了第一应用.这篇博文是为那些初学者写到.如果你是一个经验丰富的AngularJS开发者,你可能对揭秘指令或者AngularJS在创业公司的使用更感兴趣.在这一部分,我们将把应用的逻辑层放在一边,而是去学习如何组织正确的AngularJS项目.包括:脚手架.依赖管理.准备测试(包括单元测试和端到端测试).我们用

关于JavaScript测试工具:QUnit, Jasmine, MoCha

在进行前端开发过程中,在某些场景下,需要通过编写单元测试来提高代码质量.而JavaScript常用的单元测试框架有这几个:QUnit, Jasmine, MoCha.下面就基于这三个工具,简单做一比较: 1. QUnit QUnit是一个JavaScript单元测试框架. 它是个强大,容易使用和上手的JavaScript单元测试框架.它被用于进行 jQuery, jQuery UI and jQuery 移动工程的测试,以及其他通用的JavaScript代码测试. Features:- Simi

使用mocha测试

学习了MOCHA官网的示例,将学习成果记录一下.[原文+例子:使用mocha测试] mocha是什么 Mocha是一个跑在node和浏览器上的javascript测试框架,让异步测试变得简单有趣, 并提供灵活精确的报告. 安装 使用npm全局安装 $ npm install --global mocha 作为项目开发依赖安装 $ npm install --save-dev mocha 开始 创建测试文件learn-mocha $ npm install mocha -g // 全局安装moch

前端测试 karma mocha should 都是什么鬼?

测试TDD和BDD的区别 TDD是测试驱动开发,通过用测试用例来规范约束开发者,编写出质量更高的代码 BDD是行为驱动开发,描述行为路径,就像描述故事,产品和前线业务人员可参与到开发流程中,减轻测试和开发写测试用例的成本.用通用的语言形式尽可能避免沟通上的障碍,实现产品和开发者同时定义系统的需求. karma  mocha  should  这些都是什么鬼? karma 是驱动测试的runner,可以执行Javascript代码在多个真实的浏览器中测试.并生成测试报告 安装 Karma :  $

【Mocha.js 101】同步、异步与 Promise

前情提要 在上一篇文章<[Mocha.js 101]Mocha 入门指南>中,我们提到了如何用 Mocha.js 进行前端自动化测试,并做了几个简单的例子来体验 Mocha.js 给我们带来的便利. 在本篇文章中,我们将了解到 Mocha.js 的同步/异步测试,以及如何测试 Promise. 同步代码测试 在上一篇文章中,其实我们已经学会了如何测试同步代码.今天,我们 BDD 风格编写一个测试: var should = require( 'should' ); var Calculator