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 函数由之前的另一个模块提供。

