[英]Separation of unit tests and integration tests
I'm interested in creating full mocked unit tests, as well as integration tests that check if some async operation has returned correctly.我有兴趣创建完整的模拟单元测试,以及检查某些异步操作是否正确返回的集成测试。 I'd like one command for the
unit
tests and one for the integration
tests that way I can run them separately in my CI tools.我想要一个用于
unit
测试的命令和一个用于integration
测试的命令,这样我就可以在我的 CI 工具中单独运行它们。 What's the best way to do this?做到这一点的最佳方法是什么? Tools like mocha, and jest only seem to focus on one way of doing things.
像 mocha 和 jest 这样的工具似乎只专注于一种做事方式。
The only option I see is using mocha and having two folders in a directory.我看到的唯一选择是使用 mocha 并在一个目录中有两个文件夹。
Something like:类似的东西:
__unit__
__integration__
Then I'd need some way of telling mocha to run all the __unit__
tests in the src
directory, and another to tell it to run all the __integration__
tests.然后我需要某种方式告诉 mocha 运行
src
目录中的所有__unit__
测试,另一种方式告诉它运行所有__integration__
测试。
Thoughts?想法?
Mocha supports directories, file globbing and test name grepping which can be used to create "groups" of tests. Mocha 支持目录、文件通配和测试名称grepping ,可用于创建测试“组”。
test/unit/whatever_spec.js
test/int/whatever_spec.js
Then run tests against all js
files in a directory with然后对目录中的所有
js
文件运行测试
mocha test/unit
mocha test/int
mocha test/unit test/int
test/unit_whatever_spec.js
test/int_whatever_spec.js
Then run mocha against specific files with然后针对特定文件运行 mocha
mocha test/unit_*_spec.js
mocha test/int_*_spec.js
mocha
Create outer blocks in mocha that describe the test type and class/subject.在 mocha 中创建描述测试类型和类/主题的外部块。
describe('Unit::Whatever', function(){})
describe('Integration::Whatever', function(){})
Then run the named blocks with mochas "grep" argument --grep
/ -g
然后使用 mochas "grep" 参数运行命名块
--grep
/ -g
mocha -g ^Unit::
mocha -g ^Integration::
mocha
It is still useful to keep the file or directory separation when using test names so you can easily differentiate the source file of a failing test.在使用测试名称时保持文件或目录分隔仍然很有用,这样您就可以轻松区分失败测试的源文件。
Store each test command in your package.json scripts
section so it's easy to run with something like yarn test:int
or npm run test:int
.将每个测试命令存储在您的 package.json
scripts
部分,以便使用诸如yarn test:int
或npm run test:int
类的东西很容易运行。
{
scripts: {
"test": "mocha test/unit test/int",
"test:unit": "mocha test/unit",
"test:int": "mocha test/int"
}
}
mocha does not support label or category. mocha 不支持标签或类别。 You understand correctly.
你理解正确。 You must create two folders, unit and integration, and call mocha like this
必须创建两个文件夹,unit和integration,像这样调用mocha
mocha unit mocha integration摩卡单元摩卡集成
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.