简体   繁体   English

PHPUnit 配置扩展以运行特定的测试套件

[英]PHPUnit configure extension to run for a specific test suite

I want to run a PHPUnit test runner extension for a specific test suite only, but I did not find a way to achieve that.我只想为特定的测试套件运行 PHPUnit 测试运行器扩展,但我没有找到实现这一目标的方法。

I know that extensions can be configured using arguments , however, it seems to be no way to retrieve the current test suite.我知道可以使用 arguments 配置扩展,但是,似乎无法检索当前的测试套件。

We can also take a broader view and may want to retrieve the options passed to the PHPUnit command-line test runner.我们还可以采取更广泛的观点,并可能希望检索传递给 PHPUnit 命令行测试运行程序的选项。

My phpunit.xml file is configured as following (simplified for the example purpose):我的phpunit.xml文件配置如下(为示例目的而简化):

    <phpunit>
        <testsuites>
            <testsuite name="unit">
                <directory>ci/unit</directory>
            </testsuite>
            <testsuite name="unit-with-extension">
                <directory>ci/unit-with-extension</directory>
            </testsuite>
        </testsuites>
        <extensions>
            <extension class="Extensions\CustomExtension"/>
        </extensions>
    </phpunit>

The custom extension:自定义扩展:

declare(strict_types=1);

namespace Extensions\CustomExtension;

use PHPUnit\Runner\AfterLastTestHook;
use PHPUnit\Runner\BeforeFirstTestHook;

class CustomExtension implements AfterLastTestHook, BeforeFirstTestHook
{
    public function executeBeforeFirstTest(): void
    {
        ...
    }

    public function executeAfterLastTest(): void
    {
        ...
    }
}

Finally, the test suite is run using:最后,测试套件使用以下命令运行:

phpunit --unit-with-extension

Thank you for your assistance!谢谢您的帮助!

Well, there is a reason to have a configuration file.好吧,有一个配置文件是有原因的。

Or two.或者两个。 (Or three and so on). (或三个等等)。

Honestly, the most straight forward way is to invoke the test-runner with the appropriate configuration file and then you can have all the variants you're looking for.老实说,最直接的方法是使用适当的配置文件调用测试运行程序,然后您就可以拥有您正在寻找的所有变体。

Introduce a Makefile or composer scripts so you have some book-keeping on how you invoke the different configurations and have them at your fingertips.引入一个Makefile或 composer 脚本,这样您就可以记录如何调用不同的配置并让它们触手可及。

If you want to extend, Phpunit is pure php and you could fiddle with superglobals and your own bootstrapping - perhaps on runner extension level - but this is not making anything more easy nor portable or your tests any better (very most likely, no offence please).如果你想扩展,Phpunit 是纯 php,你可以摆弄超全局变量和你自己的引导程序——也许是在运行器扩展级别——但这并没有使任何事情变得更容易、更便携,或者你的测试变得更好(很可能,请不要冒犯)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM