alorel/phpunit-auto-rerun

此包已被废弃,不再维护。没有建议的替代包。

允许失败的 PHPUnit 测试自动重试。

0.2 2017-06-23 21:56 UTC

This package is not auto-updated.

Last update: 2020-01-24 16:09:48 UTC


README

Average time to resolve an issue Percentage of issues still open Build Status codecov Dependency Status Reference Status HHVM Status

Latest Stable Version Total Downloads License

允许失败的 PHPUnit 测试自动重试。

使用场景

任何存在外部依赖且无法完全控制的地方,例如开发某个 API 的 SDK,可能适用速率限制或经历短暂的停机时间。

要求

  • 在 HHVM 上测试过
  • PHP 5.6 或 7.0+
  • 在 PHPUnit 5.4 (require-dev ^5.0) 上测试过,但可能在旧版本上运行(更新您的安装!)

安装

composer require --dev alorel/phpunit-auto-rerun

使用

您可以通过两种方式之一将重试功能集成到测试中:使用类继承或使用 traits。

在许多情况下,您可以简单地从 PHPUnit_Retriable_TestCase 继承您的测试用例,它本身继承自原始的 PHPUnit_Framework_TestCase。在这里,您需要做的就是替换测试用例中的类

<?php

    namespace Some\Thing\Or\Another;

    class PHPUnitReflectionTest extends \PHPUnit_Framework_TestCase {

将会变成

<?php

    namespace Some\Thing\Or\Another;

    class PHPUnitReflectionTest extends \PHPUnit_Retriable_TestCase {

在测试用例已经具有复杂继承的情况下,您只需在需要重试功能的测试用例中使用 traits

<?php

    namespace Some\Thing\Or\Another;

    class MyTestCase extends MyTestBase {

        use \PHPUnit_Retriable_TestCase_Trait;

    }

修改现有测试用例/恢复

PHPUnit_Retriable_TestCase 类被故意留在全局命名空间中,以便尽可能多地类似于原始的 PHPUnit_Framework_TestCase。大多数 IDE 都会生成/建议一个测试用例模板来包含

class PHPUnitReflectionTest extends \PHPUnit_Retriable_TestCase

因此,您可以在任一方向上安全地进行测试范围内的搜索和替换操作。

配置

配置通过注解执行,与配置预期异常、@before 等方式相同

  • @retryCount 指定测试用例将运行的最多次数。将其设置为 0 将简单调用原始测试运行器,将其设置为 1 将运行测试而不进行重试(等效功能),将其设置为 5 将运行一次,在失败时重试最多 4 次。
  • @sleepTime 指定脚本在重试之间将休眠多少秒。

这两个值默认为 0

可以通过注解类和测试方法来执行配置 - 类注解应用于所有方法,并由方法注解覆盖,例如,考虑以下片段

<?php

    /**
     * @retryCount 5
     */
    class SomeTest extends PHPUnit_Retriable_TestCase {

        function testOne() {
            //Will inherit the default sleep time of 0
            //and SomeTest's retry count of 5
        }

        /**
         * @retryCount 0
         */
        function testTwo() {
            //sleep time 0 (default)
            //retry count 0 (overrides SomeTest)
        }

        /**
         * @sleepTime 10
         */
        function testThree() {
            //sleep time 10 (overrides default)
            //retry count 5 (inherit from SomeTest)
        }

        /**
         * @sleepTime 3
         * @retryCount 3
         */
        function testFour() {
            //sleep time 3 (overrides default)
            //retry count 3 (overrides SomeTest)
        }
    }

常见问题解答 *

Q: 这会与 @dataProvider 注解一起工作吗? A: 是的 - 测试将使用相同的数据提供者值继续重试,当测试成功时继续到下一个。这适用于数组数据提供者和生成器/迭代器。

问题:为什么我现在的断言数量比以前多?答案:即使测试失败,断言数量仍然会增加——这并不是很明显如何更改,我认为这不是一个重要的功能,因此我没有费心实现。

问题:我可以通过CLI/phpunit.xml设置配置参数吗?答案:不可以,而且看起来没有编辑原始PHPUnit代码就无法实现。

* 我在发布之前把这些放在一起,没有人问过。对此表示歉意。😞