我有一个 symfony 6 项目,它在带有 php 8.2 的 docker 容器中运行。我使用 phpunit 版本 10.5
当我运行测试时,我得到以下输出:
$ bin/phpunit
PHPUnit 10.5.5 by Sebastian Bergmann and contributors.
Runtime: PHP 8.2.12
Configuration: /var/www/phpunit.xml
............................................................... 63 / 222 ( 28%)
............................................................... 126 / 222 ( 56%)
.EEE........................................................... 189 / 222 ( 85%)
................................. 222 / 222 (100%)
Time: 00:20.429, Memory: 120.00 MB
Auth Data Extractor (App\Tests\UnitTests\Security\AuthDataExtractor)
✔ Get credentials admin 1 house
✔ Get credentials edit 2 house
[....]
Witness Service (App\Tests\UnitTests\Domain\WitnessService)
✔ Create
✔ Create no witness
✔ Update no previous witnesses
✔ Update replace previous witnesses
✔ Get collection
ERRORS!
Tests: 222, Assertions: 1866, Errors: 3.
我的所有测试结果都显示为绿色,表示已通过,但 EEE 和摘要显示我有三个错误。好吧,但是他们在哪里?!
之前(我在几周后重新启动该项目)当我遇到错误时,phpunit 给了我一个输出,其中的问题是在哪些方法中。
更糟糕的是:如果我按文件夹运行测试,我要么没有错误,要么只有一个错误。但是当我将范围缩小到单个测试文件时,所有内容都是绿色的并且没有显示错误。
我进行了作曲家更新,甚至从头开始构建了容器,但至少总是有红色 E。
如何找出哪个测试出现错误?
这是我的 phpunit.xml 配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!-- https://phpunit.readthedocs.io/en/latest/configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd"
colors="true"
bootstrap="tests/bootstrap.php"
cacheResult="false"
executionOrder="default"
testdox="true"
displayDetailsOnTestsThatTriggerErrors="true"
cacheDirectory=".phpunit.cache">
<coverage/>
<php>
<ini name="error_reporting" value="-1"/>
<server name="APP_ENV" value="test" force="true"/>
<server name="SHELL_VERBOSITY" value="-1"/>
<server name="KERNEL_CLASS" value="App\Kernel"/>
<server name="SYMFONY_PHPUNIT_REMOVE" value=""/>
<!--env name="SYMFONY_DEPRECATIONS_HELPER" value="weak" /-->
<!-- ###+ nelmio/cors-bundle ### -->
<env name="CORS_ALLOW_ORIGIN" value="^https?://(localhost|127\.0\.0\.1)(:[0-9]+)?$"/>
</php>
<extensions>
<bootstrap class="DAMA\DoctrineTestBundle\PHPUnit\PHPUnitExtension" />
</extensions>
<testsuites>
<testsuite name="Project Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory>src</directory>
</include>
</source>
</phpunit>
我可以找到错误,但我仍然无法解释这种行为。
我运行 phpstan,phpstan 确实向我显示了我的错误,修复后,它们也随着 phpunit 消失了。
这仍然不能解释为什么 phpunit 至少没有告诉我有问题的类。 这是正确的行为吗?
这是 phpstan 输出,它导致我出现错误:
$ bin/phpstan
Note: Using configuration file /var/www/phpstan.neon.
271/271 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%
------ --------------------------------------------------------------------------------------------------------------
Line src/Repository/ChargeRepository.php
------ --------------------------------------------------------------------------------------------------------------
111 Method App\Repository\ChargeRepository::createQueryForCriteria() should return Doctrine\ORM\QueryBuilder but
return statement is missing.
------ --------------------------------------------------------------------------------------------------------------
------ ----------------------------------------------------------------------------------------------
Line tests/UnitTests/Domain/SapService/CurrencyExchangeServiceTest.php
------ ----------------------------------------------------------------------------------------------
47 Instantiated class App\Serializer\CurrencyExchangeService not found.
💡 Learn more at https://phpstan.org/user-guide/discovering-symbols
47 Property App\Tests\UnitTests\Domain\SapService\CurrencyExchangeServiceTest::$exchangeService
(App\Domain\SapService\CurrencyExchangeServiceInterface) does not accept
App\Serializer\CurrencyExchangeService.
------ ----------------------------------------------------------------------------------------------