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


当前回答

你可以试试"it.only"

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

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

在这种情况下,只执行第一个

其他回答

尝试使用摩卡的——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

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

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

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

只在“describe”,“it”或“context”之前使用。我使用“$npm run test:unit”运行,它只执行带有.only的单元。

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

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

使用Mocha的——fgrep(或者-f)你可以选择包含字符串的测试,例如:

mocha -f 'my test x'

将在it()、describe()或context()块中运行包含我的测试x的所有测试。