PHPUnit_Framework_Exception: PHPUnit_Framework_TestCase::$name 不能为空

PHPUnit_Framework_Exception: PHPUnit_Framework_TestCase::$name must not be null

我正在开发一个专为满足此目的而定制的 vagrant box。我的 PHPUnit 版本是 5.2.12,Laravel 版本是 5.2.22

当我执行 phpunit 命令时,出现以下错误:

PHPUnit_Framework_Exception: PHPUnit_Framework_TestCase::$name must not be null.

代码

以下是我的phpunit.xml内容:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="bootstrap/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="true"
         stopOnFailure="false"
         stderr="true">
    <testsuites>
        <testsuite name="Application Test Suite">
            <directory>./tests/</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist>
            <directory suffix=".php">app/</directory>
        </whitelist>
    </filter>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
    </php>
</phpunit>

所以问题基本上是覆盖了 __construct 方法:

class TestCase extends Illuminate\Foundation\Testing\TestCase
{
    public function __construct() 
    {
        //some code which should not be there
    }
}

删除构造函数后异常消失。

通过删除构造函数,您只是在避免错误,而不是解决错误。问题是,你正在扩展 PHPUnit_Framework_TestCase class,它有一个带有签名的构造函数:public function __construct($name = null, array $data = [], $dataName = ''). 看到问题了吗?它需要 $name、$data 和 $dataName,而你什么也没给它!

所以,不要删除构造函数,而是像这样重写它:

public function __construct($name = null, array $data = [], $dataName = '') {
    parent::__construct($name, $data, $dataName);

    // your constructor code goes here.
}

我遇到了同样的问题,这个完美解决了。