开发者

PHPUnit reports a PHPUnit_Framework_Exception

开发者 https://www.devze.com 2023-04-07 07:12 出处:网络
I added a simple test to a bundle. As suggested in the manual I tried to have PHPUnit load the configuration with:

I added a simple test to a bundle.

As suggested in the manual I tried to have PHPUnit load the configuration with:

phpunit -c /app

phpunit.xml looks like this:

<?xml version="1.0" encoding="UTF-8"?>

<!-- http://www.phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit
    backupGlobals               = "false"
    backupStaticAttributes      = "false"
    colors                      = "true"
    convertErrorsToExceptions   = "true"
    convertNoticesToExceptions  = "true"
    convertWarningsToExceptions = "true"
    processIsolation            = "false"
    stopOnFailure               = "false"
    syntaxCheck                 = "false"
    bootstrap                   = "bootstrap.php.cache" >

    <testsuites>
        <testsuite name="Project Test Suite">
            <directory>../src/*Bundle/Tests</directory>
        </testsuite>
    </testsuites>
</phpunit> 

The error message I get is:

root@h0x03:/var/www/fi/FrontendIntegrator# phpunit -c app/
PHP Fatal error:  Uncaught exception 'PHPUnit_Framework_Exception' with message 'Neither "Project Test Suite.php" nor "Project Test Suite.php" could be opened.' in /usr/share/php/PHPUnit/Util/Skeleton/Test.php:102
Stack trace:
#0 /usr/share/php/PHPUnit/TextUI/Command.php(157): PHPUnit_Util_Skeleton_Test->__construct('Project Test Su...', '')
#1 /usr/share/php/PHPUnit/TextUI/Command.php(129): PHPUnit_TextUI_Command->run(Array, true)
#2 /usr/bin/phpunit(49): PHPUnit_TextUI_Command::main()
#3 {main}
  thrown in /usr/share/php/PHPUnit/Util/Skeleton/Test.php on line 102

PHPUnit apparently loads 开发者_开发问答the file and tries to find a PHP-file named after the test suites name. I cannot find any information on why it does so or how such a file should look like.


It seems like PHPUnit tries to find a file named after the name-attribute of the testsuite-tag

  1. if it either cannot find any tests (wrong naming or wrong directory) or

  2. if there is a fatal error occuring in a test-script; which occures so early that PHPUnit cannot identify the content of that file as a test - as no class could be loaded up until that point.


I had the same problem today testing my controllers. Turns out just following the Book's command for it doesn't work, since PHPUnit can't find the test classes. I managed to find a workaround, just specify the test file you'd like tested, and it'll work.

phpunit -c app src/Acme/DemoBundle/Tests/Utility/CalculatorTest.php


It happened to me that I had one extra directory level in my bundles. Instead of MyVendorName\MyBundleNameBundle I had MyVendorName\MyProject\MyBundleNameBundle

The default directories within phpunit.xml.dist, in the section testsuites are those:

<testsuites>
    <testsuite name="Project Test Suite">
        <directory>../src/*/*Bundle/Tests</directory>
        <directory>../src/*/Bundle/*Bundle/Tests</directory>
    </testsuite>
</testsuites>

but none did match my bundle namespace: Xmontero\Emperors\AdminBundle whose tests, in turn, were in the directory src/Xmontero/Emperors/AdminBundle/Tests.

In addition ALL my bundles had that syntax and I removed any bundle with the other syntax (like Acme\DemoBundle). As a result, phpunit did not find any test.

I solved like this: just below this line in phpunit.xml.dist

<directory>../src/*/*Bundle/Tests</directory>

I added this line

<directory>../src/*/*/*Bundle/Tests</directory>

so it reads:

<testsuites>
    <testsuite name="Project Test Suite">
        <directory>../src/*/*Bundle/Tests</directory>
        <directory>../src/*/*/*Bundle/Tests</directory>
        <directory>../src/*/Bundle/*Bundle/Tests</directory>
    </testsuite>
</testsuites>

...and everything worked fine ;)

-- EDIT 1 day later --

It gives me now report on coverage about the test itself, when running phpunit -c app --coverage-html myNiceDir - when I expect it to give me only the coverage of the system under test.

This is because there is a whitelist that follows the same pattern, and there is something to be added in the exclude list:

Where it read this:

<exclude>
    <directory>../src/*/*Bundle/Resources</directory>
    <directory>../src/*/*Bundle/Tests</directory>
    <directory>../src/*/Bundle/*Bundle/Resources</directory>
    <directory>../src/*/Bundle/*Bundle/Tests</directory>
</exclude>

I changed into this:

<exclude>
    <directory>../src/*/*Bundle/Resources</directory>
    <directory>../src/*/*/*Bundle/Resources</directory>
    <directory>../src/*/*Bundle/Tests</directory>
    <directory>../src/*/*/*Bundle/Tests</directory>
    <directory>../src/*/Bundle/*Bundle/Resources</directory>
    <directory>../src/*/Bundle/*Bundle/Tests</directory>
</exclude>

I hope now it's correct ;)


I had the same exact error, and I solved by just upgrading my phpunit version, I had 3.6.10 and now I have 4.0.17 and it works perfectly and as expected. The Synfony book is not completely wrong, it just states the wrong phpunit version as the needed one.

0

精彩评论

暂无评论...
验证码 换一张
取 消