Upgrade to Pro — share decks privately, control downloads, hide ads and more …

pytest - testing tips and useful plugins

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.

pytest - testing tips and useful plugins

Avatar for Andreu Vallbona Plazas

Andreu Vallbona Plazas

May 25, 2018

More Decks by Andreu Vallbona Plazas

Other Decks in Programming

Transcript

  1. whoami • Andreu Vallbona @avallbona • Bachelor degree in computer

    science • Professional programmer since 1995 • Web developer at APSL, Mallorca, Spain
  2. 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)
  3. 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)
  4. 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
  5. 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