6

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

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

我怎样才能得到测试的标题(可能也称为“名称”)?

4

4 回答 4

9

您可以使用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. 
#### 
于 2013-02-11T14:05:45.433 回答
2

使用此解决方案很简单:

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

=========你好测试===============

于 2016-02-25T10:21:14.030 回答
0

你应该试试Javascript's argumentsobject(在这里阅读更多):

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

编辑:
我创建了一个小的(和记录的)jsFiddle 示例,说明它应该如何工作。
请注意,我的答案是纯粹JavaScript的,并且不仅适用于QUnit.

于 2013-02-11T10:31:46.910 回答
0

QUnit.config.current 是一个包含当前正在运行的测试的对象。所以你可以显示它,比如 console.log(QUnit.config.current)。这个对象有很多参数(testName,started..)你可以改变它们。

QUnit.test("some test", function() {
  console.log( QUnit.config.current.testName);
});
于 2016-03-02T12:18:43.707 回答