首页 > 解决方案 > 为什么 PHPUnit 没有启动我的自定义测试?

问题描述

PHPUnit doc中所述,您可以实现PHPUnit\Framework\Test编写自定义测试。

听起来不错,但是如何将这些测试作为我的测试套件的一部分启动?

给定以下测试目录:

+tests/
   |- NormalTestExtendingTestCaseTest.php
   |- CustomTestImplementingTest.php
phpunit.xml

还有我的 phpunit.xml 文件:

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.3/phpunit.xsd"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
>
    <testsuites>
        <testsuite name="My Test Suite">
            <directory>./tests</directory>
        </testsuite>
    </testsuites>
</phpunit>

只有 NormalTestExtendingTestCaseTest.php 由 PHPUnit 执行。


CustomTestImplementingTest.php

<?php
use PHPUnit\Framework\TestCase;

class CustomTestImplementingTest implements PHPUnit\Framework\Test
{

    public function count()
    {
        return 1;
    }

    public function run(PHPUnit\Framework\TestResult $result = null)
    {
        if ($result === null) {
            $result = new PHPUnit\Framework\TestResult;
        }

        $result->startTest($this);
        // do something
        $result->endTest($this, $stopTime);

        return $result;
    }
}

标签: phpunit

解决方案


检查您的类是否具有正确的继承、正确的名称方法(以“test”开头)和正确的命名空间。

您可能必须运行 composer dump-autoload。


推荐阅读