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 函数即可。