简体   繁体   English

为什么 PHPUnit 会尝试查找名称为 testsuite 的文件?

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

I have this in my phpunit.xml file:我的 phpunit.xml 文件中有这个:

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

And when I go to run it, I get this error:当我 go 运行它时,我收到此错误:

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

Why does PHPUnit give me this error, and why is it looking for "MyTests.php" if I gave it a directory in which to look for tests?为什么 PHPUnit 会给我这个错误,如果我给它一个目录来寻找测试,为什么它会寻找“MyTests.php”?

And on a related note, when I add more <testsuite> entries with other tests, PHPUnit runs without error.并且在相关说明中,当我在其他测试中添加更多<testsuite>条目时,PHPUnit 运行没有错误。 What's up with that?那是怎么回事?

By default PHPUnit will add "all *Test classes that are found in *Test.php files" (see PHPUnit docs ).默认情况下 PHPUnit 将添加“在*Test.php文件中找到的所有*Test类”(参见PHPUnit 文档)。 If it doesn't find any files matching that description, (eg a SomeTest.php file defining a SomeTest class), it falls back to looking for a file based on the name attribute of the test suite.如果它没有找到与该描述匹配的任何文件(例如,定义SomeTest类的SomeTest.php文件),它会回退到基于测试套件的名称属性查找文件。

The solution is to create a file matching that description so that PHPUnit doesn't fall back to its default of searching by the testsuite name:解决方案是创建一个与该描述匹配的文件,以便 PHPUnit 不会退回到默认的按测试套件名称搜索:

<?php
// in ../path/to/some/tests/SomeTest.php:
class SomeTest extends PHPUnit_Framework_TestCase {
    public function test() {
        //... test cases here
    }
}
?>

Now you should be able to run phpunit without errors:现在您应该能够运行phpunit而不会出现错误:

$ phpunit
PHPUnit 3.5.14 by Sebastian Bergmann.

.

Time: 0 seconds, Memory: 10.75Mb

OK (1 test, 0 assertions)

It will work without errors when you add more testsuite entries if PHPUnit is able to find matching test cases to run under those other suites.如果 PHPUnit 能够找到匹配的测试用例以在其他套件下运行,则添加更多testsuite条目时它将正常工作。 If it finds tests to run in any testsuite, it won't resort to searching by the name attribute for the suites it couldn't find anything for.如果它找到要在任何测试套件中运行的测试,它不会求助于通过name属性搜索它找不到任何东西的套件。

I believe the problem is that you're not telling it which files contain the test cases and/or suites you want to run.我认为问题在于您没有告诉它哪些文件包含您要运行的测试用例和/或套件。 Try adding the suffix="Test.php" attribute.尝试添加suffix="Test.php"属性。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM