使用faker 生成中文测试数据
Posted 有味青年
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用faker 生成中文测试数据相关的知识,希望对你有一定的参考价值。
https://github.com/fzaninotto/Faker/blob/master/src/Faker/Provider/zh_CN/Address.php
常用的类型都在里面。
下面是一个实例。使用了laravel 框架的工厂模式向数据库填充测试数据。
$factory->define(App\Models\Customer::class, function ($faker) {
$faker = Faker\Factory::create(‘zh_CN‘);
return [
‘name‘ => $faker->name(),
‘sex‘ => $faker->numberBetween($min = 0, $max = 1),
‘age‘ => $faker->numberBetween($min = 30, $max = 90),
‘minzu_id‘ => $faker->numberBetween($min = 1, $max = 8),
‘location_id‘ => $faker->numberBetween($min = 5, $max = 9),
‘marriage‘ => 1,
‘education‘ => 1,
‘job‘ => 1,
‘user_id‘ => 1,
‘orgnization‘ => ‘xx大学‘,
‘contactor‘ => $faker->name(),
‘contactormobile‘ => $faker->phoneNumber(),
‘idnumber‘ => $faker->numerify(‘640############‘),
‘address‘ => $faker->address(),
‘hukou‘ => $faker->address(),
‘mobile‘ => $faker->phoneNumber(),
‘telephone‘ => $faker->phoneNumber(),
‘created_at‘ => $faker->date($format = ‘Y-m-d‘, $max = ‘now‘),//‘2016-09-01‘,
‘updated_at‘ => $faker->date($format = ‘Y-m-d‘, $max = ‘now‘)//‘2016-09-01‘
];
});
$faker = Faker\Factory::create(‘zh_CN‘);
return [
‘name‘ => $faker->name(),
‘sex‘ => $faker->numberBetween($min = 0, $max = 1),
‘age‘ => $faker->numberBetween($min = 30, $max = 90),
‘minzu_id‘ => $faker->numberBetween($min = 1, $max = 8),
‘location_id‘ => $faker->numberBetween($min = 5, $max = 9),
‘marriage‘ => 1,
‘education‘ => 1,
‘job‘ => 1,
‘user_id‘ => 1,
‘orgnization‘ => ‘xx大学‘,
‘contactor‘ => $faker->name(),
‘contactormobile‘ => $faker->phoneNumber(),
‘idnumber‘ => $faker->numerify(‘640############‘),
‘address‘ => $faker->address(),
‘hukou‘ => $faker->address(),
‘mobile‘ => $faker->phoneNumber(),
‘telephone‘ => $faker->phoneNumber(),
‘created_at‘ => $faker->date($format = ‘Y-m-d‘, $max = ‘now‘),//‘2016-09-01‘,
‘updated_at‘ => $faker->date($format = ‘Y-m-d‘, $max = ‘now‘)//‘2016-09-01‘
];
});
以上是关于使用faker 生成中文测试数据的主要内容,如果未能解决你的问题,请参考以下文章
使用python的faker库和dataclass快速生成测试数据
Python:随机生成测试数据的模块--faker的基本使用