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. What's the best way to do this? Tools like mocha, and jest only seem to focus on one way of doing things.
我有兴趣创建完整的模拟单元测试,以及检查某些异步操作是否已正确返回的集成测试。我想要一个用于单元测试的命令和一个用于集成测试的命令,我可以在我的CI工具中单独运行它们。最好的方法是什么?像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?
思考?
2 个解决方案
#1
26
Mocha supports directories, file globbing and test name grepping which can be used to create "groups" of tests.
Mocha支持目录,文件通配和测试名称grepping,可用于创建测试的“组”。
Directories
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
File Prefix
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
Test Names
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 -g
然后使用mochas“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.
使用测试名称时保持文件或目录分离仍然很有用,这样您就可以轻松区分失败测试的源文件。
package.json
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脚本部分中,这样就可以很容易地运行像yarn test:int或npm run test:int。
{
scripts: {
"test": "mocha test/unit test/int",
"test:unit": "mocha test/unit",
"test:int": "mocha test/int"
}
}
#2
-2
mocha does not support label or category. You understand correctly. You must create two folders, unit and integration, and call mocha like this
mocha不支持标签或类别。你理解正确。您必须创建两个文件夹,单元和集成,并像这样调用mocha
mocha unit mocha integration
摩卡单位摩卡整合
#1
26
Mocha supports directories, file globbing and test name grepping which can be used to create "groups" of tests.
Mocha支持目录,文件通配和测试名称grepping,可用于创建测试的“组”。
Directories
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
File Prefix
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
Test Names
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 -g
然后使用mochas“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.
使用测试名称时保持文件或目录分离仍然很有用,这样您就可以轻松区分失败测试的源文件。
package.json
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脚本部分中,这样就可以很容易地运行像yarn test:int或npm run test:int。
{
scripts: {
"test": "mocha test/unit test/int",
"test:unit": "mocha test/unit",
"test:int": "mocha test/int"
}
}
#2
-2
mocha does not support label or category. You understand correctly. You must create two folders, unit and integration, and call mocha like this
mocha不支持标签或类别。你理解正确。您必须创建两个文件夹,单元和集成,并像这样调用mocha
mocha unit mocha integration
摩卡单位摩卡整合