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


当前回答

根据您的使用模式,您可能只喜欢使用。我们使用TDD风格;它是这样的:

test.only('Date part of valid Partition Key', function (done) {
    //...
}

只有这个测试将从所有文件/套件中运行。

其他回答

执行single test - by filename -

实际上,如果你从你的mocha中删除glob模式(例如./test/**/*.spec.js),你也可以通过文件名(而不仅仅是通过" it()-string-grepping ")来运行一个单独的mocha测试。选项,分别创建一个副本,没有:

node_modules/.bin/mocha --opts test/mocha.single.opts test/self-test.spec.js

这是我的mocha.single.opts(唯一的不同是错过了前面提到的glob行)

--require ./test/common.js
--compilers js:babel-core/register
--reporter list
--recursive

背景:虽然你可以覆盖options - file中的各种开关(从——开始),但你不能覆盖glob。这种联系也有 一些解释。

提示:如果node_modules/.bin/mocha让你感到困惑,请使用本地包mocha。你也可以只写mocha,如果你全局安装了它。


如果你想要舒适的包装。仍然:从摩卡中删除**/*-ish glob。选项,将它们插入这里,用于所有测试,将它们留给单个测试:

"test": "mocha ./test/**/*.spec.js",
"test-watch": "mocha -R list -w ./test/**/*.spec.js",
"test-single": "mocha",
"test-single-watch": "mocha -R list -w",

用法:

> npm run test

分别

> npm run test-single -- test/ES6.self-test.spec.js 

注意——它将后面的任何文本链接到NPM脚本

尝试使用摩卡的——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()调用,以便便于定位和选择特定的集合。

如果你使用npm测试(使用包。Json脚本)使用一个额外的——通过mocha传递参数

例如NPM test -- --grep“my second test”

编辑:看起来——grep可能有点麻烦(可能取决于其他参数)。您可以:

修改package.json:

"test:mocha": "mocha --grep \"<DealsList />\" .",

或者使用保释金,这似乎不那么麻烦

npm test -- --bail

嗨,上面的解决方案对我不起作用。 运行单个测试的另一种方法是

mocha test/cartcheckout/checkout.js -g 'Test Name Goes here'

这有助于从单个文件和特定名称运行测试用例。

有多种方法可以做到这一点。

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.