[英]How do I run a function before each test when using qUnit?
qUnit 的 nUnits [SetUp]
属性的等价物是什么?
var mySetupFunc(details){/* setup code */}
QUnit.testStart(mySetupFunc);
As of QUnit version 1.10.0pre-A, each registered callback will receive a hash as the first (and only) parameter.从 QUnit 1.10.0pre-A 版本开始,每个注册的回调都会收到一个哈希值作为第一个(也是唯一的)参数。 I've named mine 'details' in the example above.我在上面的例子中命名了我的“细节”。 The contents of the hash vary by callback.散列的内容因回调而异。 Here's a list of information in each hash.这是每个散列中的信息列表。
begin开始
(start of all tests) (所有测试开始)
{} /* empty hash */
done完毕
(end of all tests) (所有测试结束)
log日志
(called within the ok() methods, etc) (在 ok() 方法等中调用)
testStart测试开始
(called at the start of each test) (在每次测试开始时调用)
testDone测试完成
(called at the end of each test) (在每次测试结束时调用)
moduleStart模块开始
(called at the start of each module) (在每个模块开始时调用)
moduleDone模块完成
(called at the end of each test) (在每次测试结束时调用)
// There's probably a more elegant way of doing this,
// but these two methods will add a row to a table for each test showing how long
// each test took.
var profileStartTime = null;
function startTimer(details) {
profileStartTime = new Date();
}
function stopTimer(details) {
var stopDate = new Date();
var duration = stopDate - profileStartTime;
jQuery('#profiling').append(
"<tr><td>"
+ (details.module ? details.module + ":" : "")
+ details.name
+ "<\/td><td class='duration'>"
+ duration
+ "<\/td><\/tr>");
}
QUnit.testStart(startTimer);
QUnit.testDone(stopTimer);
My html table that is reference above looks like this:我上面引用的 html 表如下所示:
<div style='margin: 10px 0;'>
<table summary='profiling' class='profiling_table'>
<thead>
<tr>
<th>Test Name</th>
<th>Duration</th>
</tr>
</thead>
<tbody id='profiling'>
</tbody>
</table>
</div>
Perry Tew's answer helped me greatly in solving this issue for myself, and if anyone is interested I wrote an encapsulated events object that will setup all the events for you to just hook into. Perry Tew 的回答极大地帮助了我为自己解决这个问题,如果有人感兴趣,我编写了一个封装的事件对象,它将设置所有事件供您使用。 See below:见下文:
Please note that console.log()
doesn't work on all browsers!请注意, console.log()
不适用于所有浏览器!
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.21/jquery-ui.min.js"></script>
<script src="lib/qunit-1.9.0.js"></script>
<script src="lib/jquery.mockjax.js"></script>
<!-- QUnit Events -->
<script>
var testSetup = {
begin : function (data) /* before any tests start */ {
console.log("begin: [" + new Date().toLocaleTimeString() + "]");
},
moduleStart : function (data) /* before the start of each module */ {
console.log("-------\n moduleStart:", data.name);
},
testStart : function (data) /* before the start of each test */ {
console.log(" testStart:", data.name);
},
log : function (data) /* called after every assertion */ {
console.log(" log:", data.message);
},
testDone : function (data) /* after each test */ {
console.log(" testDone:", data);
},
moduleDone : function (data) /* after each module */ {
console.log(" moduleDone:", data);
},
done : function (data) /* all tests done */ {
console.log("done:", data);
},
init : function () {
QUnit.begin = testSetup.begin;
QUnit.moduleStart = testSetup.moduleStart;
QUnit.testStart = testSetup.testStart;
QUnit.log = testSetup.log;
QUnit.testDone = testSetup.testDone;
QUnit.moduleDone = testSetup.moduleDone;
QUnit.done = testSetup.done;
console.log("\n======== QUnit events initialized ==========");
}
};
$(document).ready(testSetup.init);
</script>
QUnit.testStart(name)
is called whenever a new test batch of assertions starts running.每当新的断言测试批次开始运行时,就会调用QUnit.testStart(name)
。name
is the string name of the test batch.name
是测试批次的字符串名称。
See the documentation for more info.有关更多信息,请参阅文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.