faker 测试数据生成_使用Faker简化测试数据生成

faker 测试数据生成

Testing is an iterative part of the development process that we carry out to ensure the quality of our code. A large portion of this entails writing test cases and testing each unit of our application using random test data.

测试是我们执行以确保代码质量的开发过程的迭代部分。 其中很大一部分需要编写测试用例,并使用随机测试数据来测试应用程序的每个单元。

Actual data for our application comes in when we release it to production, but during the development process we need fake data similar to real data for testing purposes. The popular open source library Faker provides us with the ability to generate different data suitable for a wide range of scenarios.

我们的应用程序的实际数据是在我们将其发布到生产环境时输入的,但是在开发过程中,我们需要与真实数据相似的虚假数据来进行测试。 流行的开源库Faker使我们能够生成适用于各种场景的不同数据。

Here we’ll focus on generating random test data using Faker for testing our test cases.

在这里,我们将重点介绍使用Faker生成随机测试数据来测试我们的测试用例。

Faker的工作方式 (How Faker Works)

Faker comes with a set of built-in data providers which can be easily accessed to generate test data. Additionally, we can define our own test data types making it highly extensible. But first, let’s look at a basic example that shows how Faker works:

Faker带有一组内置的数据提供程序,可以方便地访问它们以生成测试数据。 另外,我们可以定义我们自己的测试数据类型,使其高度可扩展。 但首先,让我们看一个显示Faker如何工作的基本示例:

<?php
require "vendor/autoload.php";

$faker = FakerFactory::create();

// generate data by accessing properties
for ($i = 0; $i < 10; $i++) {
    echo "<p>" . $faker->name . "</p>";
    echo "<p>" . $faker->address . "</p>";
}

The example assumes Faker was installed using Composer and uses the Composer autoloader to make the class definitions available. You can also use Faker by cloning it from its GitHub repository and using its included autoloader if you’re not using Composer.

该示例假定使用安装了Composer的 Faker并使用Composer自动加载器使类定义可用。 您还可以通过从Faker的GitHub存储库中克隆Faker来使用Faker,如果不使用Composer,则可以使用其随附的自动加载器。

To use Faker, we first need to obtain an instance from FakerFactory. All of the default data providers are loaded automatically into the $faker object. Then we generate random data just by calling

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值