Get the name (name) of the current QUnit test run

I would like to write a quick splitter to the console in each QUnit test as follows:

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

How can I get the title (possibly also called "name") of the test?

+4
source share
4 answers

You can achieve this with QUnit callbacks . They are called at several different points during the execution of the tests (for example, 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('####'); }; 

He puts this in a file called helper.js and includes it in the test.html page.

It produces a conclusion as follows:

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

Simple use of this solution:

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

========= hello test ===============

+1
source

You should try the Javascript arguments object (more info here ):

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

EDIT:
I created a small (and documented) jsFiddle example on how it should work.
Please note that my answer is pure Javascript one and is true not only for QUnit .

0
source

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

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

All Articles