如何使用Mocha运行单个测试? [英] How to run a single test with Mocha?

查看:652
本文介绍了如何使用Mocha运行单个测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Mocha来测试我的JavaScript内容。我的测试文件包含5个测试。是否可以运行特定的测试(或一组测试)而不是文件中的所有测试?

I use Mocha to test my JavaScript stuff. My test file contains 5 tests. Is that possible to run a specific test (or set of tests) rather than all the tests in the file?

推荐答案

尝试使用 mocha的 - grep 选项

Try using mocha's --grep option:

    -g, --grep <pattern>            only run tests matching <pattern>

您可以将任何有效的JavaScript正则表达式用作< pattern> 。例如,如果我们有 test / mytest.js

You can use any valid JavaScript regex as <pattern>. For instance, if we have 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'

运行单个测试。请注意,这会覆盖所有 describe(name,fn) it(name,fn)调用的名称。

To run a single test. Note that this greps across the names of all describe(name, fn) and it(name, fn) invocations.

考虑使用嵌套的 describe()调用命名空间,以便于查找和选择特定集合。

Consider using nested describe() calls for namespacing in order to make it easy to locate and select particular sets.

这篇关于如何使用Mocha运行单个测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆