What is Python factory_boy?

If you are working on building a python-based application, you might have also written unit tests for your programs. You could be aware of fixtures generally used in creating prepare static data for the test. You can learn more about it in my pytest blog.
As per the factory_boy documentation, it is a fixtures replacement tool, it aims to replace static, hard-to-maintain fixtures with easy-to-use factories for complex objects
We can install factory_boy using the below command.
$ pip install factory_boyLet's see one sample example with fixture and fake created with factory_boy
In the following example, we have created an Employee class with two property methods.
## example2.py
import uuidfrom dataclasses import dataclass@dataclass
class Employee():
id: uuid.UUID
first_name: str
last_name: str
admin: bool
address: str
state:str
country: str
phone: str
email: str@property
def get_employee_name(self):
return f"{self.first_name} {self.last_name}"@property
def get_employee_email(self):
return self.emaillet's create a test case with a fixture. In the following test, we have created a fixture dummy_employee which created an instance of an Employee class with constant static values.
## test_example2.py
import uuid
import pytestfrom src.example2 import Employee@pytest.fixture
def dummy_employee():
return Employee(
id = "943f4ff1-b5d6-4eeb-82f9-5640b4b7ecdb",
first_name = "Sunil",
last_name = "Kumar",
admin = False,
address = "address",
state= "Oregon",
country= "US",
phone = "12345678",
email = "[email protected]",)
def test_get_employee_name(dummy_employee):
emp= dummy_employee
assert emp.get_employee_name == "Sunil Kumar"def test_get_employee_email(dummy_employee):
emp= dummy_employee
assert emp.get_employee_email == "[email protected]"Output:

Now let's refactor the above code and use the fake created using by factory_boy.
In the following tests, we have imported factory and created a fake EmployeeFactory which inherited factory.Factory class. You can see we are not hard coding any static values for EmployeeFactory. The values of each attribute are created automatically by factory.Faker(). We can the instance EmployeeFactory using its build() method. Each time we call the build() method create an instance of an Employee with different values.
## test_example2.py
import uuid
import factory
import pytestfrom src.example2 import Employeeclass EmployeeFactory(factory.Factory):
class Meta:
model = Employee
id = factory.Faker("uuid4")
first_name = factory.Faker('first_name')
last_name = factory.Faker('last_name')
admin = False
address = factory.Faker('address')
state= factory.Faker('state')
country= factory.Faker('country')
phone = factory.Faker("phone_number")
email = factory.Faker("safe_email")def test_get_employee_namewith_fake():
emp= EmployeeFactory.build()
assert emp.get_employee_name == f"{emp.first_name} {emp.last_name}"def test_get_employee_email_with_fake():
emp= EmployeeFactory.build()
assert emp.get_employee_email == emp.emailOutput:

Conclusion :
In this blog, we have seen how we can use factory_boy to make fake data for testing. It is just a very simple example. You can do lot more with factory_boy. Please visit the documentation.
Reference :






