I want to use Faker to generate some data for tests.
But I have trouble generating consitent data for a single user:
>>> from faker import Factory
>>> fake = Factory.create()
>>> fake.name()
>>> u'Tayshaun Corkery'
>>> fake.email()
>>> u'[email protected]'
As you see the email doesn't reflect the previously generated name. The docs say:
Each call to method
fake.name()
yields a different (random) result. This is because faker forwardsfaker.Generator.method_name()
calls tofaker.Generator.format(method_name)
.
Is there a way to generate consistent person data without writing much of additional code?
You can use Factory_Boy for it. It comes with built-in Faker and allows you to create, from an already generated attribute (a name, a surname, for example) a "lazy" attribute (an email for example) using the previous data.
I have made a script that will use the first name and last name from faker and add a random number between 1 and 1000, put them together to make a fake generated email with a number to make it look more legit (Example [email protected]), here is the code
from faker import Faker # Import Faker And Random For The Random Number #
from random import *
fake = Faker('en_GB') # Sets Faker's Location To Great Britain#
fn = ((fake.first_name())) # Makes The First Name Into A Veriable #
ln = ((fake.last_name())) # Makes The Last Name Into A Veriable #
rn = str (randint(1,1000)) # Makes A Random Number From 1 To 1000 And It Used To Add A Random Number To The End Of The Email To Make It Look Legit #
email = (fn + ln + rn + "@gmail.co.uk") # Adds The First Name, Last Name And The Random Number And The Extension '@gmail.co.uk' #
print("First Name: " + fn + "\n" + "last Name: " + ln + "\n" + "Email: " + email) # Prints The Whole Output#
I Hope This Helps, If You Need Any More Help Message Me On Twitter @R00T_H4X0R
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With