This is the 21st day of my participation in Gwen Challenge

preface

Use the Faker library in Python to generate more data, rather than having to test your own set of data every time you want to test an interface.

Address:

Faker. Readthedocs. IO/en/master/I…

Installation:

pip install Faker
Copy the code

The Chinese language

The support for Chinese language is generally used only name, address, mobile phone number

from faker import Faker
fake = Faker('zh_CN')
for _ in range(10) :print(fake.name())
    print(fake.address())
    print(fake.phone_number())
Copy the code

You know, like license plates and things like that are generated in the foreign version of the license plate because it’s not submitted for localization or something, right

To generate the license plate

Actually write their own license plate generation code is quite simple

print(fake.lexify(text='License plate:? ', letters=Hebei, Shanxi, Henan and Beijing)
          + fake.bothify(text='? # # # # # ', letters="ABCDE"))
Copy the code

birthday

Date_of_birth (tzinfo=None, minimum_age=0, maximum_age=115) defaults to 0-115 years old

print(fake.date_of_birth(minimum_age=20, maximum_age=35))
Copy the code

email

print(fake.ascii_email())
Copy the code

The base type

    print(fake.pybool())
    print(fake.pyint())
    print(fake.pyfloat())
    print(fake.pydecimal())
Copy the code

Date/time

Date-time generation is the most used and provides the most methods,

The date of

Date_between (start_date=’-30y’, end_date=’today’) generates a Date object within a specified period of time. The default value is the last 30 years

Date plus time

Date_time_between (start_date=’-30y’, end_date=’now’, tzinfo=None) Generates a DateTime object within a period of time, the default is the last 30 years. You can also add parameters to change it

Generate placeholder paragraphs

The Chinese paragraph is not pretty, I’d rather use the English paragraph

fakeUS = Faker('en_US')
print(fakeUS.paragraph(nb_sentences=5))
Copy the code

Custom generation

bothify

bothify(text=’## ?? = ‘abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ’, ‘letters) to generate a string function, according to each of the following rules replace text placeholder: The numeric symbol (‘ # ‘) is replaced with random numbers (0 to 9). Question mark (‘? ‘) is replaced with random characters from letters. By default, letters contain all ASCII letters, both uppercase and lowercase. Generate the license plate where the following few generated is used in this method. print(fake.bothify(text=’? #####’, letters=”ABCDE”)))

lexify

lexify(text=’???? = ‘abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ’, ‘letters) to generate a string, the text of each question mark (‘? ‘) is replaced by random characters in the letter. By default, letters contain all ASCII letters, both uppercase and lowercase. This method is used to generate the first digit in front of the license plate, taking a random value from the given text. Fake.lexify (text=’ license:? ‘, letters=’ Hebei, Shanxi, Henan and Beijing ‘)

numerify

numerify(text='###')

  • (‘#’) is replaced with random numbers (0 to 9).
  • (‘%’) is replaced with random non-zero numbers (1 through 9).
  • (‘! ‘) is replaced by a random number or an empty string.
  • (‘@’) is replaced with a random non-zero number or an empty string.
for _ in range(5):
     fake.numerify(text='Intel Core i%-%%##K vs AMD Ryzen % %%##X')

'Intel Core i9-8766K vs AMD Ryzen 5 8604X'
'Intel Core i5-3293K vs AMD Ryzen 5 9382X'
'Intel Core i8-9241K vs AMD Ryzen 6 7615X'
'Intel Core i8-9593K vs AMD Ryzen 1 9187X'
'Intel Core i8-6416K vs AMD Ryzen 6 2409X'
Copy the code

random_choices

Random_choices (elements=(‘a’, ‘b’, ‘c’), length=None) generates a list of randomly sampled objects from each element. Length controls the length

fake.random_choices(elements=('a'.'b'.'c'.'d'), length=10)
fake.random_choices(elements=OrderedDict([("a".0.45), ("b".0.35), ("c".0.15), ("d".0.05)))Copy the code

random_int

Random_int (min=0, Max =9999, step=1) generates a random integer

conclusion

Life is short, I use Python, and with the power of Python, we can implement our automated testing requirements with just a few lines of code.