【问题标题】:How to run tests for a specific suite from included module configurations?如何从包含的模块配置中运行特定套件的测试?
【发布时间】:2020-10-08 08:30:57
【问题描述】:

我的项目根文件夹中有一个 codeception.yml。 这对我的不同模块的其他代码接收 yml 有一个包含配置指令

include:
    - tests/Modules/*

这在 codeception 文档中被描述为多应用程序设置

https://codeception.com/docs/08-Customization#One-Runner-for-Multiple-Applications

这些子配置在他们自己的 codeception.yml 中描述了不同的套件配置,就像

中的这个

测试/模块/前端/codeception.yml

suites:
    Presentation:
        path: Presentation
        class_name: PresentationTester
        modules:
...

问题:在我的项目根文件夹中,我无法通过

为所有包含的模块运行特定套件
vendor/bin/codecept run Presentation

上面写着

找不到套件“演示文稿”

运行它指向某个配置确实有效

vendor/bin/codecept run Presentation -c tests/Modules/Frontend

但它只运行这个模块。我想用一个命令运行所有模块的某些套件。

运行组 (-g) 也可以,因此这表明配置已正确加载。

为什么这不适用于套件?所有配置都包含在我的主要 yml 中,因此我认为必须找到套件。

当没有任何套件可以从中心点运行时,这样的设置有什么意义?

有什么想法吗?

【问题讨论】:

    标签: php unit-testing phpunit codeception


    【解决方案1】:

    更新:发现了一些问题和解决方案;请参考我回答的结尾

    昨天我发现自己和你的情况一样。我正在从像这样以套件为中心的结构重构我们的测试套件

    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 ...
    

    其中suite1suite2 ...,都是您的测试套件不包括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
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2011-12-06
      • 2015-11-27
      • 2011-04-09
      • 1970-01-01
      • 2014-09-20
      • 2015-01-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多