我用Mocha来测试我的JavaScript东西。我的测试文件包含5个测试。是否可以运行一个特定的测试(或一组测试),而不是文件中的所有测试?
当前回答
有多种方法可以做到这一点。
If you just want to run one test from your entire list of test cases then, you can write only ahead of your test case. it.only('<test scenario name>', function() { // ... }); or you can also execute the mocha grep command as below mocha -g <test-scenario-name> If you want to run all the test cases which are inside one describe section, then you can also write only to describe as well. describe.only('<Description of the tests under this section>', function() { // ... }); If you have multiple test files & you wanted to run only one of then you can follow the below command. npm test <filepath> eg : npm test test/api/controllers/test.js here 'test/api/controllers/test.js' is filepath.
其他回答
根据您的使用模式,您可能只喜欢使用。我们使用TDD风格;它是这样的:
test.only('Date part of valid Partition Key', function (done) {
//...
}
只有这个测试将从所有文件/套件中运行。
不知道为什么grep方法在使用npm测试时不适合我。不过这是可行的。出于某种原因,我还需要指定测试文件夹。
npm test -- test/sometest.js
只在“describe”,“it”或“context”之前使用。我使用“$npm run test:unit”运行,它只执行带有.only的单元。
describe.only('get success', function() {
// ...
});
it.only('should return 1', function() {
// ...
});
嗨,上面的解决方案对我不起作用。 运行单个测试的另一种方法是
mocha test/cartcheckout/checkout.js -g 'Test Name Goes here'
这有助于从单个文件和特定名称运行测试用例。
对于那些希望运行单个文件但又不能使其工作的人来说,对我有用的是,我需要将我的测试用例包装在如下所示的描述套件中,然后使用描述标题,例如。'My Test Description'作为模式。
describe('My Test Description', () => {
it('test case 1', () => {
// My test code
})
it('test case 2', () => {
// My test code
})
})
然后运行
纱线测试-g“我的测试描述”
or
运行我的测试说明
推荐文章
- 很好的初学者教程socket.io?
- HtmlSpecialChars在JavaScript中等价于什么?
- React: 'Redirect'没有从' React -router-dom'中导出
- 如何在React中使用钩子强制组件重新渲染?
- 我如何使用Jest模拟JavaScript的“窗口”对象?
- 如何在摩卡编程跳过测试?
- 我如何等待一个承诺完成之前返回一个函数的变量?
- 在JavaScript中根据键值查找和删除数组中的对象
- 使嵌套JavaScript对象平放/不平放的最快方法
- 如何以及为什么'a'['toUpperCase']()在JavaScript工作?
- 有Grunt生成index.html不同的设置
- 文档之间的区别。addEventListener和window。addEventListener?
- 如何检查动态附加的事件监听器是否存在?
- 如何写setTimeout与参数Coffeescript
- 将JavaScript字符串中的多个空格替换为单个空格