![](/img/trans.png)
[英]Retrieve the current test's name within a Mocha test using Typescript
[英]How can I retrieve the current test's name within a Mocha test?
对于其他日志记录,我需要能够打印当前测试的描述。
我怎么能这样做(使用Mocha BDD)?
如果你是直接回调里面describe
,您可以使用this.title
为标题的describe
或this.fullTitle()
获得的层次标题describe
(祖先的标题+这个称号)。 如果你在回调中it
你可以分别使用this.test.title
或this.test.fullTitle()
。 所以:
describe("top", function() {
console.log(this.title);
console.log(this.fullTitle());
it("test", function () {
console.log(this.test.title);
console.log(this.test.fullTitle());
});
});
上面的console.log
语句将输出:
top
top
test
top test
这是一个更完整的示例,显示标题如何根据嵌套而变化:
function dump () {
console.log("running: (fullTitle)", this.test.fullTitle(), "(title)",
this.test.title);
}
function directDump() {
console.log("running (direct): (fullTitle)", this.fullTitle(), "(title)",
this.title);
}
describe("top", function () {
directDump.call(this);
it("test 1", dump);
it("test 2", dump);
describe("level 1", function () {
directDump.call(this);
it("test 1", dump);
it("test 2", dump);
});
});
console.log
语句将输出:
running (direct): (fullTitle) top (title) top
running (direct): (fullTitle) top level 1 (title) level 1
running: (fullTitle) top test 1 (title) test 1
running: (fullTitle) top test 2 (title) test 2
running: (fullTitle) top level 1 test 1 (title) test 1
running: (fullTitle) top level 1 test 2 (title) test 2
在beforeEach
,尝试this.currentTest.title
。
例:
beforeEach(function(){
console.log(this.currentTest.title);
})
使用Mocha 3.4.1
。
对于mocha“^ 5.1.0”,您可以使用console.log(this.ctx.test.title);
在任何测试方法内
it('test method name'), function() { var testName= this.test.title; }
你可以使用:
afterEach(function(){
console.log(this.currentTest.title); //displays test title for each test method
});
干得好:
console.log(this.title);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.