在進行前端開發過程中,在某些場景下,需要通過編寫單元測試來提高代碼品質。而JavaScript常用的單元測試架構有這幾個:QUnit, Jasmine, MoCha.下面就基于這三個工具,簡單做一比較:
1. QUnit
QUnit是一個JavaScript單元測試架構. 它是個強大,容易使用和上手的JavaScript單元測試架構.它被用于進行 jQuery, jQuery UI and jQuery 移動工程的測試,以及其他通用的JavaScript代碼測試.
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
使用方法:
index.html:
<a></a>
斷言方法:
- 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.
- 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
基本用法:
Using the default model SpecRunner.html which referenced jasmine.css, jasmine.js, and jasmine-html.js. Write your own spec as below:
MySpec.js
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:
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.
- 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
(QUnit style)test.js
(TDD style)test.js
(BDD style)test.js
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.
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.
QUnit 是非常容易上手,你僅僅需要包含兩個檔案(qunit.css and qunit.js)和一些很少的标記,然後就可以開始編寫測試了。QUnit是一種TDD風格的測試;
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.
jasmine 是很容易開始---它是 all-in-one package ,可以讓你和一個組測試起來很快速,并且你可以很快的上手,Jasmine是一種BDD風格的測試;
本文轉自 念槐聚 部落格園部落格,原文連結:http://www.cnblogs.com/haochuang/p/5714745.html,如需轉載請自行聯系原作者