1. 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.

Features:
- 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

Usage:

index.html:
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>QUnit Example</title>
    <link rel="stylesheet" href="/resources/qunit.css">
  </head>
  <body>
    <div id="qunit"></div>
    <div id="qunit-fixture"></div>
    <script src="/resources/qunit.js"></script>
    <script src="/resources/tests.js"></script>
  </body>
</html>

tests.js:
test( "hello test", function() {
  ok( 1 == "1", "Passed!" );
});

Assertions:

- 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)
- raises (actual, expected, message)

2. Jasmine

asmine is a behavior-driven development framework for testing JavaScript code. It does not depend on any other JavaScript frameworks. It does not require a DOM. And it has a clean, obvious syntax so that you can easily write tests.

Since describe and it blocks are functions, they can contain any executable code necessary to implement the test. JavaScript scoping rules apply, so variables declared in a describe are available to any it block inside the suite.

Features:
- Open Source Framework
- Behavior Driven Development framework
- Supports both client-side and server-side testing

Behavior Driven Development:
Write a failing acceptance test <--> Write a failing unit test <--> Write code to pass the test

Usage:
Using the default model SpecRunner.html which referenced jasmine.css, jasmine.js, and jasmine-html.js. Write your own spec as below:

MySpec.js
describe("MyClass", function() {

  it("should be true", function() {
    expect(true).toBeTruthy();
  });

  it("should be false", function() {
    expect(true).toBeFalsy();
  });
});

Built-in Matchers (not):
- expect(x).(not.)toEqual(y);
- expect(x).(not.)toBe(y);
- expect(x ).(not.)toMatch(pattern);
- expect(x ).(not.)toBeDefined();
- Expect(x).(not.)toBeUndefined();
- expect(x ).(not.)toBeNull();
- expect(x ).(not.)toBeTruthy();
- expect(x ).(not.)toBeFalsy();
- expect(x ).(not.)toContain(y);
- expect(x ).(not.)toBeLessThan(y);
- expect(x ).(not.)toBeGreaterThan(y);
- expect(function(){ fn ();}).(not.)toThrow(ex);

Creating Custom Matcher:

steps:
1. Typically created in a beforeEach
2. this.addMatchers ()

Example:
beforeEach(function() {
  this.addMatchers ({
    toBeFive: function() {
      return this.actual === 5;
    }
  });
});

Skipping tests:

Add an “x” in front of the describe or the it function

3. Mocha

Mocha is a feature-rich JavaScript test framework running on node and 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.

Features:

- 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

Usage:

html
<!DOCTYPE html>
<html>
  <head>
    <title>Mocha</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="mocha.css" />
  </head>
  <body>
    <div id="mocha"></div>
    <script src="mocha.js"></script>
    <script src="chai.js"></script>
    <script>mocha.setup('tdd')</script>
    <script>expect = chai.expect;</script>
    <script src="test.js"></script>
    <script>
      mocha.run();
    </script>
  </body>
</html>

(QUnit style)test.js
suite('my first suite');

beforeEach(function() {
  console.log('setup');
});

afterEach(function() {
  console.log('teardown');
});

before(function() {
  console.log('before');
});

after(function() {
  console.log('after');
});

test('test 1', function() {
  expect(1).to.equal(1);
  console.log('test');
});

(TDD style)test.js
suite('my first suite', function() {

setup(function() {
  console.log('setup');
});

teardown(function() {
  console.log('teardown');
});

before(function() {
  console.log('before');
});

after(function() {
  console.log('after');
});

test('test 1', function() {
  expect(1).to.equal(1);
  console.log('test');
});

});

(BDD style)test.js
describe('my first suite', function() {

  beforeEach(function() {
    console.log('setup');
  });

  afterEach(function() {
    console.log('teardown');
  });

  before(function() {
    console.log('before');
  });

  after(function() {
    console.log('after');
  });

  it('should be my first test', function() {
    expect(1).to.equal(1);
    console.log('test');
  });

  describe('inner suite', function() {

    it('should be my second test', function() {
      expect(2).to.equal(2);
      console.log('test 2');
    });

  });

});

Three different assertion syntaxes in Chai js
Assert: var assert = chai.assert;
Expect: var expect = chai.expect;
Should: var should = chai.should(); // notice should is a function

Writing & Running Mocha Tests

TDD and BDD style tests:
(see above usage part)

Filtering:
In the test url, add ?grep=keyword can filter tests.

View source code:
Click on the test, the source code should display.

Exclusive Tests:
Only display one test: it.only('...', function(){...})
Only display one block tests: describe.only('...', function(){...})

Skipping Tests:
Skip one test: it.skip('...', function(){...})
Skip one block test: describe.skip('...', function(){...})

Pending Test:
Only have the description no function: it('...');

Global Leaks:
Newer version does not have this problem.

Slow Test:
Debug source code in developer tool, set break point to one test, you will see the time of the test spent.

Asynchronous Tests with Mocha

it('should be something', function(done){
  ...
  done();
})

Timeout:

The default timeout is 2 seconds for each test.
  

mocha.setup({timeout:3000}); // set timeout for all tests

describe('Outer Describe', function() {
  it('should be asynchronous', function(done) {
    this.timeout(2500); // setup timeout only for this test
    setTimeout(function() {
      expect(1).to.equal(1);
      done();
    }, 2400);
  })
});

Comparison

QUnit is very easy to get started with, as you only need to include two files(qunit.css and qunit.js) and a little bit of markup, then you can start writing tests. QUnit is TDD style tests.

Jasmine is easier to get started – it’s all-in-one package will generally get you and a team up and testing much faster, and you’ll be in good hands with it. Jasmine is BDD style tests.

Mocha is significantly more flexible, but you have to piece it together yourself. There is no spy framework built in to Mocha, so most people use sinon.js. There’s no assertion framework built in to Mocha either, so you’ll have to pick one. Chai is the popular one, but there are many, many others available. You can also configure Mocha for BDD (jasmine style) or TDD (QUnit style) easily. But you have to pick and choose how you want Mocha to work. This flexibility is great because you can build the test environment that you really want. But it means you have more work to do, more individual pieces to maintain /  keep up to date, etc.

JavaScript测试工具比较: QUnit, Jasmine, and Mocha的更多相关文章

  1. 前端技术之:JavaScript测试工具

    Mocha 一个用于Node.js与浏览器端的简单.自由.有趣的JavaScript测试框架. https://mochajs.org/ https://github.com/mochajs/moch ...

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

    在进行前端开发过程中,在某些场景下,需要通过编写单元测试来提高代码质量.而JavaScript常用的单元测试框架有这几个:QUnit, Jasmine, MoCha.下面就基于这三个工具,简单做一比较 ...

  3. 测试工具使用-Qunit单元测试使用过程

    031302620 应课程要求写一篇单元测试工具的博客,但是暂时没用到java,所以不想使用junit(对各种类都不熟悉的也不好谈什么测试),原计划是要用phpunit,但是安装经历了三个小时,查阅各 ...

  4. JavaScript测试工具

    大家都知道Javascript的测试比较麻烦,一般是开发使用一些浏览器的插件比如IE develop bar或是firebug来调试,而测试往往需要通过页面展示后的js错误提示来定位.那么还有其他比较 ...

  5. Javascript单元测试框架比较Qunit VS Jasmine

    Javascript单元测试框架比较Qunit VS Jasmine 工欲行其事必先利其器,好的单元测试框架是TDD成功的一半.Javascript优秀的测试框架很多, 包括Jasmine,Qunit ...

  6. javascript测试框架mocha

    node测试框架mocha 简单.灵活.有趣,mocha是一个功能丰富的javascript测试框架,运行在node和浏览器中,使异步测试变得更加简单有趣.http://mochajs.org/ 安装 ...

  7. JavaScript开发工具大全

    译者按: 最全的JavaScript开发工具列表,总有一款适合你! 原文: THE ULTIMATE LIST OF JAVASCRIPT TOOLS 译者: Fundebug 为了保证可读性,本文采 ...

  8. WEB前端常用的测试工具

    一.QUnit 前端测试工具 QUnit是一个强大的JavaScript单元测试框架,该框架是由jQuery团队的成员所开发,并且是jQuery的官方测试套件.Qunit是Jquery的单元测试框架, ...

  9. modern.IE – Web 开发必备的 IE 浏览器测试工具

    modern.IE 是微软推出的一个开发人员中心,提供免费的工具和资源,旨在使您能够花更少的时间来测试各种版本的 Internet Explorer,并留出更多时间在现代 Web 上构建重要的内容.m ...

随机推荐

  1. Docker容器是否可以改变世界?

    Docker容器是否可以改变世界? 2016-01-15 杜亦舒 2016年了,很多大牛开始预测技术趋势,其中一个普遍的观点我也很认同: Docker会更加流行,会改变程序世界 2015年的上半年我接 ...

  2. Alpha版本十天冲刺--Day6

    会议总结 队员 今天完成 遇到的问题 明天要做 感想 鲍亮 获取帖子接口,url图片解析 无 获取帖子详情接口,发帖接口 这两天都是白天睡大觉,晚上敲代码,感觉得调一下作息了,假期更加爱赖床了,还得继 ...

  3. dos2unix 命令

    最近在学习shell编程,可是在<Linux程序设计>指定的网站上下载了源码,使用的时候却一直出问题.提示:"bash: ./here1:/bin/sh^M:损坏的解释器: 没有 ...

  4. JS中循环绑定遇到的问题及解决方法

    本文是原创文章,如需转载,请注明文章出处 在工作中,有时会有这样的需求:在一个页面上添加了6个按钮,然后分别为他们绑定点击事件监听器,当点击按钮1时,输出1,当点击按钮2时,输出2. 循环绑定代码如下 ...

  5. document.all.wb.ExecWB

      <%@ page language="java" pageEncoding="UTF-8"%>   <%@ taglib uri=&quo ...

  6. Linux内核原子(1) - spinlock的实现

    spinlock的数据结构spinlock_t定义在头文件linux/spinlock_types.h里面: typedef struct { raw_spinlock_t raw_lock; #if ...

  7. 周爱民:真正的架构师是没有title的(图灵访谈)

    周爱民,现任豌豆荚架构师,国内软件开发界资深软件工程师.从1996年起开始涉足商业软件开发,历任部门经理.区域总经理.高级软件工程师.平台架构师等职,有18年的软件开发与架构.项目管理及团队建设经验, ...

  8. 微信 URL Scheme详细参数

    weixin:// "weixin://dl/stickers""weixin://dl/games""weixin://dl/moments&quo ...

  9. ASP.NET MVC增删改查

    ASP.NET MVC中的增删改查 基本都要使用C控制器中的两个action来完成操作,一个用于从主界面跳转到新页面.同时将所需操作的数据传到新界面,另一个则对应新界面的按钮,用于完成操作.将数据传回 ...

  10. 树(三)——自平衡二叉树(AVL)

    简介 自平衡二叉树(AVL)属于二叉平衡树的一类,此类树主要完成一个从键到值的查找过程,即字典(或映射),它维护树高度的方式与其他数据结构不同. 自平衡规则: AVL树的左.右子树都是AVL树 左.右 ...