phpunit PHP Fatal error: Uncaught Error: Class not found in
Ejecución de tests de una determinada carpeta (namespace)
Sucede que tengo varios test por ejecutar y en el paquete (namespace) en el que estoy trabajando tarda un poco en ejecutarse. Intentando lanzar el test solo de dicho namespace me da el siguiente error
vendor/bin/phpunit ./tests/Unit/Services
PHP Fatal error: Uncaught Error: Class 'App\Tests\Unit\BaseTest' not found in <path-project>/tests/Unit/Services/RulezCheckerTest.php:10
Stack trace:
#0 <path-project>/vendor/phpunit/phpunit/src/Util/FileLoader.php(61): include_once()
#1 <path-project>/vendor/phpunit/phpunit/src/Util/FileLoader.php(49): PHPUnit\Util\FileLoader::load('/Users/ioedu/pr...')
#2 <path-project>/vendor/phpunit/phpunit/src/Framework/TestSuite.php(397): PHPUnit\Util\FileLoader::checkAndLoad('/Users/ioedu/pr...')
#3 <path-project>/vendor/phpunit/phpunit/src/Framework/TestSuite.php(525): PHPUnit\Framework\TestSuite->addTestFile('/Users/ioedu/pr...')
#4 <path-project>/vendor/phpunit/phpunit/src/Runner/BaseTestRunner.php(98): PHPUnit\Framework\TestSuite->addTestFiles(Array)
#5 <path-project>/vendor/phpunit/phpunit/src/TextUI/Command.php(125): PHPUnit\Runner\BaseTestRunner->getTest('/ in <path-project>/vendor/phpunit/phpunit/src/TextUI/Command.php on line 103
Fatal error: Uncaught Error: Class 'App\Tests\Unit\BaseTest' not found in <path-project>/vendor/phpunit/phpunit/src/TextUI/Command.php on line 103
PHPUnit\TextUI\Exception: Class 'App\Tests\Unit\BaseTest' not found in <path-project>/vendor/phpunit/phpunit/src/TextUI/Command.php on line 103
Call Stack:
0.0005 400336 1. {main}() <path-project>/vendor/phpunit/phpunit/phpunit:0
0.0055 1090008 2. PHPUnit\TextUI\Command::main() <path-project>/vendor/phpunit/phpunit/phpunit:61
phpunit.xml.dist
<?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/9.3/phpunit.xsd"
backupGlobals="false" colors="true"
bootstrap="tests/bootstrap.php"
>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
<php>
<ini name="error_reporting" value="-1"/>
<server name="SHELL_VERBOSITY" value="-1"/>
</php>
<testsuites>
<testsuite name="Project Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
La solución pasa por actualizar composer.json con el nodo "autoload-dev" y el psr-4 del estandar de namespaces así:
composer.json
...
},
"autoload-dev": {
"psr-4": {
"App\\Tests\\": "tests/"
}
},
"require-dev": {
...
Una vez realizado este cambio hay que ejecutar:
composer dump
vendor/bin/phpunit ./tests/Unit/Services
# Resultado:
Time: 00:01.997, Memory: 6.00 MB
OK (9 tests, 10 assertions)
Autor: Eduardo A. F.
Publicado: 30-09-2020 13:04