我用Mocha来测试我的JavaScript东西。我的测试文件包含5个测试。是否可以运行一个特定的测试(或一组测试),而不是文件中的所有测试?
当前回答
如果你使用npm测试(使用包。Json脚本)使用一个额外的——通过mocha传递参数
例如NPM test -- --grep“my second test”
编辑:看起来——grep可能有点麻烦(可能取决于其他参数)。您可以:
修改package.json:
"test:mocha": "mocha --grep \"<DealsList />\" .",
或者使用保释金,这似乎不那么麻烦
npm test -- --bail
其他回答
将所有测试合并到一个test.js文件中,并在package.json中添加一个脚本:
"scripts": {
"api:test": "node_modules/.bin/mocha --timeout 10000 --recursive api_test/"
},
在你的测试目录下输入这个命令:
npm run api:test
查看文档,我们看到简单地使用:
mocha test/myfile
将工作。你可以省略结尾的'.js'。
嗨,上面的解决方案对我不起作用。 运行单个测试的另一种方法是
mocha test/cartcheckout/checkout.js -g 'Test Name Goes here'
这有助于从单个文件和特定名称运行测试用例。
如果你使用npm测试(使用包。Json脚本)使用一个额外的——通过mocha传递参数
例如NPM test -- --grep“my second test”
编辑:看起来——grep可能有点麻烦(可能取决于其他参数)。您可以:
修改package.json:
"test:mocha": "mocha --grep \"<DealsList />\" .",
或者使用保释金,这似乎不那么麻烦
npm test -- --bail
有多种方法可以做到这一点。
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.