我的一些测试用例使用自定义测试库。而且这些测试用例非常慢。因此,我只想在构建服务器中运行它们,而不要在本地服务器中运行它们。我想在本地运行其他测试。

以下是目录结构。 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

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

2)我认为更可取的方法是:
  • 创建phpunit.xml.dist并配置phpunit的默认执行(对于CI服务器和所有刚克隆存储库的用户)
  • 通过配置本地phpunit执行来修改phpunit.xml(通过将<exclude>tests/unit-tests/slow</exclude>添加到默认值
    测试套件)
  • 从版本控制中排除phpunit.xml

  • docs:



    一些链接:

    The XML Configuration File. Test Suites

    How to run a specific phpunit xml testsuite?

    10-05 20:31
    查看更多