Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Faker to Test data in batch based on Python

Shulou Source: shulou.com Published: 2022-06-01 11:03:31 10月01日 Update

This article mainly introduces based on Python how to use Faker batch test data related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe you will have something to gain after reading this article on how to use Faker batch test data based on Python, let's take a look.

In the process of testing, it is often necessary to create data in batches, and there are many methods, the most simple and convenient one should be to use Faker, a third-party library of python.

Script function, batch generate 10000 pieces of basic data, write to the local file: test_data.csv

Execute on the linux server, take the test_data.csv to the local windows,excel and open the possible Chinese garbled code. You can use: data-Import data from text / CSV, modify the encoding format, and display Chinese normally.

The following is a detailed script:

Import csvfrom faker import Fakerimport datetimefake = Faker (['zh_CN']) file = open ("test_data.csv", "w", newline= "")

Create a file, which is the file name, w opening method (w represents new, if it already exists, delete rewrite), newline (if not added, there will be one more blank line for each row of data)

Fwrite = csv.writer (file)

Get the object to write to the file:

Fwrite.writerow (["name", "phone", "Card_id", "Company", "address", "Credit Card", "position", "email"])

Write the title header:

For i in range (9999): user_name = fake.name () phone = fake.phone_number () card_id = fake.ssn () company = fake.company () addr = fake.address () bank_card = fake.credit_card_number () title = fake.job () email = fake.email () fwrite.writerow ([user_name, phone, card_id, company, addr, bank_card, title, email])

Write a row of data:

File.close ()

Effect Preview:

This is the end of the article on "how to use Faker batch Test data based on Python". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to use Faker batch test data based on Python". If you want to learn more knowledge, welcome to follow the industry information channel.

Tags: Data test file knowledge line content article script Chinese tripartite lower editor garbled representative value function credit credit card company address object Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Shulou Information Docker OPPO Reno Linux