简体   繁体   English

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

[英]get title (name) of currently running QUnit test

I would like to log a quick separator to the console in each QUnit test like this: 我想在每个QUnit测试中将快速分隔符记录到控制台,如下所示:

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

How can I get the title (maybe also called "name") of the test? 如何获得测试的标题(也可称为“名称”)?

You can achieve that using the callbacks of QUnit . 您可以使用QUnit回调实现这一目标 They are called at several different points during the execution of the tests (eg before each test, after each module, ...) 它们在测试执行期间的几个不同点被调用(例如在每次测试之前,在每个模块之后,......)

Here is an example from my test suite: 以下是我的测试套件中的一个示例:

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('####');
};

It put this in a file called helper.js and include it on the test index.html page. 它将它放在一个名为helper.js的文件中,并将其包含在测试index.html页面中。

It produces output like this: 它产生如下输出:

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

It's simple to use this solution : 使用此解决方案很简单:

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

========= hello test============== =========你好测试==============

You should try Javascript 's arguments object (read more here ): 你应该尝试Javascriptarguments对象( 在这里阅读更多):

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

EDIT: 编辑:
I have created a small (and documented) jsFiddle example of how it should work. 我已经创建了一个小的(并且有文档记录的) jsFiddle示例 ,说明它应该如何工作。
Notice that my answer is a pure JavaScript one and is true no only for QUnit . 请注意,我的答案是一个纯JavaScript并且不仅仅适用于QUnit

QUnit.config.current is an Object contains the currently running test. QUnit.config.current是一个包含当前正在运行的测试的对象。 So you can show it, like console.log(QUnit.config.current). 所以你可以显示它,比如console.log(QUnit.config.current)。 There are many paramters of this object(testName, started..) you can change them. 这个对象有很多参数(testName,启动..)你可以改变它们。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM