phpunit 没有执行任何测试

phpunit no tests executed

我有一个 'No tests executed' 和 phpunit..

这条线有效

$ phpunit install/InstallDbTest.php
...
<result expected>
...

$ cat suites/installtests.xml   
<phpunit>
    <testsuites>
        <testsuite name="database">
            <file>install/InstallDbTest.php</file>
        </testsuite>
    </testsuites>
</phpunit>

$ phpunit -c suites/installtests.xml 
PHPUnit 4.7.4 by Sebastian Bergmann and contributors.



Time: 130 ms, Memory: 11.25Mb

No tests executed!

有谁能告诉我我哪里做错了吗?

提前致谢!

您需要更正 phpunit.xml 中的路径。

正在尝试查找文件suites/install/InstallDbTest.php。由于该文件不存在,因此没有测试 运行,您会收到消息。

将配置更改为以下内容:

<phpunit>
    <testsuites>
        <testsuite name="database">
            <file>../install/InstallDbTest.php</file>
        </testsuite>
    </testsuites>
</phpunit>

phpunit.xml 中的文件路径都是相对于 xml 文件的位置。

可能在 InstallDbTest.php 文件中您缺少:

<?php
...

对于那些使用与 Composer 的自动加载文件不同的 bootstrap 文件的人,请确保您没有 use 您的其中一个测试用例 classes.

确实,PHPUnit(在我的例子中是 6.2 版)添加到 运行 TestSuite 所有 PHP 文件中 没有 class 已加载 (看看它是如何完成的 in the code)。来自 TestSuite::addTestFile($filename) 方法:

$classes = get_declared_classes();
$filename   = Fileloader::checkAndLoad($filename);
$newClasses = \array_diff(\get_declared_classes(), $classes);

因此,如果您的 class 已经属于 get_declared_classes,它将被忽略。

总结一下:不要 use 你的测试 class 之前 ;)