我用Mocha来测试我的JavaScript东西。我的测试文件包含5个测试。是否可以运行一个特定的测试(或一组测试),而不是文件中的所有测试?
当前回答
尝试使用摩卡的——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()调用,以便便于定位和选择特定的集合。
其他回答
有多种方法可以做到这一点。
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.
将所有测试合并到一个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'
这有助于从单个文件和特定名称运行测试用例。
对于那些希望运行单个文件但又不能使其工作的人来说,对我有用的是,我需要将我的测试用例包装在如下所示的描述套件中,然后使用描述标题,例如。'My Test Description'作为模式。
describe('My Test Description', () => {
it('test case 1', () => {
// My test code
})
it('test case 2', () => {
// My test code
})
})
然后运行
纱线测试-g“我的测试描述”
or
运行我的测试说明
推荐文章
- 使用jQuery以像素为整数填充或边距值
- 检查是否选择了jQuery选项,如果没有选择默认值
- Next.js React应用中没有定义Window
- 如何重置笑话模拟函数调用计数之前,每次测试
- 如何强制一个功能React组件渲染?
- 在javascript中从平面数组构建树数组
- 将Dropzone.js与其他字段集成到现有的HTML表单中
- 如何在AngularJS中观察路由变化?
- JavaScript DOM删除元素
- 将dd-mm-yyyy字符串转换为日期
- Javascript复选框onChange
- Javascript函数前导bang !语法
- 如何在页面上遍历所有DOM元素?
- 在JS/jQuery中触发按键/按键/按键事件?
- 如何每5秒重新加载页面?