为什么PHPUnit试图找到一个文件与测试套件的名称


why does PHPUnit try to find a file with the name of the testsuite?

我在我的phpunit.xml文件:

<phpunit ...>
    <testsuites>
        <testsuite name="MyTests">
            <directory>../path/to/some/tests</directory>
        </testsuite>
    </testsuites>
    ... // more settings for <filter> and <logging>
</phpunit>

当我去运行它时,我得到这个错误:

PHP fatal error: Uncaught exception 'PHPUnit_Framework_Exception'
with message 'Neither "MyTests.php" nor "MyTests.php" could be opened.'

为什么PHPUnit给我这个错误,为什么它寻找"MyTests.php",如果我给它一个目录,在其中寻找测试?

在一个相关的注意事项上,当我添加更多的<testsuite>条目与其他测试,PHPUnit运行没有错误。这是怎么回事?

默认情况下PHPUnit将添加"在*Test.php文件中找到的所有*Test类"(参见PHPUnit文档)。如果它没有找到任何与描述匹配的文件,(例如,一个SomeTest.php文件定义了一个SomeTest类),它就会返回到基于测试套件的name属性寻找一个文件。

解决方案是创建一个匹配该描述的文件,这样PHPUnit就不会回到默认的通过testsuite名称进行搜索:
<?php
// in ../path/to/some/tests/SomeTest.php:
class SomeTest extends PHPUnit_Framework_TestCase {
    public function test() {
        //... test cases here
    }
}
?>

现在你应该能够运行phpunit没有错误:

$ phpunit
PHPUnit 3.5.14 by Sebastian Bergmann.
.
Time: 0 seconds, Memory: 10.75Mb
OK (1 test, 0 assertions)

如果PHPUnit能够在其他套件下找到匹配的测试用例,那么当您添加更多testsuite条目时,它将没有错误地工作。如果它发现要在任何测试套件中运行的测试,它将不会求助于通过name属性来搜索它找不到任何内容的套件。

我认为问题在于您没有告诉它哪些文件包含您想要运行的测试用例和/或套件。尝试添加suffix="Test.php"属性。

<testsuite name="MyTests">
    <directory suffix="Test.php">../path/to/some/tests</directory>
</testsuite>