2013-02-11 31 views
4

标题(名称),我想快速分离器登录到控制台中的每个QUnit测试是这样的:获得当前正在运行的QUnit测试

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

我怎样才能拿到冠军(可能也被称为“名“)的测试?

+0

请确保避免您运行测试的场景,然后您需要阅读日志以了解它们是否通过。这绕过了自动化测试的大部分好处!如果日志只是提供额外的信息,那么没有问题。 – 2015-08-07 15:19:40

回答

8

您可以使用callbacks of 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. 
#### 
+0

这对我来说非常有帮助,可以测试一个QUnit测试发生器! :) 谢谢。 – 2015-08-07 15:17:12

0

你应该尝试Javascriptarguments对象(阅读更多here):

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

编辑:
我创建了一个小的(和记录)它应该如何工作jsFiddle example
请注意,我的答案是纯粹的JavaScript之一,并且不仅对于QUnit是正确的。

+1

不起作用,'arguments.callee.caller.arguments [0]'是'undefined' – nachtigall 2013-02-11 13:52:50

+0

我不确定它为什么不适合你,它应该。检查:http://jsfiddle.net/Uc8fs/ – 2013-02-12 05:42:03

+2

由于QUnit调用test()的具体方式,它不起作用。所以一般来说,是的,但不是在这种非常QUnit的情况下。无论如何,我现在得到了正确的解决方案:)尽管如此,谢谢! – nachtigall 2013-02-12 08:21:33

1

它易于使用该解决方案:

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

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

0

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

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

由于其长度和内容_被标记为低质量,所以我遇到过这个答案。请不要只是“转储”代码,而是提供代码的作用以及为什么应该起作用的解释。 – 2016-03-02 16:46:50

+0

QUnit.config.current是一个Object包含当前正在运行的测试。所以你可以显示它,如 console.log(QUnit.config.current)。这个对象有许多参数(testName,开始..),你可以改变它们。 – collo21 2016-03-03 07:59:36

+0

您是否可以将这些信息编辑成您的答案? – 2016-03-03 08:05:32