我有很多这样的QUnit测试:
test('some test.', function() {
equal(1, 1, "dummy");
});
我还有一个包含测试套件的.html文件:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Cache-Control" content="no-store" />
<title>QUnit Test Suite</title>
<link rel="stylesheet"
href="http://code.jquery.com/qunit/qunit-1.16.0.css" type="text/css"
media="screen">
<script data-main="MainTest" src="lib/require.js"></script>
</head>
<body>
<div id="qunit"></div>
<div id="dialog"></div>
<div id="test-content"></div>
</body>
</html>
我的问题是,当我打开此.html文件(testsuite.qunit.html)时,有时我看不到任何结果,而只是一个空白页。有时我看到一些结果。所有测试都在运行,我可以在顶部看到它:
测试在64毫秒内完成。161个声明(共161个)通过,0个失败。
我检查了qunit生成的html,看来QUnit无法将pass
css类添加到某些测试中。这是一个可见的:
<li id="qunit-test-output-ca229798" class="pass">
<strong><span class="test-name">decorated web service call test</span>
<b class="counts">(1)</b></strong>
<a href="test.js">Rerun</a><span class="runtime">5082 ms</span>
<ol class="qunit-assert-list qunit-collapsed"></ol></li>
这是不可见的:
<li id="qunit-test-output-ca229798">
<strong><span class="test-name">decorated web service call test</span>
<b class="counts">(1)</b></strong>
<a href="test2.js">Rerun</a><span class="runtime">5082 ms</span>
<ol class="qunit-assert-list qunit-collapsed"></ol></li>
可能是什么问题呢?
在大多数情况下,我看到这是由于以下两个原因之一:
(1)源代码中的错误(这似乎不太可能,因为有时测试对您来说可以顺利通过),或
(2)一些源代码是异步的,因此可能会导致争用条件。
如果您有任何异步代码(ajax调用,setTimeout,setInterval等),则需要使用QUnit异步控制机制:
QUnit.test('a test with async code', function(assert) {
var done = assert.async(); // tell QUnit you plan to do async stuff
callSomeAsyncFunction(function() {
// this is the callback for the async action
// all of your assertions here
done(); // tell QUnit you're done with async actions
});
});
我建议您通过注释掉一些测试块来缩小范围,以缩小导致问题的测试范围,并最终解决单个测试有时会通过有时停滞的问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句