为什么phpUnit测试代码覆盖率在包含文件时不能在Symfony2中工作?

时间:2012-10-25 05:47:43

标签: symfony phpunit code-coverage

PHP Fatal error:  Class 'Symfony\Bundle\FrameworkBundle\Test\WebTestCase' not found in /home/manivasagam/projects/ProofCentral/src/ProofCentral/ServiceBundle/Tests/Services/AttachmentServiceTest.php on line 15
PHP Stack trace:
PHP   1. {main}() /usr/bin/phpunit:0
PHP   2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:46
PHP   3. PHPUnit_TextUI_Command->run() /usr/share/php/PHPUnit/TextUI/Command.php:129
PHP   4. PHPUnit_Runner_BaseTestRunner->getTest() /usr/share/php/PHPUnit/TextUI/Command.php:150
PHP   5. PHPUnit_Runner_BaseTestRunner->loadSuiteClass() /usr/share/php/PHPUnit/Runner/BaseTestRunner.php:104
PHP   6. PHPUnit_Runner_StandardTestSuiteLoader->load() /usr/share/php/PHPUnit/Runner/BaseTestRunner.php:168
PHP   7. PHPUnit_Util_Fileloader::checkAndLoad() /usr/share/php/PHPUnit/Runner/StandardTestSuiteLoader.php:77
PHP   8. PHPUnit_Util_Fileloader::load() /usr/share/php/PHPUnit/Util/Fileloader.php:76
PHP   9. include_once() /usr/share/php/PHPUnit/Util/Fileloader.php:92

1 个答案:

答案 0 :(得分:1)

这里的问题是你没有加载bootstrap文件

我将配置放在app/phpunit.xml

其中包含以下xml:

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

<!-- http://www.phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit
    backupGlobals="false"
         backupStaticAttributes="false"
         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>
            <directory>../src/*/Bundle/*/Tests</directory>
        </testsuite>

    </testsuites>

    <!--
    <php>
        <server name="KERNEL_DIR" value="/path/to/your/app/" />
    </php>
    -->

    <filter>
        <whitelist>
            <directory>../src</directory>
            <exclude>
                <directory>../src/*/Bundle/*/Resources</directory>
                <directory>../src/*/Bundle/*/Controller</directory>
                <directory>../src/*/Bundle/*/Tests</directory>
                <directory>../src/*/Bundle/*/DataFixtures</directory>
                <directory>../src/*/Bundle/*/DependencyInjection</directory>
                <directory>../src/*/Bundle/*/Form</directory>
                <directory>../src/*/Bundle/*/Event*</directory>
        <directory>../src/*/Bundle/*/Firewall</directory>
            </exclude>
        </whitelist>
    </filter>

</phpunit>

然后运行phpunit

phpunit --coverage-text app src/ProofCentral/ServiceBundle/Tests/Services/CalculatorTest.php --bootstrap path/to/your/app/bootstrap.php.cache