更新:发现了一些问题和解决方案;请参考我回答的结尾
昨天我发现自己和你的情况一样。我正在从像这样以套件为中心的结构重构我们的测试套件
tests/
unit/
Module1/
Module2/
... (more modules)
integration/
Module1/
Module2/
... (more modules)
... (more suites)
像这样的以模块为中心的新结构
tests/
Module1/
Unit/
Integration/
... (more suites)
Module2/
Unit/
Integration/
... (more suites)
... (more modules)
但在我看来 Codeception 不 支持这一点。在搜索和调试Codeception代码后,我找到了这些信息。
解决方法
这里
https://github.com/Codeception/Codeception/issues/5486
用户 LeeShan8 似乎也出现了同样的问题,用户 vertexvaar 谈到了他制作的一个补丁,该补丁提供了一个 --recurse-include 选项来在每个子模块中运行一个特定的套件(补丁可以是从那里下载)。
所以我从 vertexvaar 搜索了 PR,以了解他的补丁发生了什么。这是 PR https://github.com/Codeception/Codeception/pull/5737,但由于 SamMousa 不活动而被关闭。
SamMousa 指出了一种解决方法:--skip 选项。
如果您想在每个子模块中从 unit 套件运行测试,您可以这样做:
codecept run -s suite1 -s suite2 ...
其中suite1、suite2 ...,都是您的测试套件不包括unit。如果您跳过所有其他套件,Codeception 将在每个子模块中运行剩余的套件(在本例中为 unit)。
我的解决方案
我真的不想通过在--skip 选项中列出所有其他套件来运行unit 套件。太无聊了,写起来很慢而且容易出错。
我真的很想指定 codecept run unit 并查看所有单元测试的运行,一个模块一个模块。所以我创建了一个小补丁 (https://gist.github.com/mpallante/101b1508ffc5a2c4bf30d3344437ca0b) 来修复此行为。
它类似于 vertexvar 但是 为了简单起见,我没有添加新选项。它只是从每个子模块运行指定的套件。
我还使用composer-patches (https://github.com/cweagans/composer-patches) 自动化了补丁应用程序。
我安装了composer-patches
composer require cweagans/composer-patches
然后将补丁放入patches/allow-codeception-run-suites-from-submodules.patch 并更改我的composer.json 以包含补丁(在"extra" 部分):
...
"extra": {
...
"patches": {
"codeception/codeception": {
"Allow Codeception to run suites from submodules": "patches/allow-codeception-run-suites-from-submodules.patch"
}
}
},
...
最后,我运行了composer update codeception/codeception:它将删除代码接收,重新安装它,然后应用补丁:
$ php which composer update codeception/codeception
Gathering patches for root package.
Removing package codeception/codeception so that it can be re-installed and re-patched.
- Removing codeception/codeception (4.1.7)
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
Gathering patches for root package.
Gathering patches for dependencies. This might take a minute.
- Installing codeception/codeception (4.1.7): Loading from cache
- Applying patches for codeception/codeception
patches/allow-codeception-run-suites-from-submodules.patch (Allow Codeception to run suites from submodules)
注意
请注意,这个解决方案在我的情况下似乎有效,但尚未经过全面测试。我通常以很少的方式运行 Codeception(单个套件、所有套件、单个测试文件),所以不知道是否存在冲突。
但这可能是一个起点。
也许在未来的某个时候,我可以将这个补丁贡献给项目本身。
更新
我发现了一个小问题:当一个套件在子模块中定义时,但不是全局,codecept run 会跳过它。
我通过在全局 codeception.yml 配置文件中定义套件来解决这个问题:
include:
- modules/*/Tests
suites:
integration:
unit:
paths:
...
然后,在每个子模块中,我定义一个 ./modules/Mod1/Tests/codeception.yml 文件,如下所示:
suites:
unit:
path: ./Unit
modules:
enabled:
- Asserts
- \Helper\Unit
integration:
path: ./Integration
modules:
enabled:
- \Helper\Integration
namespace: Modules\Mod1\Tests
paths:
tests: .
output: ../../../tests/_output
support: ../../../tests/_support
data: ./_data
希望这会有所帮助。