尝试运行时出现Codeception错误“找不到Yii类”

4

我目前在实习期间开展一个项目。我已经编写了一些自动化测试。我通过运行 codeception 初始化并构建套件。

codecept bootstrap
codecept build

这两个命令按预期工作,但是当我尝试运行测试时,出现以下情况:

FATAL ERROR. TESTS NOT FINISHED.
Class 'Yii' not found 
in /srv/http/blog/vendor/codeception/codeception/src/Codeception/Module/Yii2.php:242

你们有什么想法吗?我查看了 Yii 和 Codeception 的文档,但找不到解决方案。

更新 新的错误信息

FATAL ERROR. TESTS NOT FINISHED.
Class 'Yii' not found 
in /srv/http/blog/vendor/yiisoft/yii2/helpers/BaseUrl.php:129

你在这个项目中安装了Yii2吗? - Naktibalda
是的,我正在使用Yii2框架,通过composer安装了codeception和Yii2。 - Kyle
1
很遗憾,这个区域的文档不太完善。请务必阅读:http://www.yiiframework.com/doc-2.0/guide-test-environment-setup.html 和关于测试的其他所有内容。对我来说,需要花费相当多的尝试和错误来让它正常工作。虽然我无法识别您的错误,但是终于能让测试正常工作还是非常棒的。 - karpy47
2个回答

4

在我的情况下,_bootstrap.php文件无法正确加载。
为了解决这个问题,我已经在我的codeception.yml中添加了以下内容:

3.0之前版本

settings:
    bootstrap: _bootstrap.php
    memory_limit: 1024M
    colors: true

发布 3.0

 bootstrap: _bootstrap.php
 settings:        
    memory_limit: 1024M
    colors: true

我不太清楚颜色是什么,但它对我有用。此外,这里是我的_bootstrap.php文件,可以提供一些见解。

使用 app\components\Helpers;

define('YII_ENV', 'test');

require_once __DIR__ . '/../vendor/yiisoft/yii2/Yii.php';
require __DIR__ .'/../vendor/autoload.php';

$config = require(__DIR__ . '/../config/console.test.php');

//
$application = new yii\console\Application( $config );

最后两行是用来加载Yii类的。 希望对您有用!

3.0及更高版本的更新

来自http://phptest.club/t/bootstrap-deprecations-in-3-0/2196

如果您收到此消息:

DEPRECATION: 'settings: bootstrap: _bootstrap.php' option is deprecated! Replace it with: 'bootstrap: _bootstrap.php' (not under settings section). See http://phptest.club/t/bootstrap-deprecations-in-3-0/2196 
DEPRECATION: Bootstrap file (_bootstrap.php) is defined in configuration but can't be loaded. Disable 'settings: bootstrap:' configuration to remove this message

做以下事情:
Do the following

这是正确的,但将Bootstrap放在设置下已被弃用(根据您阅读此内容的时间可能会被删除)。请参见http://phptest.club/t/bootstrap-deprecations-in-3-0/2196。 - Stefano Mtangoo
1
更新了我的评论,希望对3.0版本的帖子也有所帮助! - Nebulosar

-2

在 Yii2 代码库中存在一些问题,请检查一下,也许能帮到你。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接