包含数据库的自动测试很麻烦。准备测试数据(所谓的夹具)很麻烦。
Yii面向功能测试和包含数据库的测试,而不是模拟数据库结果的纯逻辑测试。在2.0中,我们为Codeception提供了一套完整的测试项目,这是一个类似于BDD的测试框架。为了测试像BDD这样的页面,假定实际情况是可行的,也就是说,自然需要数据库。
所有灯具都可以手写,但是例如,如果您想使用21或更高的分页率...
因此,让我们利用Faker。
在
Yii项目模板的
1 2 3 4 5 6 7 8 9 10 11 12 13 | $config = yii\helpers\ArrayHelper::merge( // ... , [ 'controllerMap' => [ 'fixture' => [ 'class' => 'yii\faker\FixtureController', 'fixtureDataPath' => '@tests/codeception/fixtures/data', 'templatePath' => '@tests/codeception/templates', 'namespace' => 'tests\codeception\fixtures', ], ], ] ); |
根据
设置在
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?php /** * @var $faker \Faker\Generator * @var $index integer */ return [ 'email' => $faker->email, 'password_hash' => Yii::$app->security->generatePasswordHash('password_' . $index), 'auth_key' => Yii::$app->security->generateRandomString(), 'name' => $faker->firstName . ' ' . $faker->lastName, 'birthday' => $faker->date(), 'created_at' => $faker->unixTime, 'updated_at' => $faker->unixTime, ]; |
如果从
当用
执行
1 2 3 4 5 6 7 8 9 10 11 12 | $ tests/codeception/bin/yii fixture/generate user Fixtures will be generated under the path: /.../tests/codeception/fixtures/data Templates will be taken from path: /.../tests/codeception/templates * user Generate above fixtures? (yes|no) [no]: yes The following fixtures template files were generated: * user |
是的,就是这样。再见!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?php return [ [ 'email' => '[email protected]', 'password_hash' => '$2y$13$kU/MfgI/XUD6JacW7VFe2O3iab3pksTEvrXaJ9c0mof86lnNEJwW.', 'auth_key' => 'l7iv336Q00OrP0Mcn7sUgvbUAhd3gU1W', 'name' => 'Ophelia Jacobs', 'birthday' => '1980-09-09', 'created_at' => 602074437, 'updated_at' => 390537715, ], [ 'email' => '[email protected]', 'password_hash' => '$2y$13$ry7b0lKIcY1grUQmQBkjgupbRsNxAnoOsAgOwJsxgXQCnlljYM3W6', 'auth_key' => 'Ux5iYaPTpY5pgym9fuwEjrZXOCdZbXO5', 'name' => 'Audra Cole', 'birthday' => '2010-05-17', 'created_at' => 447193310, 'updated_at' => 293260057, ], ]; |
如果2还不够,请
1 | tests/codeception/bin/yii fixture/generate user --count=10 |
使用
,您可以制作10或20。
手工创建真正脱节的数据很麻烦,而且如果人类手工创建,它将是任意的。有时由于假设泄漏了测试用例。
从
模板生成的另一个优点是,您可以修复不可复制的密码哈希。如果在灯具数据中调用
因此,仅因为有了模板,您就不想对夹具数据进行版本控制。夹具应进行版本控制,以确保测试是绿色的且已提交。
版本控制意味着此数据是开发人员的。根据需要进行编辑,直到重新生成它,并重写所有不便之处。对于多对多表,您可能需要直接编写夹具而不编写模板。
要使用灯具数据,必须定义一个类。但是,它与
命令
1 2 3 4 5 6 7 8 9 10 | <?php namespace tests\codeception\fixtures; use yii\test\ActiveFixture; class UserFixture extends ActiveFixture { public $modelClass = 'app\models\User'; public $depends = []; } |
在
现在您可以加载数据。在运行自动化测试之前,请确保您创建的数据实际上已进入数据库。迁移测试数据库,并使用所有固定装置填充它。
1 2 | tests/codeception/bin/yii migrate/up tests/codeception/bin/yii fixture/load "*" |
如果成功,则可以。如果创建的夹具因外键限制或字符限制而不合适,则不能在测试中使用它。检查后卸载。
1 | tests/codeception/bin/yii fixture/unload "*" |
顺便说一句,如果到目前为止,没有人会再降低您的动力。让我们快速编写一个自动化测试。
我没有太多时间,因此请参阅手册或从应用程序模板中阅读有关如何在各种测试中加载它的信息。
由于Faker是通用工具,因此可以与Laravel和Symfony一起使用。我还在Packagist中看到了CakePHP3插件。
如果要在Laravel中使用Faker,请参阅Jeff的文章。
http://codebyjeff.com/blog/2013/09/quick-tip-faker-and-laravel-seed-generators
圣诞快乐