简体   繁体   English

如何使用 Mocha 运行单个测试?

[英]How to run a single test with Mocha?

I use Mocha to test my JavaScript stuff.我使用 Mocha 来测试我的 JavaScript 内容。 My test file contains 5 tests.我的测试文件包含 5 个测试。 Is that possible to run a specific test (or set of tests) rather than all the tests in the file?是否可以运行特定测试(或一组测试)而不是文件中的所有测试?

Try using mocha's --grep option :尝试使用mocha 的--grep选项

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

You can use any valid JavaScript regex as <pattern> .您可以使用任何有效的 JavaScript 正则表达式作为<pattern> For instance, if we have test/mytest.js :例如,如果我们有test/mytest.js

it('logs a', function(done) {
  console.log('a');
  done();
});

it('logs b', function(done) {
  console.log('b');
  done();
});

Then:然后:

$ mocha -g 'logs a'

To run a single test.运行单个测试。 Note that this greps across the names of all describe(name, fn) and it(name, fn) invocations.请注意,这会遍历所有describe(name, fn)it(name, fn)调用的it(name, fn)

Consider using nested describe() calls for namespacing in order to make it easy to locate and select particular sets.考虑使用嵌套的describe()调用进行命名空间,以便轻松定位和选择特定的集合。

Depending on your usage pattern, you might just like to use only .根据您的使用模式,你可能只是想用。 We use the TDD style;我们使用 TDD 风格; it looks like this:它看起来像这样:

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

Only this test will run from all the files/suites.只有这个测试才会从所有文件/套件运行。

If you are using npm test (using package.json scripts) use an extra -- to pass the param through to mocha如果您使用npm test (使用 package.json 脚本),请使用额外的--将参数传递给 mocha

eg npm test -- --grep "my second test"例如npm test -- --grep "my second test"

EDIT : Looks like --grep can be a little fussy (probably depending on the other arguments).编辑:看起来--grep可能有点挑剔(可能取决于其他参数)。 You can:你可以:

Modify the package.json:修改package.json:

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

Or alternatively use --bail which seems to be less fussy或者使用--bail似乎不那么挑剔

npm test -- --bail

Just use .only before 'describe', 'it' or 'context'.只需在“描述”、“它”或“上下文”之前使用 .only。 I run using "$npm run test:unit", and it executes only units with .only.我使用“$npm run test:unit”运行,它只执行带有 .only 的单元。

describe.only('get success', function() {
 // ...
});

it.only('should return 1', function() {
  // ...
});

run single test –by filename–运行单个测试——按文件名——

Actually, one can also run a single mocha test by filename (not just by „it()-string-grepping“) if you remove the glob pattern (eg ./test/**/*.spec.js ) from your mocha.opts, respectively create a copy, without:实际上,如果您从 mocha 中删除 glob 模式(例如./test/**/*.spec.js ),您还可以通过文件名(不仅仅是“it()-string-grepping”)运行单个 mocha 测试.opts,分别创建一个副本,不带:

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

Here's my mocha.single.opts (it's only different in missing the aforementioned glob line)这是我的 mocha.single.opts (只是缺少上述 glob 行不同)

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

Background: While you can override the various switches from the opts-File (starting with -- ) you can't override the glob .背景:虽然您可以覆盖 opts-File 中的各种开关(以--开头),但您不能覆盖 glob That link also has some explanations.该链接也有一些解释。

Hint: if node_modules/.bin/mocha confuses you, to use the local package mocha.提示:如果node_modules/.bin/mocha使您感到困惑,请使用本地包 mocha。 You can also write just mocha , if you have it installed globally.你也可以只写mocha ,如果你全局安装了它。


And if you want the comforts of package.json : Still: remove the **/* -ish glob from your mocha.opts , insert them here, for the all-testing, leave them away for the single testing:如果你想要package.json的舒适:仍然:从你的mocha.opts删除**/* -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",

usage:用法:

> npm run test

respectively分别

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

mind the -- which chains whatever text comes after it to the npm script 注意--它将后面的任何文本链接到npm脚本

There are multiple ways by which you can do this.有多种方法可以做到这一点。

  • 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 grep 命令

    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 部分中的所有测试用例,那么您也可以只编写描述。

     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/api/controllers/test.js”是文件路径。

You can try "it.only"你可以试试“it.only”

 it.only('Test one ', () => {

            expect(x).to.equal(y);
        });
it('Test two ', () => {

            expect(x).to.equal(y);
        });

in this the first one only will execute在这第一个只会执行

Hi above solutions didn't work for me.嗨,上述解决方案对我不起作用。 The other way of running a single test is运行单个测试的另一种方法是

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

This helps to run a test case from a single file and with specific name.这有助于从单个文件和特定名称运行测试用例。

Looking into https://mochajs.org/#usage we see that simply use查看https://mochajs.org/#usage我们看到只需使用

mocha test/myfile摩卡测试/我的文件

will work.将工作。 You can omit the '.js' at the end.您可以省略末尾的“.js”。

For those who are looking to run a single file but they cannot make it work, what worked for me was that I needed to wrap my test cases in a describe suite as below and then use the describe title eg 'My Test Description' as pattern.对于那些希望运行单个文件但他们无法使其工作的人,对我有用的是我需要将我的测试用例包装在下面的描述套件中,然后使用描述标题,例如“我的测试描述”作为模式.

describe('My Test Description', () => {
  it('test case 1', () => {
    // My test code
  })
  it('test case 2', () => {
  // My test code
  })
})

then run然后运行

yarn test -g "My Test Description"

or要么

npm run test -g "My Test Description"

Not sure why the grep method is not working for me when using npm test.不知道为什么 grep 方法在使用 npm test 时对我不起作用。 This works though.这虽然有效。 I also need to specify the test folder also for some reason.由于某种原因,我还需要指定测试文件夹。

npm test -- test/sometest.js

Using Mocha's --fgrep (or just -f) you can select tests containing string, for example:使用Mocha 的--fgrep (或只是 -f),您可以选择包含字符串的测试,例如:

mocha -f 'my test x'

will run all tests containing my test x in either it() , describe() or context() blocks.将在it()describe()context()块中运行包含my test x所有测试。

Consolidate all your tests in one test.js file & your package json add scripts as:将所有测试合并到一个 test.js 文件中,并将您的包 json 添加脚本为:

  "scripts": {
  "api:test": "node_modules/.bin/mocha --timeout 10000 --recursive api_test/"
},

Type command on your test directory:在您的测试目录中键入命令:

npm run api:test

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM