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 函式。

廣告