如何配置Phpunit以在ZF2应用程序中测试整个供应商文件夹



我正在开发具有通用文件夹结构的Zend Framework 2应用程序,因此文件夹/vendor包含所有(项目外部)库。设置单元测试环境,我希望能够运行所有供应商测试。文件夹结构不同,具体取决于库。有些软件包根本没有测试。

可能的解决方案是创建一个测试套件"供应商",并手动定义每个测试文件夹的路径,例如:

phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit ...>
    <testsuites>
        <testsuite name="vendor">
            <directory>../vendor/lib-foo/path/to/tests</directory>
            <directory>../vendor/package-bar/path/to/tests</directory>
            ...
        </testsuite>
        ...
    </testsuites>
    ...
</phpunit>

我不喜欢这个解决方案。首先,因为那时我必须手动处理每个软件包。

另一个解决方案是将/vendor定义为测试文件夹:

phpunit.xml

<?xml version="1.0" encoding="UTF-8"?>
<phpunit ...>
    <testsuites>
        <testsuite name="vendor">
            <directory>../vendor</directory>
            ...
        </testsuite>
        ...
    </testsuites>
    ...
</phpunit>

好吧,但是随后phpunit必须扫描很多文件夹,这些文件夹不需要,测试将需要更多的时间。

是否有更好的解决方案,可以使过程自动化并避免大量手动配置?

可能很难在单个测试运行中运行所有Phpunit供应商测试套件。一个问题是,每个不同的测试套件都可以运送自己的配置文件,甚至需要自定义 bootstrap 配置文件。使用一个命令运行所有测试套件时,您无法介绍一下。

我可能会为此使用一些外壳魔术。请注意,此示例依赖于第三方软件包中的每个Party软件包中的phpunit.xml(.dist)文件(对于大多数库,这是一个合理的假设)。您甚至可以将其集成到连续的集成过程中以连续测试:

for FILE in $(find . -name 'phpunit.xml*') ; do
    sh -c 'cd '$(dirname $FILE)' && composer install'
    vendor/bin/phpunit -c $FILE
done

最新更新