简体   繁体   English

Phpunit没有运行Symfony测试

[英]Phpunit not running Symfony tests

I'm trying to run a few functional tests in Symfony, but they won't work. 我正在尝试在Symfony中运行一些功能测试,但它们不起作用。 Running phpunit targeting the whole AppBundle results in "No tests executed", and targeting it to a specific class gets me this error: 运行针对整个AppBundle的phpunit导致“没有执行测试”,并将其定位到特定类会导致我出现此错误:

$ phpunit tests/AppBundle/ApplicationAvailabilityFunctionalTest.php
PHP Fatal error:  Uncaught PHPUnit\Runner\Exception: Class 'tests/AppBundle/ApplicationAvailabilityFunctionalTest' could not be found in '/home/.../tests/AppBundle/ApplicationAvailabilityFunctionalTest.php'. in phar:///usr/local/bin/phpunit/phpunit/Runner/StandardTestSuiteLoader.php:107

Later I found out unit tests are not working as well, same error message. 后来我发现单元测试不能正常工作,同样的错误信息。

The functional test I'm trying to run is Symfony Best Practices example: 我正在尝试运行的功能测试是Symfony Best Practices示例:

// tests/AppBundle/ApplicationAvailabilityFunctionalTest.php
namespace Tests\AppBundle;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class ApplicationAvailabilityFunctionalTest extends WebTestCase
{
    /**
     * @dataProvider urlProvider
     */
    public function testPageIsSuccessful($url)
    {
        $client = self::createClient();
        $client->request('GET', $url);

        $this->assertTrue($client->getResponse()->isSuccessful());
    }

    public function urlProvider()
    {
        return array(
            array('/'),
            array('/team'),
            // ...
        );
    }
} 

I have tried targeting another class, the DefaultControllerTest, but the same exception showed up. 我已经尝试过针对另一个类DefaultControllerTest,但是出现了同样的异常。 I'm running phpunit version 6.0.7 (downloaded as Phar), Symfony 3.2 and PHP 7.0. 我正在运行phpunit版本6.0.7(下载为Phar),Symfony 3.2和PHP 7.0。 I've had to require phpunit/phpunit ^4.8 with composer since this exception appeared: 我必须要求phpunit / phpunit ^ 4.8与作曲家,因为这个例外出现了:

PHP Fatal error:  Class 'PHPUnit_Framework_TestCase' not found in /home/.../vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php on line 23

I have tried using vendor/bin/phpunit instead of the .phar version, and at least it detects the tests, but all of them have weird results. 我尝试使用vendor/bin/phpunit而不是.phar版本,至少它检测到测试,但所有这些都有奇怪的结果。 It doesn't recognize expectException, for instance: 它不识别expectException,例如:

$ vendor/bin/phpunit -c phpunit.xml.dist
Error: Call to undefined method Tests\AppBundle\Util\Chart\ChartDataTest::expectException()

In functional tests, it doesn't seem to reach any path, even though they are perfectly accessible through the browser: 在功能测试中,它似乎没有达到任何路径,即使它们可以通过浏览器完全访问:

$ vendor/bin/phpunit -c phpunit.xml.dist
1) Tests\AppBundle\ApplicationAvailabilityFunctionalTest::testPageIsSuccessful with data set #0 ('/')
Failed asserting that false is true.

Is it a version issue? 这是版本问题吗?

composer.json file: composer.json文件:

    {
"name": "symfony/framework-standard-edition",
"license": "MIT",
"type": "project",
"description": "The \"Symfony Standard Edition\" distribution",
"autoload": {
    "psr-4": { "": "src/" },
    "classmap": [ "app/AppKernel.php", "app/AppCache.php" ]
},
"autoload-dev": {
    "psr-4": { "Tests\\": "tests/" }
},
"require": {
    "php": ">=5.5.9",
    "symfony/symfony": "3.2.*",
    "doctrine/orm": "^2.5",
    "doctrine/doctrine-bundle": "^1.6",
    "doctrine/doctrine-cache-bundle": "^1.2",
    "symfony/swiftmailer-bundle": "^2.3",
    "symfony/monolog-bundle": "^3.0",
    "symfony/polyfill-apcu": "^1.0",
    "sensio/distribution-bundle": "^5.0",
    "sensio/framework-extra-bundle": "^3.0.2",
    "incenteev/composer-parameter-handler": "^2.0",
    "phpunit/phpunit": "^4.8"
},
"require-dev": {
    "sensio/generator-bundle": "^3.0",
    "symfony/phpunit-bridge": "^3.0",
    "doctrine/doctrine-fixtures-bundle": "^2.3"
},
"scripts": {
    "symfony-scripts": [
        "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile",
        "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::prepareDeploymentTarget"
    ],
    "post-install-cmd": [
        "@symfony-scripts"
    ],
    "post-update-cmd": [
        "@symfony-scripts"
    ]
},
"config": {
    "platform": {
        "php": "5.5.9"
    }
},
"extra": {
    "symfony-app-dir": "app",
    "symfony-bin-dir": "bin",
    "symfony-var-dir": "var",
    "symfony-web-dir": "web",
    "symfony-tests-dir": "tests",
    "symfony-assets-install": "relative",
    "incenteev-parameters": {
        "file": "app/config/parameters.yml"
    },
    "branch-alias": {
        "dev-master": "3.2-dev"
    }
}
   }

Here's phpunit.xml.dist : (currently in root folder, unmodified since project creation) 这是phpunit.xml.dist :(当前在根文件夹中,自项目创建以来未经修改)

<?xml version="1.0" encoding="UTF-8"?>
<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.8/phpunit.xsd"
         backupGlobals="false"
         colors="true"
         bootstrap="app/autoload.php"
>
    <php>
        <ini name="error_reporting" value="-1" />
        <server name="KERNEL_DIR" value="app/" />
    </php>

    <testsuites>
        <testsuite name="Project Test Suite">
            <directory>tests</directory>
        </testsuite>
    </testsuites>

    <filter>
        <whitelist>
            <directory>src</directory>
            <exclude>
                <directory>src/*Bundle/Resources</directory>
                <directory>src/*/*Bundle/Resources</directory>
                <directory>src/*/Bundle/*Bundle/Resources</directory>
            </exclude>
        </whitelist>
    </filter>
</phpunit>

It was a version issue, it seems: for some reason when the Symfony project was created the PHP version was set to 5.5, which restrained more recent PHPUnit versions. 这是一个版本问题,似乎:出于某种原因,在创建Symfony项目时,PHP版本被设置为5.5,这限制了更新的PHPUnit版本。 When I downloaded the PHAR version, it was version 6.0 (which did not work with the version of the project I was working with - PHP 5.6 - but was runnable since my php-cli was 7.0). 当我下载PHAR版本时,它是版本6.0(它不适用于我正在使用的项目版本 - PHP 5.6 - 但由于我的php-cli是7.0,因此可以运行)。 PHPUnit 4.8, which came through composer, did run the ApplicationAvailability tests ok (this, at least, is something weird happening with my code, I will find out about that later), but still had awkward behaviour (understandable, since it's practically deprecated). PHPUnit 4.8,通过作曲家来完成,确实运行了ApplicationAvailability测试(至少,这是我的代码发生的奇怪事情,我稍后会发现),但仍然有尴尬的行为(可以理解,因为它实际上已被弃用) 。 When I configured composer.json with PHP 5.6, everything was clear: PHPUnit upgraded to 5.7, which works perfectly with PHP 5.6. 当我使用PHP 5.6配置composer.json时,一切都很清楚:PHPUnit升级到5.7,与PHP 5.6完美配合。 Thanks @mickadoo for pointing out that the tests did run ok. 感谢@mickadoo指出测试确实运行正常。

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

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