如何配置PHPUnit来测试ZF2应用程序中的整个供应商文件夹?

时间:2022-03-02 13:56:02

I'm developing a Zend Framework 2 application with a common folder structure, so that the folder /vendor contains all (project external) libraries. Setting up the unit testing environment I would like to be able to run all vendor tests. The folders structures are different depending on the library. Some packages have no tests at all.

我正在开发一个具有通用文件夹结构的Zend Framework 2应用程序,以便文件夹/供应商包含所有(项目外部)库。设置单元测试环境我希望能够运行所有供应商测试。文件夹结构因库而异。有些包没有任何测试。

A possible solution would be to create a test suite "vendor" and manually define there the paths to every single test folder, e.g.:

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

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>

I don't like this solution. First of all because then I'd have to handle every package manually.

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

Another solution would be to define /vendor as test folder:

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

phpunit.xml

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

Well, but then PHPUnit has to scan a lot of folders, that it doesn't need, and the tests will need much more time.

好吧,但是PHPUnit必须扫描很多文件夹,它不需要,测试需要更多的时间。

Is there a better solution, that would make possible to automate the process and avoid much manual configuration?

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

1 个解决方案

#1


It would probably be difficult to run all PHPUnit vendor test suites with a single test run. One issue is that each of the different test suites might ship its own configuration file or even require a custom bootstrap configuration file. You cannot cover that when running all test suites with a single command.

通过单次测试运行可能很难运行所有PHPUnit供应商测试套件。一个问题是每个不同的测试套件可能会发布自己的配置文件,甚至需要自定义引导程序配置文件。使用单个命令运行所有测试套件时无法涵盖这一点。

I'd probably use some shell magic for this. Note that this example relies on the presence of a phpunit.xml(.dist) file in each of your 3rd party packages (for most libraries that's a reasonable assumption). You could even integrate this into your continuous integration process to test this continuously:

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

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

#1


It would probably be difficult to run all PHPUnit vendor test suites with a single test run. One issue is that each of the different test suites might ship its own configuration file or even require a custom bootstrap configuration file. You cannot cover that when running all test suites with a single command.

通过单次测试运行可能很难运行所有PHPUnit供应商测试套件。一个问题是每个不同的测试套件可能会发布自己的配置文件,甚至需要自定义引导程序配置文件。使用单个命令运行所有测试套件时无法涵盖这一点。

I'd probably use some shell magic for this. Note that this example relies on the presence of a phpunit.xml(.dist) file in each of your 3rd party packages (for most libraries that's a reasonable assumption). You could even integrate this into your continuous integration process to test this continuously:

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

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