开发环境以及版本
PHP 7.3.12
PHP 7.3.12 (cli) (built: Nov 21 2019 19:00:57) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.3.12, Copyright (c) 1998-2018 Zend Technologies with Zend OPcache v7.3.12, Copyright (c) 1999-2018, by Zend Technologies
Yii 2.0.30
Codeception 2.3.9
问题描述
使用框架自带的Codeception进行单元测试编写,引入自定义命名空间的model,提示找不到类
lxy@bogon ddddemo % php vendor/bin/codecept run unit ExampleTest Codeception PHP Testing Framework v2.3.9 Powered by PHPUnit 6.5.14 by Sebastian Bergmann and contributors. App\tests.unit Tests (1) -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Fatal error: Uncaught Error: Class 'app\application\domains\user\UserInfo' not found in /Users/lxy/code/ddddemo/tests/unit/ExampleTest.php:25 Stack trace: #0 /Users/lxy/code/ddddemo/tests/unit/ExampleTest.php(57): app\tests\unit\ExampleTest->testSomeFeature() #1 /Users/lxy/code/ddddemo/vendor/phpunit/phpunit/src/Framework/TestSuite.php(755): app\tests\unit\ExampleTest->run(Object(PHPUnit\Framework\TestResult)) #2 /Users/lxy/code/ddddemo/vendor/codeception/base/src/Codeception/PHPUnit/Runner.php(106): PHPUnit\Framework\TestSuite->run(Object(PHPUnit\Framework\TestResult)) #3 /Users/lxy/code/ddddemo/vendor/codeception/base/src/Codeception/SuiteManager.php(157): Codeception\PHPUnit\Runner->doEnhancedRun(Object(Codeception\Suite), Object(PHPUnit\Framework\TestResult), Array) #4 /Users/lxy/code/ddddemo/vendor/codeception/base/src/Codeception/Codecept.php(189): Codeception\SuiteManager->run(Object(Codeception\PHPUnit\Runner), Object(PHPUnit\Framework\TestResult), Array) #5 /Users/lxy/code/ddddemo/vendor/codeception/base/src/Code in /Users/lxy/code/ddddemo/tests/unit/ExampleTest.php on line 25 FATAL ERROR. TESTS NOT FINISHED. Uncaught Error: Class 'app\application\domains\user\UserInfo' not found in /Users/lxy/code/ddddemo/tests/unit/ExampleTest.php:25 Stack trace: #0 /Users/lxy/code/ddddemo/tests/unit/ExampleTest.php(57): app\tests\unit\ExampleTest->testSomeFeature() #1 /Users/lxy/code/ddddemo/vendor/phpunit/phpunit/src/Framework/TestSuite.php(755): app\tests\unit\ExampleTest->run(Object(PHPUnit\Framework\TestResult)) #2 /Users/lxy/code/ddddemo/vendor/codeception/base/src/Codeception/PHPUnit/Runner.php(106): PHPUnit\Framework\TestSuite->run(Object(PHPUnit\Framework\TestResult)) #3 /Users/lxy/code/ddddemo/vendor/codeception/base/src/Codeception/SuiteManager.php(157): Codeception\PHPUnit\Runner->doEnhancedRun(Object(Codeception\Suite), Object(PHPUnit\Framework\TestResult), Array) #4 /Users/lxy/code/ddddemo/vendor/codeception/base/src/Codeception/Codecept.php(189): Codeception\SuiteManager->run(Object(Codeception\PHPUnit\Runner), Object(PHPUnit\Framework\TestResult), Array) #5 /Users/lxy/code/ddddemo/vendor/codeception/base/src/Code in /Users/lxy/code/ddddemo/tests/unit/ExampleTest.php:25 lxy@bogon ddddemo % php vendor/bin/codecept run unit ExampleTest Codeception PHP Testing Framework v2.3.9 Powered by PHPUnit 6.5.14 by Sebastian Bergmann and contributors. Fatal error: Trait '_generated\UnitTesterActions' not found in /Users/lxy/code/ddddemo/tests/_support/UnitTester.php on line 21
codeception 相关的配置文件没有修改过
分析过程
1 检查配置文件
对于单元测试涉及到的文件有这两个
- codeception.yml
- unit.suite.yml
codeception.yml
actor: Tester paths: tests: tests log: tests/_output data: tests/_data helpers: tests/_support //重点看支持类生成目录 settings: bootstrap: _bootstrap.php memory_limit: 1024M colors: true modules: config: Yii2: configFile: 'config/test.php'
unit.suite.yml
# Codeception Test Suite Configuration # suite for unit (internal) tests. # RUN `build` COMMAND AFTER ADDING/REMOVING MODULES. class_name: UnitTester modules: enabled: # - app\application\domains\user\UserInfo - Asserts - Yii2: part: [orm, email, fixtures]
2 定位 tests/_support
tests/_support 默认有3个文件
我们打开对应单元测试的
<?php /** * Inherited Methods * @method void wantToTest($text) * @method void wantTo($text) * @method void execute($callable) * @method void expectTo($prediction) * @method void expect($prediction) * @method void amGoingTo($argumentation) * @method void am($role) * @method void lookForwardTo($achieveValue) * @method void comment($description) * @method \Codeception\Lib\Friend haveFriend($name, $actorClass = NULL) * * @SuppressWarnings(PHPMD) */ class UnitTester extends \Codeception\Actor { use _generated\UnitTesterActions; /** * Define custom actions here */ }
发现了问题,文件没有命名空间
在执行测试之后,项目会生成_generated
文件夹,这里面的文件是自动生成的,经过查看也是没有命名空间,这直接导致找不到对应的文件。
解决方案
1 删除 tests/_support 内的所有文件
_support 内的文件会自动生成,删除不会造成影响。
2 修改配置文件 codeception.yml
配置文件增加 namespace
,添加之后会给 tests/_support 内的文件增加命名空间。
修改后的文件如下
namespace: app\tests\_support actor: Tester paths: tests: tests log: tests/_output data: tests/_data helpers: tests/_support settings: bootstrap: _bootstrap.php memory_limit: 1024M colors: true modules: config: Yii2: configFile: 'config/test.php'
3 执行 build重新生成文件
lxy@bogon ddddemo % php vendor/bin/codecept build Building Actor classes for suites: functional, unit -> FunctionalTesterActions.php generated successfully. 0 methods added app\tests\_support\FunctionalTester includes modules: Filesystem, Yii2 FunctionalTester.php created. -> UnitTesterActions.php generated successfully. 0 methods added app\tests\_support\UnitTester includes modules: Asserts, Yii2 UnitTester.php created.
4 执行 run unit
lxy@bogon ddddemo % php vendor/bin/codecept run unit Codeception PHP Testing Framework v2.3.9 Powered by PHPUnit 6.5.14 by Sebastian Bergmann and contributors. App\tests\_support.unit Tests (1) ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ✔ ExampleTest: Some feature (0.03s) --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 3x DEPRECATION: Calling the "Symfony\Component\EventDispatcher\EventDispatcherInterface::dispatch()" method with the event name as the first argument is deprecated since Symfony 4.3, pass it as the second argument and provide the event object as the first argument instead. /Users/lxy/code/ddddemo/vendor/symfony/event-dispatcher/EventDispatcher.php:58 Time: 212 ms, Memory: 12.00MB OK (1 test, 1 assertion)