Test data will not produce? Fake Data!

2020/06/18 08:00
Reading number 243





Python & Faker

Test data will not produce?





In the process of testing, everyone should have encountered various data construction problems. e. G. Construct a batch of address books, a batch of three elements of users (name, phone number, ID card), and a batch of bank card data

At this time, most of the test data may be as follows:

Zhang San, 130 0000 0001

Li Si, 130 0000 0002

Wang Wu, 130 million 0003

……

Or just knock around and make a batch.

Did you do that? Frankly speaking, the previous Xiaobian is Maozi.

Such test data not only need to be manually typed, but also can't be fake again, wasting time, manpower, and low data value Later, I thought of a way to synchronize the online data, but also to encrypt and decrypt, and also to find the data you want one by one.


Until one day, Xiaobian met Faker, It can generate a batch of all kinds of fake data that looks like "real"



What data does Fake have?


PART 01


At present, the Faker library provides three types of data that can be "constructed", which are officially divided into: Standard Providers Community Providers、Localized Providers。


Standard Providers


It includes the generation method of general credit card, color, occupation, date and time and other data.

Community Providers


It is provided by some communities, and currently includes web related, cloud related, WiFi, microservices, and credit score data.

Community Providers


According to regional/language differences, localization provides some methods, such as the names generated in simplified Chinese are different from those generated in traditional Chinese.


Fake several address books


 for _ in range(3):     print ( 'Name:' , fake.name(), 'Mobile number:' , fake.phone_number())     #Fake is an object created by Faker, and Chinese is specified

Name: Wang Xia Mobile number: 15744918509

Name: Li Xu Mobile number: 18025187089

Name: Guo Juan Mobile number: 13196551713



Fake a group of credit card data


 print( 'Card Number:' , fake.credit_card_number(card_type= None )) print( 'Card Provider' , fake.credit_card_provider(card_type= None )) print( 'Card Security Code' , fake.credit_card_security_code(card_type= None )) print( 'Card Expire' , fake.credit_card_expire()) #Fake is an object created by Faker

Card Number:  2720041566219373

Card Provider:  Mastercard

Card Security Code:  215

Card Expire:  07/20

You can use dir (fake) to see which data can be faked in the faker library. Currently, faker supports nearly 300 types of data, and it also supports self expansion.



How to make data


PART 02


The data that can be faked by Faker is introduced in the front, and the following tape is for you to actually operate it.


Step 1

Install Faker Library

 pip install Faker


Step 2

Create a Faker object with the installed Faker library

 from faker import Faker fake = Faker()


Step 3

Specify language

 fake = Faker( "zh_CN" )


Step 4

Fake data

Then you can call different methods to generate various data with the fake object.



Extended Faker


PART 03

If the data is not enough for generating data, Faker also supports creating custom provider generated data.

 from faker import Faker from faker.providers import BaseProvider
#Create a custom provider class CustomProvider (BaseProvider) : def customize_ua (self) : return 'test_Faker_customize_ua'
#Add Provider fake = Faker() fake.add_provider(CustomProvider) print(fake.customize_ua())

test_Faker_customize_ua

Is it very simple and easy to expand. In the future, commonly used data can be created by the provider itself and generated automatically, which not only saves time, but also increases reusability.



Write at the end


The Last

Reading the source code of Faker, it is easy to find that Faker actually maintains a "database", which is powerful and has done a lot of localized processing and compatibility. In addition, As an open source library, the source code of Faker is worth studying, and it is also a sharp tool for Python novices to practice open source projects

Of course, the disadvantages are obvious. It is not so intelligent. The generated data is randomly generated, and the amount of data is not so large.


【GitHub】 https://github.com/joke2k/faker

【Docs】 https://faker.readthedocs.io/en/master/


Sogou test WeChat signal: Qa_xiaoming

Sogou test QQ fan group: 459645679



This article is shared from the WeChat official account Sogou QA.
In case of infringement, please contact support@oschina.cn Delete.
Participation in this article“ OSC Source Innovation Plan ”, welcome you to join us and share with us.

Expand to read the full text
Loading
Click to lead the topic 📣 Post and join the discussion 🔥
Reward
zero comment
zero Collection
zero fabulous
 Back to top
Top