Switching from nose to py.test at MozillaΒΆ

Matthieu Agopian slides - http://mathieu.agopian.info/presentations/2015_06_djangocon_europe/

glossary

  • test - bit of code to assert other code working
  • test runner - gather tests, run them, report
  • test suite, build - full collection
  • fixture - data fixture (django) OR dependency injection (pytest)

What is pytest

  • Holger Krekel
  • no boiler place tests (eg assert 1 + 1 == 2) and still get meaningful report
  • plugins
  • 10 slowest tests –duration=10
  • modify tracebacks
  • drop to pdb on fail –pdb
  • output capture on by default - only display for fails
  • mark tests @pytest.mark.functional - run separately
  • awesome failure reports - eg comparing dictionaries

dependency injection

  • instead of setUp/tearDown ...
  • eg. rf, settings provided by pytest-django (request factory, django settings)

parameterisation

  • have same test run multiple times with different inputs and expected outputs

config

  • pytest.ini - markers to use, default args, test files
  • conftest.py - locally for current folder and children - eg use another settings file

plugins

  • pytest-django
    • –create-db, –reuse-db, to save time
    • @pytest.mark.django_db - explicit, see tests that will be slow. Can be set in conftest.py
  • pytest-cov
  • pytest-cache (–lf - only run last failed test)
  • pytest-xdist

why change?

  • is better maintained
  • compatible with current tests
  • will be better for future tests

why not?

  • no dependency injection for nose classes
  • can still use the autouse fixtures
  • no data fixture bundling

can convert some asserts quickly

Previous topic

Salt for Django Developers

Next topic

A sincere tale of Django, developers and security

This Page