从加载 phpunit 中排除某些测试

Excluding certain tests from loading in phpunit

我的一些测试用例使用自定义测试库。这些测试用例也非常慢。所以我想 运行 它们只在构建服务器中而不是在我的本地。我想 运行 在本地进行其他测试。

目录结构如下。 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个选项:

  1. 在您的 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

显然,您可以根据需要命名测试套件。

  1. 我认为更好的方法是:
  • 创建 phpunit.xml.dist 并配置 phpunit 的默认执行(对于 CI 服务器和所有刚刚克隆存储库的人)
  • 通过配置 phpunit 的本地执行来修改 phpunit.xml (通过将 <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.


部分链接:

The XML Configuration File. Test Suites

How to run a specific phpunit xml testsuite?