自动创建虚拟数据以使用Faker进行自动化测试


包含数据库的自动测试很麻烦。准备测试数据(所谓的夹具)很麻烦。

Yii面向功能测试和包含数据库的测试,而不是模拟数据库结果的纯逻辑测试。在2.0中,我们为Codeception提供了一套完整的测试项目,这是一个类似于BDD的测试框架。为了测试像BDD这样的页面,假定实际情况是可行的,也就是说,自然需要数据库。

所有灯具都可以手写,但是例如,如果您想使用21或更高的分页率...

因此,让我们利用Faker。

Yii项目模板的tests文件夹下,有一个专用于测试环境的codeception\bin\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',
            ],
        ],
    ]
);

根据

设置在tests/codeception/templates中写入灯具模板。例如,对于用户装置,请像这样编写templates/user.php

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,
];

如果从

$faker中获取属性,则每次检索该属性时,都会得到看起来像废话的数据。如果不是随机的,则可以使用$index进行计算或从固定数组中提取。

当用

执行tests/codeception/bin/yii fixture/generate user时,将设置模板路径和数据路径,因此...

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。

手工创建真正脱节的数据很麻烦,而且如果人类手工创建,它将是任意的。有时由于假设泄漏了测试用例。

模板生成的另一个优点是,您可以修复不可复制的密码哈希。如果在灯具数据中调用generatePasswordHash,则出于安全原因,每次实际上它都是不同的数据。它可能是一个很小的细节,但是如果您使用夹具数据生成值,则在某些情况下可能无法重现故障。夹具数据是测试代码的一部分。

因此,仅因为有了模板,您就不想对夹具数据进行版本控制。夹具应进行版本控制,以确保测试是绿色的且已提交。

版本控制意味着此数据是开发人员的。根据需要进行编辑,直到重新生成它,并重写所有不便之处。对于多对多表,您可能需要直接编写夹具而不编写模板。

要使用灯具数据,必须定义一个类。但是,它与AssetBundle一样容易实现。用于测试yii
命令FixtureController中的namespace指向...

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 = [];
}

$modelClass是一个ActiveRecord类,用于获取有关要加载哪个表的信息。

$depends中,在加载此灯具之前注册要加载的其他灯具的多个类名。正是AssetBundle的粘合剂。现在,您不必在加载时错过外键。

现在您可以加载数据。在运行自动化测试之前,请确保您创建的数据实际上已进入数据库。迁移测试数据库,并使用所有固定装置填充它。

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

圣诞快乐