在 phpunit 中从加载中排除某些测试 [英] Excluding certain tests from loading in phpunit

查看:78
本文介绍了在 phpunit 中从加载中排除某些测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Some of my testcases use a custom testing library. Also these testcases are very slow. So I would like to run them only in the build server and not in my local. I want to run the other tests locally.

以下是目录结构.slow 目录中的那些是应该排除的慢测试用例.

Following is the directory structure. The ones inside the slow directory are the slow test cases that should be excluded.

/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 注释创建组.这有效,但问题是这些测试文件正在加载(尽管测试未执行).由于他们需要本地未安装的测试库,因此出现错误.

I tried creating groups using @group annotation. This works, but the issue is that these test files are getting loaded (tests not executed though). Since they require the test library which is not installed locally, it is giving an error.

创建 phpunit.xml 配置以便默认排除(甚至不加载)这些缓慢的测试并且可以在需要时执行的最佳方法是什么?

What is the best way to create the phpunit.xml configuration such that these slow tests are excluded (and not even loaded) by default and can be executed if needed?

推荐答案

有 2 个选项:

1) 在您的 phpunit.xml 中创建 2 套测试套件 - 一套用于 CI 服务器,一套用于本地开发

1) In your phpunit.xml create 2 test suits - one for CI server and one for local development

<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 服务器上运行

So on CI server you can run

phpunit --testsuite all_tests

本地

phpunit --testsuite only_fast_tests

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

Obviously, you can name test suites as you want.

2) 我认为最好的方法是:

2) I think preferable way is:

  • 创建 phpunit.xml.dist 并配置 phpunit 的默认执行(适用于 CI 服务器和所有刚刚克隆存储库的人)
  • 通过配置本地执行phpunit来修改phpunit.xml(通过添加 tests/unit-tests/slow 到默认测试套件)
  • 从版本控制中排除 phpunit.xml.
  • Create phpunit.xml.dist and configure default execution of phpunit (for CI server and all those who just cloned repository)
  • Modify phpunit.xml by configuring your local execution of phpunit (by adding <exclude>tests/unit-tests/slow</exclude> to default testsuite)
  • Exclude phpunit.xml from version control.

来自文档:

如果 phpunit.xml 或 phpunit.xml.dist(按此顺序)存在于当前工作目录和 --configuration 未使用,配置将自动从该文件中读取.

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.

<小时>

一些链接:


Some links:

XML 配置文件.测试套件

如何运行特定的 phpunit xml 测试套件?

这篇关于在 phpunit 中从加载中排除某些测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆