Qunit 入门必读
0.测试如此简单:
1.逻辑比较:
2.告诉它有多少条断言:
3.单向比较:
4.双向比较:
2) notEqual: notEqual::function传入的第一第二个参数不相等(==),则通过。
3)strictEqual : strictEqual::function传入的第一第二个参数绝对相等(===),则通过。
4) notStrictEqual : notStrictEqual::function传入的第一第二个参数绝对不相等(!==),则通过。
5) deepEqual deepEqual::function传入的第一第二个参数完全相等(基本类型,===),则通过。
6) notDeepEqual notDeepEqual::function传入的第一第二个参数不完全相等(基本类型,===),则通过。
5.预期断言:
throws() throws::function传入的第一个参数(回调函数)抛出错误,则通过。
6.异步测试:
7.模块组合:
1)简单模块组合:
2) 带回调函数(setup,teardown)模块组合:
module("module2", {
setup: function() {
ok(true, "once extra assert per test");
this.prop = "foo";
},
teardown: function() {
ok(true, "and one extra assert after each test");
}
});
test("test with setup and teardown", function() {
expect(3);
same(this.prop, "foo", "this.prop === 'foo' in all tests");
});
8.全局回调:
// 测试开始
QUnit.begin = function() {
console.log("Running Test Suite");
};
// 测试结束
QUnit.done = function(failures, total) {
console.info("Suite: %d failures / %d tests", failures, total);
};
// 在每个模块前运行
QUnit.moduleStart = function(name) {
console.group("Module: " + name);
};
// 在每个模块后运行
QUnit.moduleDone = function(name, failures, total) {
console.info("Module: %d failures / %d tests", failures, total);
console.groupEnd();
};
// 每个单元测试前运行.
QUnit.testStart = function(name) {
console.group("Test: " + name);
};
// 每个单元测试后运行
QUnit.testDone = function(name, failures, total) {
console.info("Test: %d failures / %d tests", failures, total);
console.groupEnd();
};
// 在每一个断言后运行
QUnit.log = function(result, message) {
console[ result ? "log" : "error" ](message);
};
9.界面转逻辑:
1)Dom 测试容器:
#qunit-fixture
2)模拟事件:
https://github.com/jquery/jquery-ui/tree/master/tests
10.实用测试插件:
1) https://github.com/jquery/jquery-ui/blob/master/tests/jquery.simulate.js
2) http://qunitjs.com/plugins/
参考阅读:
原创文章,版权声明:自由转载-非商用-非衍生-保持署名 | Creative Commons BY-NC-ND 3.0