SlideShare a Scribd company logo
Fixtures and factories
by Hector Canto
We will talk about
fixture concept
How to build fixtures with:
factoryboy: Mother pattern
faker: data generator
Make it work in pytest
What is a fixture
“””A test fixture is a device used to consistently test some item, device, or piece of software.
Test fixtures are used in the testing of electronics, software and physical devices.”””
wikipedia.com/en/Test_fixture
Why is this important
Testing should be a big chunk of our daily work
Testing is hard and costly
Let’s make it easier
Making new tests should become easier with time
Testing structure: AAA
Arrange
Act - Execute
Assert
Arranging
In arrange phase we prepare the test
data to input
data to be “there”
the system to act
secondary systems to interact
Data fixtures
We are going to focus on data fixtures for
inputs
expectancies
AAA in python unitest
class TestExample(unittest.Case):
def setUp(self):
...
def test_one(self):
dummy_user = ExampleUserFactory()
self.db.save(dummy_user)
...
result = SystemUnderTest()
...
self.assertTrue(result)
def tearDown(self):
...
In pytest
Any test dependency
usually set as parameter or decorator
import pytest
@pytest.fixture(autouse=True, scope="session")
def global_fixture():
...
pytestmark = pytest.mark.usefixtures("module_fixture")
@pytest.mark.usefixtures("fixture_as_decorator")
def test_one(fixture_as_param):
...
AAA in pytest
@pytest.fixture(scope="module", name="arranged", autouse=False)
def arrange_fixtures():
... # set up
yield "value"
... # tear down
def test_using_fixture_explicitly(arranged):
result = SystemUnderTest(arranged)
assert result is True
...
Data fixtures can be
Inputs
Configuration
Data present in DB, cache files,
Params to query
A dependency to inject
Data fixtures can be (II)
A mock or dummy to use or inject
Set the application’s state
Ready the system under test
The system under test ready for assertion
Revert or clean-up procedures
Where to put fixtures:
in the same place as the test
but it makes the IDE angry
in the closest conftest.py
conftest is pytest’s __init__.py
makes fixture globally available downstream
Fixture example
import random
@pytest.fixture(name="cool_fixture")
def this_name_is_just_for_the_function():
yield random.randint()
def test_using_fixture(cool_fixture):
system_under_test(param=cool_fixture)
Test name fixture
def test_one(request):
test_name = request.node.name
result = system_under_test(test_name)
assert result == test_name
Anti-patterns
Copy-paste the same dict for each test
Have a thousand JSON files
Recommendations
Generate them programmatically
In the test, highlight the difference
Use Mother pattern and data generators
Enter factory-boy
import factory
class UserMother(factory.DictFactory):
firstname = "Hector"
lastname = "Canto"
address = "Praza do Rei, 1, Vigo CP 36000"
Enter faker
class UserMother(factory.DictFactory):
firstname = factory.Faker('first_name')
lastname = factory.Faker('last_name')
address = factory.Faker('address')
random_user = UserMother()
random_user == {
'firstname': 'Rebecca',
'lastname': 'Sloan',
'address': '52097 Daniel Ports Apt. 689nPort Jeffrey, NM 55289'
}
More faker
random_user = UserMother(firstname="Guido")
random_user == {
'firstname': 'Guido',
'lastname': 'Deleon',
'address': '870 Victoria MillsnWilliamville, CA 44946'
}
Batch generation
Iterated generation
UserMother.create_batch(size=5)
many = UserMother.create_batch(size=10, firstname=factory.Iterator(["One", "Two", "Three"]))
many[0].firstname == "One"
many[2].firstname == "Three"
many[3].firstname == "One"
FactoryBoy with ORMs
DjangoORM
SQLAlchemy
Mogo and MongoEngine
not hard to create your own
Example
class UserMother(factory.orm.SQLAlchemyFactory):
class Meta:
model = User
sqlalchemy_session_factory = lambda: common.TestSession()
def test_with_specific_db(test_session):
UserMother._meta.sqlalchmey_session = test_session
# You can also set the live DB and populate it for demos
Set up for SQLA
import factory
from sqlalchemy import orm
TestSession = orm.scoped_session(orm.sessionmaker())
"""Global scoped session (thread-safe) for tests"""
class BaseFactory(factory.alchemy.SQLAlchemyModelFactory):
class Meta:
abstract = True
sqlalchemy_session = TestSession
sqlalchemy_session_persistence = "flush"
class UserFactory(BaseFactory):
class Meta:
model = User
Get or Create Fixture
# Create user once, use everywhere
class UserFactory(BaseUserFactory):
class Meta:
sqlalchemy_get_or_create = ('id',)
user1 = UserFactory(id=1, firstname="Dennis", lastname="Ritchie")
user2 = UserFactory(id=1)
user1 == user2
Good things about factory-boy
Highly customizable
Related factories
Works with ORM
Bad things
Inner Faker is weird
Documentation gaps (as usual)
A bit hard to work with relationships
For the future
polyfactory
make your own providers
check out Maybe, Traits, post_gen hooks
random seed fixing
sequence resetting
user random.sample and others
Tactical tips
Add factories to your libraries
Specially in serverless and microservices
Real case example
3 libraries: common models, common APIs, common 3rd party services
On each we have factories to create
DB data
API callback bodies
event message payloads
Code samples
tests/user_factories.py
Params
Related
Fuzzy
Lazy
/tests/conftest.py
Register fixtures
# tests/conftest/py
register(AdminFactory, "admin")
register(DbUserFactory, "user1", email="user1@hello.es")
register(DbUserFactory, "user2", status=0)
register(DbProfileFactory2, "profile")
# usage
def test_with_reg_fx(admin):
assert admin.first_name == "Admin"
Fixtures and Factories with python-factoryboyfactoryboy_hectorcanto.pdf
Fixtures and Factories with python-factoryboyfactoryboy_hectorcanto.pdf

More Related Content

PDF
Effective testing with pytest
PDF
Phactory
PDF
33rd Degree 2013, Bad Tests, Good Tests
ODP
Pruebas unitarias con django
PDF
Тестирование и Django
PDF
Java programming lab manual
PDF
Django (Web Konferencia 2009)
PDF
MT_01_unittest_python.pdf
Effective testing with pytest
Phactory
33rd Degree 2013, Bad Tests, Good Tests
Pruebas unitarias con django
Тестирование и Django
Java programming lab manual
Django (Web Konferencia 2009)
MT_01_unittest_python.pdf

Similar to Fixtures and Factories with python-factoryboyfactoryboy_hectorcanto.pdf (20)

PDF
Advanced Django
KEY
Workshop quality assurance for php projects tek12
PDF
FITC Web Unleashed 2017 - Introduction to the World of Testing for Front-End ...
PDF
An Introduction to the World of Testing for Front-End Developers
 
PPT
2012 JDays Bad Tests Good Tests
ODP
Building and Incredible Machine with Pipelines and Generators in PHP (IPC Ber...
DOCX
Rhino Mocks
PDF
Programming with ZooKeeper - A basic tutorial
PDF
Programming with ZooKeeper - A basic tutorial
PDF
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
PPT
Beyond Unit Testing
PDF
Quality Assurance for PHP projects - ZendCon 2012
PPT
Diving in the Flex Data Binding Waters
PPTX
CiklumJavaSat_15112011:Alex Kruk VMForce
PDF
Testing the frontend
PDF
Managing Mocks
PPTX
The uniform interface is 42
PDF
Implementing and analyzing online experiments
PDF
Oliver hookins puppetcamp2011
 
PDF
Akka tips
Advanced Django
Workshop quality assurance for php projects tek12
FITC Web Unleashed 2017 - Introduction to the World of Testing for Front-End ...
An Introduction to the World of Testing for Front-End Developers
 
2012 JDays Bad Tests Good Tests
Building and Incredible Machine with Pipelines and Generators in PHP (IPC Ber...
Rhino Mocks
Programming with ZooKeeper - A basic tutorial
Programming with ZooKeeper - A basic tutorial
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Beyond Unit Testing
Quality Assurance for PHP projects - ZendCon 2012
Diving in the Flex Data Binding Waters
CiklumJavaSat_15112011:Alex Kruk VMForce
Testing the frontend
Managing Mocks
The uniform interface is 42
Implementing and analyzing online experiments
Oliver hookins puppetcamp2011
 
Akka tips
Ad

Recently uploaded (20)

PPTX
chapter 5 systemdesign2008.pptx for cimputer science students
PDF
Types of Token_ From Utility to Security.pdf
PDF
Autodesk AutoCAD Crack Free Download 2025
PDF
EaseUS PDF Editor Pro 6.2.0.2 Crack with License Key 2025
PPTX
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
PDF
AI/ML Infra Meetup | LLM Agents and Implementation Challenges
DOCX
How to Use SharePoint as an ISO-Compliant Document Management System
PDF
Topaz Photo AI Crack New Download (Latest 2025)
PPTX
Weekly report ppt - harsh dattuprasad patel.pptx
PPTX
GSA Content Generator Crack (2025 Latest)
PDF
AI-Powered Threat Modeling: The Future of Cybersecurity by Arun Kumar Elengov...
PDF
Time Tracking Features That Teams and Organizations Actually Need
PDF
Designing Intelligence for the Shop Floor.pdf
DOCX
Greta — No-Code AI for Building Full-Stack Web & Mobile Apps
PPTX
WiFi Honeypot Detecscfddssdffsedfseztor.pptx
PPTX
Patient Appointment Booking in Odoo with online payment
PDF
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
PPTX
Trending Python Topics for Data Visualization in 2025
PDF
Digital Systems & Binary Numbers (comprehensive )
PPTX
Tech Workshop Escape Room Tech Workshop
chapter 5 systemdesign2008.pptx for cimputer science students
Types of Token_ From Utility to Security.pdf
Autodesk AutoCAD Crack Free Download 2025
EaseUS PDF Editor Pro 6.2.0.2 Crack with License Key 2025
AMADEUS TRAVEL AGENT SOFTWARE | AMADEUS TICKETING SYSTEM
AI/ML Infra Meetup | LLM Agents and Implementation Challenges
How to Use SharePoint as an ISO-Compliant Document Management System
Topaz Photo AI Crack New Download (Latest 2025)
Weekly report ppt - harsh dattuprasad patel.pptx
GSA Content Generator Crack (2025 Latest)
AI-Powered Threat Modeling: The Future of Cybersecurity by Arun Kumar Elengov...
Time Tracking Features That Teams and Organizations Actually Need
Designing Intelligence for the Shop Floor.pdf
Greta — No-Code AI for Building Full-Stack Web & Mobile Apps
WiFi Honeypot Detecscfddssdffsedfseztor.pptx
Patient Appointment Booking in Odoo with online payment
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
Trending Python Topics for Data Visualization in 2025
Digital Systems & Binary Numbers (comprehensive )
Tech Workshop Escape Room Tech Workshop
Ad

Fixtures and Factories with python-factoryboyfactoryboy_hectorcanto.pdf

  • 2. We will talk about fixture concept How to build fixtures with: factoryboy: Mother pattern faker: data generator Make it work in pytest
  • 3. What is a fixture “””A test fixture is a device used to consistently test some item, device, or piece of software. Test fixtures are used in the testing of electronics, software and physical devices.””” wikipedia.com/en/Test_fixture
  • 4. Why is this important Testing should be a big chunk of our daily work Testing is hard and costly Let’s make it easier Making new tests should become easier with time
  • 6. Arranging In arrange phase we prepare the test data to input data to be “there” the system to act secondary systems to interact
  • 7. Data fixtures We are going to focus on data fixtures for inputs expectancies
  • 8. AAA in python unitest class TestExample(unittest.Case): def setUp(self): ... def test_one(self): dummy_user = ExampleUserFactory() self.db.save(dummy_user) ... result = SystemUnderTest() ... self.assertTrue(result) def tearDown(self): ...
  • 9. In pytest Any test dependency usually set as parameter or decorator import pytest @pytest.fixture(autouse=True, scope="session") def global_fixture(): ... pytestmark = pytest.mark.usefixtures("module_fixture") @pytest.mark.usefixtures("fixture_as_decorator") def test_one(fixture_as_param): ...
  • 10. AAA in pytest @pytest.fixture(scope="module", name="arranged", autouse=False) def arrange_fixtures(): ... # set up yield "value" ... # tear down def test_using_fixture_explicitly(arranged): result = SystemUnderTest(arranged) assert result is True ...
  • 11. Data fixtures can be Inputs Configuration Data present in DB, cache files, Params to query A dependency to inject
  • 12. Data fixtures can be (II) A mock or dummy to use or inject Set the application’s state Ready the system under test The system under test ready for assertion Revert or clean-up procedures
  • 13. Where to put fixtures: in the same place as the test but it makes the IDE angry in the closest conftest.py conftest is pytest’s __init__.py makes fixture globally available downstream
  • 14. Fixture example import random @pytest.fixture(name="cool_fixture") def this_name_is_just_for_the_function(): yield random.randint() def test_using_fixture(cool_fixture): system_under_test(param=cool_fixture)
  • 15. Test name fixture def test_one(request): test_name = request.node.name result = system_under_test(test_name) assert result == test_name
  • 16. Anti-patterns Copy-paste the same dict for each test Have a thousand JSON files Recommendations Generate them programmatically In the test, highlight the difference Use Mother pattern and data generators
  • 17. Enter factory-boy import factory class UserMother(factory.DictFactory): firstname = "Hector" lastname = "Canto" address = "Praza do Rei, 1, Vigo CP 36000"
  • 18. Enter faker class UserMother(factory.DictFactory): firstname = factory.Faker('first_name') lastname = factory.Faker('last_name') address = factory.Faker('address') random_user = UserMother() random_user == { 'firstname': 'Rebecca', 'lastname': 'Sloan', 'address': '52097 Daniel Ports Apt. 689nPort Jeffrey, NM 55289' }
  • 19. More faker random_user = UserMother(firstname="Guido") random_user == { 'firstname': 'Guido', 'lastname': 'Deleon', 'address': '870 Victoria MillsnWilliamville, CA 44946' }
  • 20. Batch generation Iterated generation UserMother.create_batch(size=5) many = UserMother.create_batch(size=10, firstname=factory.Iterator(["One", "Two", "Three"])) many[0].firstname == "One" many[2].firstname == "Three" many[3].firstname == "One"
  • 21. FactoryBoy with ORMs DjangoORM SQLAlchemy Mogo and MongoEngine not hard to create your own
  • 22. Example class UserMother(factory.orm.SQLAlchemyFactory): class Meta: model = User sqlalchemy_session_factory = lambda: common.TestSession() def test_with_specific_db(test_session): UserMother._meta.sqlalchmey_session = test_session # You can also set the live DB and populate it for demos
  • 23. Set up for SQLA import factory from sqlalchemy import orm TestSession = orm.scoped_session(orm.sessionmaker()) """Global scoped session (thread-safe) for tests""" class BaseFactory(factory.alchemy.SQLAlchemyModelFactory): class Meta: abstract = True sqlalchemy_session = TestSession sqlalchemy_session_persistence = "flush" class UserFactory(BaseFactory): class Meta: model = User
  • 24. Get or Create Fixture # Create user once, use everywhere class UserFactory(BaseUserFactory): class Meta: sqlalchemy_get_or_create = ('id',) user1 = UserFactory(id=1, firstname="Dennis", lastname="Ritchie") user2 = UserFactory(id=1) user1 == user2
  • 25. Good things about factory-boy Highly customizable Related factories Works with ORM
  • 26. Bad things Inner Faker is weird Documentation gaps (as usual) A bit hard to work with relationships
  • 27. For the future polyfactory make your own providers check out Maybe, Traits, post_gen hooks random seed fixing sequence resetting user random.sample and others Tactical tips Add factories to your libraries Specially in serverless and microservices
  • 28. Real case example 3 libraries: common models, common APIs, common 3rd party services On each we have factories to create DB data API callback bodies event message payloads
  • 30. Register fixtures # tests/conftest/py register(AdminFactory, "admin") register(DbUserFactory, "user1", email="user1@hello.es") register(DbUserFactory, "user2", status=0) register(DbProfileFactory2, "profile") # usage def test_with_reg_fx(admin): assert admin.first_name == "Admin"