$30 off During Our Annual Pro Sale. View Details »

Funcargs and other fun with pytest

Funcargs and other fun with pytest

Presented at PyCon AU 2012.

Brianna Laugher

August 18, 2012
Tweet

More Decks by Brianna Laugher

Other Decks in Technology

Transcript

  1. PyCon AU 2012
    Brianna Laugher
    funcargs &
    other fun
    with
    pytest

    View Slide

  2. pytest – all the good stuff

    all test styles: unittest, doctest, nose

    boilerplate-free style – natural asserts

    support for running tests distributed, boxed

    generate JUnit-style XML for Jenkins CI

    informative error reporting

    extensible plugins, useful builtins

    skip, xfail, custom markers

    parametrize, generate tests

    monkeypatch

    funcargs!

    View Slide

  3. View Slide

  4. my background

    using pytest seriously for ~18 months at work

    really enjoying it!

    how it feels to use pytest: →

    View Slide

  5. View Slide

  6. View Slide

  7. View Slide

  8. Graphical Forecast Editor

    client/server desktop application used by
    forecasters to generate the (text & graphical)
    forecasts

    pilot in VIC, funding to roll out nationally, ~5
    year project, 20+ new devs/testers

    existing codebase of Python and C++ (used
    operationally in US)

    home-grown test suite and runner (via GUI)

    View Slide

  9. View Slide

  10. informative error reporting

    View Slide

  11. informative error reporting

    View Slide

  12. extensible

    e.g. add in unittest assert methods

    View Slide

  13. extensible

    e.g. add hook for winpdb

    View Slide

  14. View Slide

  15. skip

    View Slide

  16. xfail

    View Slide

  17. custom markers

    View Slide

  18. View Slide

  19. parametrize

    View Slide

  20. View Slide

  21. generate tests

    defined once per module

    list/generate data to supply to 1+ tests

    way more flexible cf. py.test.mark.parametrize

    View Slide

  22. generate generators!

    View Slide

  23. View Slide

  24. monkeypatch

    View Slide

  25. View Slide

  26. View Slide

  27. View Slide

  28. View Slide

  29. funcargs!

    View Slide

  30. funcargs – idealised db example

    View Slide

  31. funcargs – trivial django example

    View Slide

  32. funcargs – trivial GAE examples

    View Slide

  33. funcargs – trivial GAE examples

    View Slide

  34. View Slide

  35. funcargs – why?

    dependency injection, aka inversion of control

    works a treat with TDD

    very natural with mocking

    encourages modular design

    View Slide

  36. View Slide

  37. funcargs – a real DB example

    View Slide

  38. funcargs – a real DB example

    View Slide

  39. funcargs – to express prereq's

    for when a decorator is not powerful enough

    View Slide

  40. View Slide

  41. funcargs – to hide monkeypatch :)

    View Slide

  42. View Slide

  43. generate + funcarg - anypython

    View Slide

  44. View Slide

  45. View Slide

  46. pytest – the future

    View Slide

  47. View Slide

  48. test evolution -v0

    View Slide

  49. View Slide

  50. test evolution - v1

    View Slide

  51. test evolution - v2

    View Slide

  52. test evolution – v3

    View Slide

  53. test evolution - v4

    View Slide

  54. View Slide

  55. pytest – all the good stuff

    all test styles: unittest, doctest, nose

    boilerplate-free style – natural asserts

    support for running tests distributed, boxed

    generate JUnit-style XML for Jenkins CI

    informative error reporting

    extensible plugins, useful builtins

    skip, xfail, custom markers

    parametrize, generate tests

    monkeypatch

    funcargs!

    View Slide

  56. View Slide

  57. View Slide


  58. http://codespeak.net/mailman/listinfo/py-dev

    testing-in-python mailing list

    Stack Overflow

    lead developer Holger Krekel is very
    responsive

    View Slide

  59. pytest.org

    View Slide

  60. credits, thanks, contact
    Comic panels from comicallyvintage.tumblr.com
    Bureau of Meteorology

    In particular, Darius Powell introduced pytest to our
    project
    Code examples: gist.github.com/3386951
    [email protected]
    brianna.laugher.id.au / [email protected]
    @pfctdayelise

    View Slide