簡體   English   中英

使用 qUnit 時如何在每次測試之前運行函數?

[英]How do I run a function before each test when using qUnit?

qUnit 的 nUnits [SetUp]屬性的等價物是什么?

注冊 QUnit 回調

var mySetupFunc(details){/* setup code */}
QUnit.testStart(mySetupFunc);

回調詳情

從 QUnit 1.10.0pre-A 版本開始,每個注冊的回調都會收到一個哈希值作為第一個(也是唯一的)參數。 我在上面的例子中命名了我的“細節”。 散列的內容因回調而異。 這是每個散列中的信息列表。

開始
(所有測試開始)

{}  /* empty hash */

完畢
(所有測試結束)

  • 失敗:(int)總測試失敗
  • 通過:(int)通過的測試總數
  • 總計:(int)運行的總測試
  • 運行時:(int)以毫秒為單位運行測試所需的時間

日志
(在 ok() 方法等中調用)

  • 結果:(布爾值)如果好則為真,如果失敗則為假
  • 消息:(字符串)您傳遞給 ok() 的任何消息

測試開始
(在每次測試開始時調用)

  • name:測試的名稱(傳遞給 test() 或 asyncTest() 的第一個參數)
  • 模塊:模塊的名稱(如果你還沒有調用 module() 方法,這將是未定義的)

測試完成
(在每次測試結束時調用)

  • 名稱:(字符串)測試的名稱(傳遞給 test() 或 asyncTest() 的第一個參數)
  • 模塊:(字符串)模塊的名稱(如果您從未調用過module(),則未定義)
  • 失敗:(int)失敗的斷言計數
  • 通過:(int)成功的斷言計數
  • 總計:(int)測試中所有斷言的計數

模塊開始
(在每個模塊開始時調用)

  • 模塊:模塊的名稱

模塊完成
(在每次測試結束時調用)

  • 模塊:(字符串)模塊的名稱
  • 失敗:(int)失敗的斷言計數(模塊中所有測試的總數)
  • 通過:(int)成功的斷言計數(模塊中所有測試的總數)
  • 總計:(int)模塊中所有斷言的計數

例子

// There's probably a more elegant way of doing this, 
// but these two methods will add a row to a table for each test showing how long
// each test took.  
var profileStartTime = null;

function startTimer(details) {
  profileStartTime = new Date();
}

function stopTimer(details) {
  var stopDate = new Date();
  var duration = stopDate - profileStartTime;
  jQuery('#profiling').append(
    "<tr><td>" 
    + (details.module ? details.module + ":" : "") 
    + details.name 
    + "<\/td><td class='duration'>" 
    + duration 
    + "<\/td><\/tr>");
}

QUnit.testStart(startTimer);
QUnit.testDone(stopTimer);

我上面引用的 html 表如下所示:

<div style='margin: 10px 0;'>
  <table summary='profiling' class='profiling_table'>
    <thead>
    <tr>
      <th>Test Name</th>
      <th>Duration</th>
    </tr>
    </thead>
    <tbody id='profiling'>
    </tbody>
  </table>
</div>

Perry Tew 的回答極大地幫助了我為自己解決這個問題,如果有人感興趣,我編寫了一個封裝的事件對象,它將設置所有事件供您使用。 見下文:

請注意, console.log()不適用於所有瀏覽器!

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.21/jquery-ui.min.js"></script>    
<script src="lib/qunit-1.9.0.js"></script>
<script src="lib/jquery.mockjax.js"></script>

<!-- QUnit Events -->
<script>

    var testSetup = {

        begin       : function (data) /* before any tests start */ {
            console.log("begin: [" + new Date().toLocaleTimeString() + "]");
        },
        moduleStart : function (data) /* before the start of each module */ {
            console.log("-------\n  moduleStart:", data.name);
        },
        testStart   : function (data) /* before the start of each test */ {
            console.log("    testStart:", data.name);
        },
        log         : function (data) /* called after every assertion */ {
            console.log("      log:", data.message);
        },
        testDone    : function (data) /* after each test */ {
            console.log("    testDone:", data);
        },
        moduleDone  : function (data) /* after each module */ {
            console.log("  moduleDone:", data);
        },
        done        : function (data) /* all tests done */ {
            console.log("done:", data);
        },

        init : function () {
            QUnit.begin = testSetup.begin;
            QUnit.moduleStart = testSetup.moduleStart;
            QUnit.testStart = testSetup.testStart;
            QUnit.log = testSetup.log;
            QUnit.testDone = testSetup.testDone;
            QUnit.moduleDone = testSetup.moduleDone;
            QUnit.done = testSetup.done;
            console.log("\n======== QUnit events initialized ==========");
        }
    };

$(document).ready(testSetup.init);
</script>

每當新的斷言測試批次開始運行時,就會調用QUnit.testStart(name) name是測試批次的字符串名稱。

有關更多信息,請參閱文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM