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?
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。
It includes the generation method of general credit card, color, occupation, date and time and other data.
It is provided by some communities, and currently includes web related, cloud related, WiFi, microservices, and credit score data.
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())
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())
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.
The data that can be faked by Faker is introduced in the front, and the following tape is for you to actually operate it.
Create a Faker object with the installed Faker library
from faker import Faker
fake = Faker()
Then you can call different methods to generate various data with the fake object.
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
class CustomProvider (BaseProvider) :
def customize_ua (self) :
return 'test_Faker_customize_ua'
fake = Faker()
fake.add_provider(CustomProvider)
print(fake.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.
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