使用PhpStorm在Laravel中运行单功能测试

时间:2019-12-21 18:59:36

标签: php laravel phpunit phpstorm

我在PhpStorm中安装了Laravel项目。将 PHP 7.3 PHPUnit 8.4.0 一起使用。

我有如下所示的phpunit.xml设置,当我执行Run -> Run -> phpunit.xml时,它可以正常工作,并且/ tests / feature /中的所有测试都可以运行...是的。

开始时输出是这样的:

[vagrant:///home/mypc/code/homestead]:/usr/bin/php /home/vagrant/myproject/vendor/phpunit/phpunit/phpunit --configuration /home/vagrant/myproject/phpunit.xml --teamcity --cache-result-file=/home/vagrant/myproject/.phpunit.result.cache

但是,我现在有很多测试,并且我并不总是希望每次都运行所有测试,因此我应该能够右键单击任何测试方法,然后单击“运行...方法名称”。 ..”,但发生错误时:

[vagrant:///home/mypc/code/homestead]:/usr/bin/php /home/vagrant/myproject/vendor/phpunit/phpunit/phpunit --configuration /home/vagrant/myproject/phpunit.xml --filter "/(::test_example_test_for_running)( .*)?$/" Tests\\Feature\\ExampleTest\\ExampleTest /home/vagrant/myproject/tests/Feature/ExampleTest.php --teamcity --cache-result-file=/home/vagrant/myproject/.phpunit.result.cache

Fatal error: Uncaught PHPUnit\Runner\Exception: Class 'Tests\\Feature\\ExampleTest\\ExampleTest' could not be found in '/home/vagrant/myproject/tests/Feature/ExampleTest.php'. in /home/vagrant/myproject/vendor/phpunit/phpunit/src/Runner/StandardTestSuiteLoader.php:65

这是我的phpunit.xml:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="vendor/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">
    <testsuites>
        <testsuite name="Unit">
            <directory suffix="Test.php">./tests/Unit</directory>
        </testsuite>

        <testsuite name="Feature">
            <directory suffix="Test.php">./tests/Feature</directory>
        </testsuite>
    </testsuites>
    <filter>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory suffix=".php">./app</directory>
        </whitelist>
    </filter>
    <extensions>
        <extension class="Tests\Bootstrap"/>
    </extensions>
    <php>
        <server name="APP_ENV" value="testing"/>
        <server name="BCRYPT_ROUNDS" value="4"/>
        <server name="CACHE_DRIVER" value="array"/>
        <server name="MAIL_DRIVER" value="array"/>
        <server name="QUEUE_CONNECTION" value="sync"/>
        <server name="SESSION_DRIVER" value="array"/>
        <server name="APP_CONFIG_CACHE" value="bootstrap/cache/config.phpunit.php"/>
        <server name="APP_SERVICES_CACHE" value="bootstrap/cache/services.phpunit.php"/>
        <server name="APP_PACKAGES_CACHE" value="bootstrap/cache/packages.phpunit.php"/>
        <server name="APP_ROUTES_CACHE" value="bootstrap/cache/routes.phpunit.php"/>
        <server name="APP_EVENTS_CACHE" value="bootstrap/cache/events.phpunit.php"/>
        <server name="DB_CONNECTION" value="sqlite"/>
        <server name="DB_DATABASE" value=":memory:"/>
    </php>
</phpunit>

1 个答案:

答案 0 :(得分:0)

升级到 phpunit 8.5 解决了该问题(感谢@LazyOne )。

在composer.json中进行了更改:

"phpunit/phpunit": "^8.0"

收件人:

"phpunit/phpunit": "^8.5"

然后运行composer update,现在一切正常。