如何指定摩卡测试目录?

单元测试 Node.js

村村Tony

2020-03-23

Mocha尝试在test默认情况下查找测试文件,如何指定另一个目录,例如server-test

第2815篇《如何指定摩卡测试目录?》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

11个回答
伽罗 2020.03.23

对于更改测试目录,这似乎没有任何“轻松”支持。
但是,相对于您的问题,也许您应该看看这个问题。

神无小宇宙 2020.03.23

正如@superjos在评论中提到的那样

mocha --recursive "some_dir"

L西里 2020.03.23

如@ jeff-dickey所建议,在项目的根目录中,创建一个名为的文件夹test在该文件夹中,创建一个名为的文件mocha.opts现在,我在尝试改善Jeff的答案的地方,对我有用的是,而不是仅指定一个测试文件夹的名称,而是通过添加以下这一行,指定了一种模式来查找要在我的项目中运行的所有测试:

*/tests/*.js --recursivemocha.opts

If you instead want to specify the exact folders to look for tests in, I did something like this:

shared/tests/*.js --recursive
server/tests/graph/*.js --recursive

I hope this helps anyone who needed more than what the other answers provide

EvaDavaid 2020.03.23

如果您使用nodejs,请package.json在下方scripts

  1. 对于global (-g)安装:"test": "mocha server-test""test": "mocha server-test/**/*.js"对于子文档
  2. 对于project安装:"test": "node_modules/mocha/bin/mocha server-test""test": "node_modules/mocha/bin/mocha server-test/**/*.js"对于子文档

然后只需正常运行测试即可 npm test

宝儿理查德 2020.03.23

我在Windows 7上使用node.js v0.10.0和mocha v1.8.2和npm v1.2.14。我只是想让摩卡咖啡使用路径测试/单元来查找测试,花了很长时间并尝试了几项之后,

在Windows上无法使用“ test / unit / *。js”选项。出于很好的理由,Windows Shell不会像unixen一样扩展通配符。

但是,使用“测试/单位”确实可以工作,而无需使用文件模式。例如。“ mocha test / unit”运行在test / unit文件夹中找到的所有文件。

它仍然只运行一个文件夹文件作为测试,但是您可以传递多个目录名称作为参数。

同样,要运行单个测试文件,您可以指定完整路径和文件名。例如。“摩卡测试/单位/mytest1.js”

我实际上在package.json中设置了npm“ scripts”:{“ test”:“ mocha test / unit”},

这样,“ npm测试”就可以运行我的单元测试了。

Sam老丝 2020.03.23

我现在遇到了这个问题,并通过删除了--recursive选项(设置了该选项)并使用了上面建议的相同结构来解决了这个问题

mochify "test/unit/**/*.js"

/test/unit/为我运行了所有目录下的所有测试,而忽略了其中的其他目录/test/

猪猪猴子 2020.03.23

如果在node.js中则从Mocha v6开始有一些新配置

选项1:.mocharc.json在项目的根目录中创建

{
  "spec": "path/to/test/files"
}

选项2:mocha在项目的中添加属性package.json

{
  ...

  "mocha": {
    "spec": "path/to/test/files"
  }
}

更多选项在这里

猴子樱 2020.03.23

用这个:

mocha server-test

或者,如果您有子目录,请使用以下命令:

mocha "server-test/**/*.js"

请注意使用双引号。如果省略它们,则可能无法在子目录中运行测试。

Tom蛋蛋 2020.03.23

这样做的好方法是在package.json中添加一个“ test” npm脚本,该脚本使用正确的参数调用mocha。这样,您的package.json也可以描述您的测试结构。它还避免了其他答案(双引号与单引号,“查找”等)中的所有这些跨平台问题。

要使摩卡运行“ test”目录中的所有js文件:

"scripts": {
    "start": "node ./bin/www", -- not required for tests, just here for context
    "test": "mocha test/**/*.js"
  },

然后仅运行烟雾测试调用:

npm test

You can standardize the running of all tests in all projects this way, so when a new developer starts on your project or another, they know "npm test" will run the tests. There is good historical precedence for this (Maven, for example, most old school "make" projects too). It sure helps CI when all projects have the same test command.

Similarly, you might have a subset of faster "smoke" tests that you might want mocha to run:

"scripts": {
    "test": "mocha test/**/*.js"
    "smoketest": "mocha smoketest/**/*.js"
  },

Then to run only the smoke tests call:

npm smoketest

Another common pattern is to place your tests in the same directory as the source that they test, but call the test files *.spec.js. For example: src/foo/foo.js is tested by src/foo/foo.spec.js.

To run all the tests named *.spec.js by convention:

  "scripts": {
    "test": "mocha **/*.spec.js"
  },

Then to run all the tests call:

npm test

在这里看到图案吗?好。:)一致性打败了mura

樱小胖Mandy 2020.03.23

如果要通过仍然只mocha在命令行上运行但要在文件夹./server-tests而不是中运行测试来执行此操作,请在文件./test创建一个文件,./test/mocha.opts其中包含以下内容:

server-tests

如果要运行该文件夹和子目录中的所有内容,请将其放入 test/mocha.opts

server-tests
--recursive

mocha.opts 是通过命令行传递的参数,因此使第一行也就是您要更改测试的目录也将从 ./test/

DavaidTony宝儿 2020.03.23

不要使用-g或--grep选项,该模式以it()内的测试名称而不是文件系统为准。当前文档对此具有误导性和/或完全错误。要将整个命令限制为文件系统的一部分,可以将模式作为最后一个参数(不是标志)传递。

例如,此命令会将您的报告者设置为spec,但仅在server-test目录中立即测试js文件:

mocha --reporter spec server-test/*.js

此命令将执行与上述相同的操作,此外,它将仅运行测试的it()字符串/定义以“ Fnord:”开头的测试用例:

mocha --reporter spec --grep "Fnord:" server-test/*.js

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android