获取当前运行的QUnit测试的标题(名称)

nac*_*all 5 javascript qunit

我想在每个QUnit测试中将快速分隔符记录到控制台,如下所示:

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

如何获得测试的标题(也可称为"名称")?

Odi*_*Odi 8

您可以使用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('####');
};
Run Code Online (Sandbox Code Playgroud)

它将它放在一个名为的文件中,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. 
#### 
Run Code Online (Sandbox Code Playgroud)