在Yii2框架下使用自带codeception进行单元测试提示model类不存在解决方法

简介: 在Yii2框架下使用自带codeception进行单元测试提示model类不存在解决方法

开发环境以及版本

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)


相关文章
|
26天前
Mybatis+mysql动态分页查询数据案例——测试类HouseDaoMybatisImplTest)
Mybatis+mysql动态分页查询数据案例——测试类HouseDaoMybatisImplTest)
20 1
|
1月前
|
敏捷开发 分布式计算 测试技术
深入理解软件测试中的自动化框架选择与优化策略
【2月更文挑战第29天】 在软件开发的生命周期中,测试环节扮演着至关重要的角色。随着敏捷开发和持续集成的普及,自动化测试成为确保软件质量和加快产品上市速度的关键手段。本文将探讨在构建自动化测试框架时面临的挑战,分析不同类型自动化框架的特点及其适用场景,并提出一系列优化策略,旨在帮助测试工程师提高测试效率,确保测试结果的准确性。
16 0
|
1天前
|
监控 测试技术 数据安全/隐私保护
如何将代理IP集成到自动化测试框架中?
如何将代理IP集成到自动化测试框架中?
|
4天前
|
敏捷开发 监控 前端开发
深入理解自动化测试框架Selenium的架构与实践
【4月更文挑战第16天】 在现代软件开发过程中,自动化测试已成为确保产品质量和加快迭代速度的关键手段。Selenium作为一种广泛使用的自动化测试工具,其开源、跨平台的特性使得它成为业界的首选之一。本文旨在剖析Selenium的核心架构,并结合实际案例探讨其在复杂Web应用测试中的高效实践方法。通过详细解读Selenium组件间的交互机制以及如何优化测试脚本,我们希望为读者提供深入理解Selenium并有效运用于日常测试工作的参考。
11 1
|
4天前
|
自然语言处理 测试技术 API
深入理解自动化测试框架Selenium的设计理念与实践
【4月更文挑战第15天】 在现代软件开发过程中,自动化测试已成为确保产品质量和加速迭代的关键手段。Selenium作为一种广泛使用的自动化测试框架,提供了对多种浏览器和平台的支持,极大地促进了Web应用的功能测试。本文旨在剖析Selenium的核心设计理念,探讨其在实际项目中的应用,并指出常见的误区及最佳实践,以期帮助测试工程师更高效地利用Selenium进行测试工作。
|
6天前
|
监控 测试技术 API
深入理解自动化测试框架Selenium的设计与实现
【4月更文挑战第14天】在软件开发过程中,自动化测试是确保代码质量、减少人工重复劳动的关键步骤。Selenium作为一款广泛使用的自动化测试工具,提供了对多种浏览器和操作系统的支持。本文将探讨Selenium的核心组件及其架构设计,分析其如何通过WebDriver与浏览器交互,以及它如何支持多种编程语言进行脚本编写。同时,我们还将讨论Selenium Grid的作用以及它如何实现并行测试,以缩短测试周期并提高测试效率。
170 59
|
7天前
|
Web App开发 前端开发 Java
框架分析(11)-测试框架
框架分析(11)-测试框架
|
21天前
|
敏捷开发 设计模式 监控
深入理解自动化测试框架的设计原则
在软件开发的复杂多变环境中,自动化测试已成为确保产品质量和加速市场交付的关键步骤。本文将探讨自动化测试框架的设计原则,包括模块化、可扩展性、易用性和可靠性,旨在为软件测试工程师提供构建高效、健壮且易于维护的自动化测试系统的指导。通过分析设计模式的应用,我们将了解如何减少代码冗余,提高测试覆盖率,并适应快速变化的技术要求。
|
23天前
|
前端开发 IDE JavaScript
深入理解自动化测试框架Selenium的设计与实现
本文旨在探讨开源自动化测试框架Selenium的核心设计及其实现机制。通过分析其架构、组件和工作原理,揭示Selenium如何有效地支持跨浏览器、跨平台的自动化Web测试。文中不仅介绍了Selenium的主要功能模块,还详细讨论了其面临的挑战及应对策略,为读者提供了深入了解和使用Selenium的理论基础和实践指导。
|
25天前
|
敏捷开发 测试技术 持续交付
深入探索软件测试自动化:框架与实践
在快速演进的软件行业中,测试自动化已成为确保产品质量和加快上市速度的关键因素。本文将深入分析测试自动化框架的构建要点,探讨其在实际应用中的效益,以及实施过程中可能面临的挑战。通过对比手动测试与自动化测试的优势与局限,本文旨在为读者提供一套系统化的测试自动化实践指南,以支持更高效、可靠的软件开发周期。
11 0