我的一些测试用例使用自定义测试库。而且这些测试用例非常慢。所以我想只在构建服务器中运行它们,而不是在我的本地服务器中运行它们。我想在本地运行其他测试。
以下是目录结构。 slow
目录中的那些是应该排除的慢测试用例。
/tests/unit-tests/test-1.php
/tests/unit-tests/test-2.php
/tests/unit-tests/slow/test-1.php
/tests/unit-tests/slow/test-2.php
/tests/unit-tests/foo/test-1.php
/tests/unit-tests/bar/test-2.php
我尝试使用@group
注释创建组。这是可行的,但问题是这些测试文件正在加载(但测试未执行)。由于它们需要本地未安装的测试库,因此会出现错误。
创建 phpunit.xml 配置的最佳方法是什么,以便默认情况下排除(甚至不加载)这些缓慢的测试,并且可以在需要时执行?
最佳答案
有 2 个选项:
- 在您的
phpunit.xml
中创建 2 个测试套件 - 一套用于 CI 服务器,一套用于本地开发
<testsuites>
<testsuite name="all_tests">
<directory>tests/unit-tests/*</directory>
</testsuite>
<testsuite name="only_fast_tests">
<directory>tests/unit-tests/*</directory>
<!-- Exclude slow tests -->
<exclude>tests/unit-tests/slow</exclude>
</testsuite>
</testsuites>
所以你可以在 CI 服务器上运行
phpunit --testsuite all_tests
本地
phpunit --testsuite only_fast_tests
显然,您可以根据需要命名测试套件。
- 我认为更好的方法是:
- 创建
phpunit.xml.dist
并配置 phpunit 的默认执行(对于 CI 服务器和所有刚刚克隆存储库的人) - 修改
phpunit.xml
通过配置 phpunit 的本地执行 (通过将<exclude>tests/unit-tests/slow</exclude>
添加到默认值 测试套件) - 排除
phpunit.xml
来自版本控制。
来自docs :
<小时/>If phpunit.xml or phpunit.xml.dist (in that order) exist in the current working directory and --configuration is not used, the configuration will be automatically read from that file.
一些链接:
关于phpunit - 排除某些测试加载到 phpunit 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38588029/