我用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.
其他回答
有多种方法可以做到这一点。
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.
查看文档,我们看到简单地使用:
mocha test/myfile
将工作。你可以省略结尾的'.js'。
尝试使用摩卡的——grep选项:
-g, --grep <pattern> only run tests matching <pattern>
您可以使用任何有效的JavaScript正则表达式作为<pattern>。例如,如果我们有test/mytest.js:
it('logs a', function(done) {
console.log('a');
done();
});
it('logs b', function(done) {
console.log('b');
done();
});
然后:
$ mocha -g 'logs a'
运行单个测试。注意,这个greps对所有describe(name, fn)和it(name, fn)调用的名称都适用。
考虑对命名空间使用嵌套的describe()调用,以便便于定位和选择特定的集合。
将所有测试合并到一个test.js文件中,并在package.json中添加一个脚本:
"scripts": {
"api:test": "node_modules/.bin/mocha --timeout 10000 --recursive api_test/"
},
在你的测试目录下输入这个命令:
npm run api:test
根据您的使用模式,您可能只喜欢使用。我们使用TDD风格;它是这样的:
test.only('Date part of valid Partition Key', function (done) {
//...
}
只有这个测试将从所有文件/套件中运行。
推荐文章
- src和dist文件夹的作用是什么?
- jQuery UI对话框-缺少关闭图标
- 如何使用AngularJS获取url参数
- 将RGB转换为白色的RGBA
- 如何将“camelCase”转换为“Camel Case”?
- 我们可以在另一个JS文件中调用用一个JavaScript编写的函数吗?
- 如何使用JavaScript重新加载ReCaptcha ?
- jQuery。由于转义了JSON中的单引号,parseJSON抛出“无效JSON”错误
- 在JavaScript关联数组中动态创建键
- ReactJS和公共文件夹中的图像
- 在React Native中使用Fetch授权头
- 为什么我的球(物体)没有缩小/消失?
- 如何使用jQuery检测页面的滚动位置
- if(key in object)或者if(object. hasownproperty (key)
- 一元加/数字(x)和parseFloat(x)之间的区别是什么?