我使用Yii2和Codeception。问题是Codeception似乎没有看到数据库转储。为简单起见,我只考虑LoginCept验收测试:
use tests\_pages\LoginPage;
$I = new WebGuy($scenario);
$I->wantTo('ensure that login works');
$loginPage = LoginPage::openBy($I);
$I->see('Login', 'h1');
$I->amGoingTo('try to login with empty credentials');
$loginPage->login('', '');
$I->expectTo('see validations errors');
$I->see('Username cannot be blank.');
$I->see('Password cannot be blank.');
...
所以,当我跑
时./vendor/bin/codecept run tests/acceptance/LoginCept.php
一切都运行正常:
Acceptance Tests (1) ----------------------------------------
Trying to ensure that login works (LoginCept.php) Ok
-------------------------------------------------------------
但是,只要我添加以下行
echo $I->grabFromDatabase('user', 'name', ['id' => 1]);
测试开始失败:
Acceptance Tests (1) ----------------------------------------
Trying to ensure that login works (LoginCept.php) Error
-------------------------------------------------------------
Time: 2.11 seconds, Memory: 9.75Mb
There was 1 error:
---------
1) Failed to ensure that login works in LoginCept.php
#1 F:\site\tests\acceptance\LoginCept.php:8 <-- 8 is the number of added line
#2 F:\site\tests\acceptance\LoginCept.php:8
FAILURES!
下面我从配置文件中输入详细信息。
codeception.yml文件包含以下内容:
paths:
tests: tests
log: tests/_log
data: tests/_data
helpers: tests/_helpers
settings:
bootstrap: _bootstrap.php
suite_class: \PHPUnit_Framework_TestSuite
memory_limit: 1024M
log: true
colors: true
modules:
enabled: [Db]
config:
Db:
dsn: 'mysql:host=localhost;dbname=site_test'
user: 'daemon'
password: 'HsqyJSAbZC3q3KJa'
dump: 'tests/_data/dump.sql'
populate: true
cleanup: true
打印出\ Yii :: $ app-&gt; db给出:
yii\db\Connection Object
(
[dsn] => mysql:host=localhost;dbname=site_test
[username] => daemon
[password] => HsqyJSAbZC3q3KJa
[attributes] =>
[pdo] =>
[enableSchemaCache] =>
[schemaCacheDuration] => 3600
...
)
使用上面的凭据,我可以手动登录到我的数据库(在phpmyadmin中)。
数据库转储位于F:\site\tests\_data\dump.sql
。我已经从我的工作数据库 site 创建了它,并在其中进行了一些修改:
CREATE DATABASE IF NOT EXISTS `site_test` DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;
USE `site_test`;
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` smallint(5),
`name` varchar(50)
);
INSERT INTO `user` VALUES (1,'admin');
每次运行时,Codeception 构建命令,给出:
$ ./vendor/bin/codecept build
Building Guy classes for suites: acceptance, functional, unit
WebGuy includes modules: WebHelper, PhpBrowser
WebGuy.php generated successfully. 48 methods added
TestGuy includes modules: Filesystem, TestHelper, Yii2
TestGuy.php generated successfully. 53 methods added
CodeGuy includes modules: CodeHelper
CodeGuy.php generated successfully. 1 methods added
答案 0 :(得分:7)
确保为您的套装配置启用了Db模块。
然后你需要在使用Db模块的方法之前运行codecept build
。
通常,每次在test suit配置中启用或禁用模块时都应该运行它。