funcargs & other fun with pytest
DESCRIPTION
Video - http://www.youtube.com/watch?v=DTNejE9EraI Code - https://gist.github.com/3386951 Pytest is a mature and comprehensive testing suite for Python projects, but it can be a little intimidating for newcomers. Where do these mysterious funcargs come from, how do parametrised tests work, and where are my xUnit-style setUp and tearDown methods? Pytest lives by "convention over configuration" - which is great once you know what the conventions are. This talk will look at real examples of pytest in use, emphasising the features that differentiate it from nose.TRANSCRIPT
![Page 1: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/1.jpg)
PyCon AU 2012Brianna Laugher
funcargs & other fun
with pytest
![Page 2: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/2.jpg)
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!
![Page 3: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/3.jpg)
![Page 4: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/4.jpg)
my background
● using pytest seriously for ~18 months at work● really enjoying it!● how it feels to use pytest: →
![Page 5: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/5.jpg)
![Page 6: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/6.jpg)
![Page 7: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/7.jpg)
![Page 8: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/8.jpg)
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)
![Page 9: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/9.jpg)
![Page 10: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/10.jpg)
informative error reporting
![Page 11: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/11.jpg)
informative error reporting
![Page 12: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/12.jpg)
extensible
● e.g. add in unittest assert methods
![Page 13: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/13.jpg)
extensible
● e.g. add hook for winpdb
![Page 14: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/14.jpg)
![Page 15: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/15.jpg)
skip
![Page 16: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/16.jpg)
xfail
![Page 17: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/17.jpg)
custom markers
![Page 18: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/18.jpg)
![Page 19: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/19.jpg)
parametrize
![Page 20: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/20.jpg)
![Page 21: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/21.jpg)
generate tests● defined once per module● list/generate data to supply to 1+ tests● way more flexible cf. py.test.mark.parametrize
![Page 22: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/22.jpg)
generate generators!
![Page 23: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/23.jpg)
![Page 24: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/24.jpg)
monkeypatch
![Page 25: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/25.jpg)
![Page 26: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/26.jpg)
![Page 27: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/27.jpg)
![Page 28: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/28.jpg)
![Page 29: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/29.jpg)
funcargs!
![Page 30: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/30.jpg)
funcargs – idealised db example
![Page 31: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/31.jpg)
funcargs – trivial django example
![Page 32: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/32.jpg)
funcargs – trivial GAE examples
![Page 33: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/33.jpg)
funcargs – trivial GAE examples
![Page 34: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/34.jpg)
![Page 35: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/35.jpg)
funcargs – why?
● dependency injection, aka inversion of control● works a treat with TDD● very natural with mocking● encourages modular design
![Page 36: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/36.jpg)
![Page 37: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/37.jpg)
funcargs – a real DB example
![Page 38: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/38.jpg)
funcargs – a real DB example
![Page 39: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/39.jpg)
funcargs – to express prereq's
● for when a decorator is not powerful enough
![Page 40: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/40.jpg)
![Page 41: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/41.jpg)
funcargs – to hide monkeypatch :)
![Page 42: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/42.jpg)
![Page 43: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/43.jpg)
generate + funcarg - anypython
![Page 44: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/44.jpg)
![Page 45: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/45.jpg)
![Page 46: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/46.jpg)
pytest – the future
![Page 47: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/47.jpg)
![Page 48: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/48.jpg)
test evolution -v0
![Page 49: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/49.jpg)
![Page 50: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/50.jpg)
test evolution - v1
![Page 51: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/51.jpg)
test evolution - v2
![Page 52: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/52.jpg)
test evolution – v3
![Page 53: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/53.jpg)
test evolution - v4
![Page 54: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/54.jpg)
![Page 55: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/55.jpg)
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!
![Page 56: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/56.jpg)
![Page 57: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/57.jpg)
![Page 58: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/58.jpg)
● http://codespeak.net/mailman/listinfo/py-dev● testing-in-python mailing list● Stack Overflow● lead developer Holger Krekel is very
responsive
![Page 59: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/59.jpg)
pytest.org
![Page 60: Funcargs & other fun with pytest](https://reader033.vdocument.in/reader033/viewer/2022052618/554941adb4c9050a4d8b5187/html5/thumbnails/60.jpg)
credits, thanks, contactComic 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] / [email protected]
@pfctdayelise