Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Pytest - testing tips and useful plugins

36 views

Published on

Pytest - testing tips and useful plugins

Published in: Technology
  • Be the first to comment

Pytest - testing tips and useful plugins

  1. 1. pytest testing tips and useful plugins
  2. 2. whoami ● Andreu Vallbona @avallbona ● Bachelor degree in computer science ● Professional programmer since 1995 ● Web developer at APSL, Mallorca, Spain
  3. 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. 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. 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. 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
  7. 7. “Talk is cheap. Show me the code.” ― Linus Torvalds
  8. 8. a more in depth insight at: http://talks.apsl.io/testing-pycones-2017/

×