如何检索当前测试'Mocha测试中的名字
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
。
对于摩卡"^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);
相关文章:
- 如何在Mocha/Chai中测试JS原型(非模块)
- 当我尝试运行mocha测试时,没有定义Worker
- Mocha js测试字符串不相等时不显示diff
- 我们能在&在块到mocha的特定测试用例(it)之后
- 如何使用console.log语句在Mocha中测试函数
- Mocha/Chai测试链接到函数返回断言错误
- 如何按照承诺使用mocha/chai/chai测试ES7异步函数
- Mocking console.log()/MOCHA测试框架中的任何其他函数
- 用mocha测试异步函数
- Mocha测试框架的test.describe返回值
- 使用节点调试器调试ES6 Mocha测试
- 如何用mocha和rewire模拟node.js单元测试中的Q方法
- Mocha测试套件因未捕获的异常而暂停
- 如何在JavaScript中使用Mocha和assert.throws测试参数的值
- Node.js+Mocha+Should.js如何测试事件发射器抛出的错误
- Javascript Mocha测试-测试函数表达式中的函数表达式
- Mocha网络套接字测试https服务器
- 编写Karma+Mocha测试,同时使用依赖注入和“完成”
- 单元测试节点模块与mocha,模块变量行为怪异
- 用Blanket测试Mocha的覆盖率;不起作用