本章解释了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函数.