最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - get title (name) of currently running QUnit test - Stack Overflow

programmeradmin0浏览0评论

I would like to log a quick separator to the console in each QUnit test like this:

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?

I would like to log a quick separator to the console in each QUnit test like this:

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?

Share Improve this question asked Feb 11, 2013 at 10:08 nachtigallnachtigall 2,4972 gold badges31 silver badges37 bronze badges 1
  • Please make sure to avoid the scenario where you run the tests and then you need to read the logs to know if they passed or not. This bypasses most of the benefits of automated testing! If the logs are just to provide extra information then no problem. – Jonathan Benn Commented Aug 7, 2015 at 15:19
Add a ment  | 

4 Answers 4

Reset to default 9

You can achieve that using the callbacks of QUnit. They are called at several different points during the execution of the tests (e.g. 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.

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):

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.
Notice that my answer is a pure JavaScript one and is true no only for QUnit.

QUnit.config.current is an Object contains the currently running test. So you can show it, like console.log(QUnit.config.current). There are many paramters of this object(testName, started..) you can change them.

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

评论列表(0)

  1. 暂无评论