javascript - 如何在 Mocha 中使用增量变量创建测试名称
问题描述
我正在使用 Mocha,我想做这样的事情:
describe('My tests', () => {
let i
before(function () {
i = 0
})
beforeEach(function () {
i++
})
it('Test ' + i, function () {
cy.log('inside first test')
})
it('Test ' + i, function () {
cy.log('inside second test')
})
})
我得到Test undefined
一个测试名称,而不是Test 1
, Test2
。我怎样才能在摩卡中实现这一点?
解决方案
由于钩子的工作方式,您可以像这样在名称中使用增量。
describe('My tests', () => {
let i = 0
it('Test ' + ++i, function () {
console.log('inside first test')
})
it('Test ' + ++i, function () {
console.log('inside second test')
})
})
你得到输出:
My tests
inside first test
√ Test 1
inside second test
√ Test 2
推荐阅读
- firebase - 未来
返回一个未来 而不是 UserData 变量 - node.js - expo publish 总是以 Ubuntu 上的 Javascript head out of memory 结束
- pytorch - 我们可以在 Pytorch DataLoader 中的工作人员之间共享内存吗?
- java - 如何忽略正则表达式中的正/负后瞻/前瞻组?
- javascript - 如何将日期传递给我的 addEventListener 函数?
- java - 加密密钥中的盐可以不同用于加密和解密吗?
- asp.net - 这行中的 (?) 是什么意思?- ASP.net
- php - Windows / PHP 在 Intranet 网站上打开本地文件夹
- java - SWIG 生成的代码在提前返回时泄漏内存
- gulp - Gulp 适用于任务,但不适用于观察者