QUnit – 执行程序
QUnit – 执行程序
本章解释了 QUnit 中方法的执行过程,说明首先调用哪个方法,然后调用哪个方法。下面举例说明QUnit测试API方法的执行过程。
<html>
<head>
<meta charset = "utf-8">
<title>QUnit basic example</title>
<link rel = "stylesheet" href = "https://code.jquery.com/qunit/qunit-1.22.0.css">
<script src = "https://code.jquery.com/qunit/qunit-1.22.0.js"></script>
</head>
<body>
<div id = "qunit"></div>
<div id = "qunit-fixture"></div>
<script>
QUnit.module( "Module A", {
beforeEach: function( assert ) {
assert.ok( true, "before test case" );
}, afterEach: function( assert ) {
assert.ok( true, "after test case" );
}
});
QUnit.test( "test case 1", function( assert ) {
assert.ok( true, "Module A: in test case 1" );
});
QUnit.test( "test case 2", function( assert ) {
assert.ok( true, "Module A: in test case 2" );
});
QUnit.module( "Module B" );
QUnit.test( "test case 1", function( assert ) {
assert.ok( true, "Module B: in test case 1" );
});
QUnit.test( "test case 2", function( assert ) {
assert.ok( true, "Module B: in test case 2" );
});
</script>
</body>
</html>
验证输出
您应该会看到以下结果 –
这就是 QUnit 的执行过程。
-
该模块用于对测试用例进行分组。
-
beforeEach()方法为每个测试用例执行,但是在执行测试用例之前。
-
afterEach()方法为每个测试用例执行,但是在测试用例执行之后。
-
在beforeEach()和afterEach() 之间,每个测试用例都会执行。
-
再次调用QUnit.module(),只需重置之前由另一个模块定义的任何 beforeEach/afterEach 函数。