phpunit - 排除某些测试加载到 phpunit 中

标签 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

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

  • 我认为更好的方法是:
    • 创建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.

    <小时/>

    一些链接:

    The XML Configuration File. Test Suites

    How to run a specific phpunit xml testsuite?

    关于phpunit - 排除某些测试加载到 phpunit 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38588029/

    相关文章:

    phpUnit 与 Symfony

    php - yii phpunit getMockBuilder 不使用 --filter

    testing - Symfony 功能测试 : receive uploaded file

    php - Symfony 3 设置测试数据库

    php - 如何使用 PHPUnit 的模拟测试名为 "method()"的方法?

    php - 拉维尔 5.6。 PHP单元。顺序不敏感的 assertJson() 方法

    PHPUnit 错误调用模拟上的未定义方法

    php - PHPunit 的 CodeCoverage 未生成

    php - Laravel 工厂没有返回正确的对象数据