person using phone and laptop computer
Photo by Austin Distel on Unsplash

Testing your code is a great way to check whether you have implemented everything correctly. No matter how much you try, no matter how hard you analyze all the cases, sometimes, just the smallest mistake can lead to a big problem which could have maybe been avoided writing tests.

When you write tests, you are obviously not going to use data from production. We need to use some dummy data that does not belong to anyone who is actually using the project. This is called faking the data.

Something that can speed your process of writing tests is having some sort of module or library that can take the burden of generating those fake data.

There can be many opportunities for you to do that, but here we are going to demonstrate how you can use the Faker library to generate fake data.

First off, install faker in your environment.

After you have installed it, you can import it:

from faker import Faker
faker_instance = Faker()

Now, if we want to generate a random name, we can do it like this:

name = faker_instance.name()
print(name)

You can see that it returns a random name and each time you run it, it is expected to return a different one.

We can use it to also generate random addresses or even random texts that can be used to represent texts of any kind:

address = faker_instance.address()
print(address)
text = faker_instance.text()
print(text)

That’s basically it. I hope you find it useful.