get title (name) of currently running QUnit test
Asked Answered
U

4

6

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?

Upland answered 11/2, 2013 at 10:8 Comment(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.Hasten
O
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. 
#### 
Otway answered 11/2, 2013 at 14:5 Comment(2)
This was fantastically helpful for me, to test a QUnit test generator! :) Thanks.Hasten
The QUnit callbacks now need to be set up by calling begin(func) etc, rather than assigning to begin etc.Coaster
C
2

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==============

Cutler answered 25/2, 2016 at 10:21 Comment(0)
R
0

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.

Reinaldo answered 11/2, 2013 at 10:31 Comment(3)
does not work, arguments.callee.caller.arguments[0] is 'undefined'Upland
I am not sure as to why it didn't work for you, it should. check: jsfiddle.net/Uc8fsReinaldo
It does not work because of the specific way that QUnit calls test(). So it works in general, yes, but not in this very QUnit situation. Anyway, I got the proper solution now :) Nonetheless, thank you!Upland
C
0

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);
});
Cutler answered 2/3, 2016 at 12:18 Comment(3)
I have come across this answer after it was flagged as low-quality because of its length and content. Please don't just "dump" code, but rather provide an explanation of what the code does and why it should work.Latakia
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.Cutler
would you please edit this information into your answer?Latakia

© 2022 - 2024 — McMap. All rights reserved.