2. whoami
● Andreu Vallbona @avallbona
● Bachelor degree in computer science
● Professional programmer since 1995
● Web developer at APSL, Mallorca, Spain
3. tips
● test database in localhost and/or in memory
● be careful with the Django signals
● mock external services (pytest-mock)
● take advantage of concurrent execution (pytest-xdist)
● reuse database from last run (pytest --reuse-db)
4. tips
● do not overly sophisticate the tests
● use what are, IMHO, the two key features of pytest:
○ fixtures: very easy way to inject the dependencies
(mostly objects) you need in order to recreate your
pre-test scenario
○ parametrize: allow us to execute the same test with
diferents parameters sets (use cases)
5. pytest-django
provide us several fixtures, among others, highlight:
● django_db - gives access to the database
● rf - request factory
● client - test client
● admin_client - authenticated test client
● admin_user - authenticated superuser
● settings - access to the django settings
● mailoutbox - mailbox where to test the sending of emails
6. model-mommy (like factory_boy but easier)
create fixtures based on django models very easily
● field values are generated automatically
● content random of the fields but can be specified individually
● You can create objects:
○ only in memory ( mommy.prepare ) useful for unit tests model methods
○ persistent ( mommy.make ) useful for integration tests
● you can define relationships between objects (fk, m2m)
● you can define recipes, which are like set of objects related to each other
● sequence fields can be defined
● you can create data generators for a given type of field