2013-02-11 32 views
4

標題(名稱),我想快速分離器登錄到控制檯中的每個QUnit測試是這樣的:獲得當前正在運行的QUnit測試

test("hello test", function() { 
    testTitle = XXX; // get "hello test" here 
    console.log("========= " + testTitle + "=============="); 
    // my test follows here 
}); 

我怎樣才能拿到冠軍(可能也被稱爲「名「)的測試?

+0

請確保避免您運行測試的場景,然後您需要閱讀日誌以瞭解它們是否通過。這繞過了自動化測試的大部分好處!如果日誌只是提供額外的信息,那麼沒有問題。 – 2015-08-07 15:19:40

回答

8

您可以使用callbacks of QUnit來實現。他們正在測試的執行過程中被稱爲在幾個不同點(每次測試之前例如,每一個模塊後,...)

這裏是我的測試套件的例子:

QUnit.begin = function() { 
    console.log('####'); 
}; 

QUnit.testStart = function(test) { 
    var module = test.module ? test.module : ''; 
    console.log('#' + module + " " + test.name + ": started."); 
}; 

QUnit.testDone = function(test) { 
    var module = test.module ? test.module : ''; 
    console.log('#' + module + " " + test.name + ": done."); 
    console.log('####'); 
}; 

它把這個在名爲helper.js的文件中,並將其包含在測試index.html頁面中。

它產生的輸出是這樣的:

#### 
#kort-Availability Includes: started. 
#kort-Availability Includes: done. 
#### 
#kort-UrlLib Constructor: started. 
#kort-UrlLib Constructor: done. 
#### 
#kort-UrlLib getCurrentUrl: started. 
#kort-UrlLib getCurrentUrl: done. 
#### 
+0

這對我來說非常有幫助,可以測試一個QUnit測試發生器! :) 謝謝。 – 2015-08-07 15:17:12

0

你應該嘗試Javascriptarguments對象(閱讀更多here):

test("hello test", function() { 
    testTitle = arguments.callee.caller.arguments[0]; // get "hello test" here 
    console.log("========= " + testTitle + "=============="); 
    // my test follows here 
}); 

編輯:
我創建了一個小的(和記錄)它應該如何工作jsFiddle example
請注意,我的答案是純粹的JavaScript之一,並且不僅對於QUnit是正確的。

+1

不起作用,'arguments.callee.caller.arguments [0]'是'undefined' – nachtigall 2013-02-11 13:52:50

+0

我不確定它爲什麼不適合你,它應該。檢查:http://jsfiddle.net/Uc8fs/ – 2013-02-12 05:42:03

+2

由於QUnit調用test()的具體方式,它不起作用。所以一般來說,是的,但不是在這種非常QUnit的情況下。無論如何,我現在得到了正確的解決方案:)儘管如此,謝謝! – nachtigall 2013-02-12 08:21:33

1

它易於使用該解決方案:

test("hello test", function(assert) { 
    testTitle = assert.test.testName; // get "hello test" here 
    console.log("========= " + testTitle + "=============="); 
    // my test follows here 
}); 

=========你好測試======== ======

0

QUnit.config.current是一個Object包含當前正在運行的測試。所以你可以顯示它,比如console.log(QUnit.config.current)。這個對象有許多參數(testName,開始..),你可以改變它們。

QUnit.test("some test", function() { 
    console.log(QUnit.config.current.testName); 
}); 
+0

由於其長度和內容_被標記爲低質量,所以我遇到過這個答案。請不要只是「轉儲」代碼,而是提供代碼的作用以及爲什麼應該起作用的解釋。 – 2016-03-02 16:46:50

+0

QUnit.config.current是一個Object包含當前正在運行的測試。所以你可以顯示它,如 console.log(QUnit.config.current)。這個對象有許多參數(testName,開始..),你可以改變它們。 – collo21 2016-03-03 07:59:36

+0

您是否可以將這些信息編輯成您的答案? – 2016-03-03 08:05:32