60
PyCon AU 2012 Brianna Laugher funcargs & other fun with pytest

Funcargs & other fun with pytest

Embed Size (px)

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.

Citation preview

Page 1: Funcargs & other fun with pytest

PyCon AU 2012Brianna Laugher

funcargs & other fun

with pytest

Page 2: Funcargs & other fun with pytest

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
Page 4: Funcargs & other fun with pytest

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
Page 6: Funcargs & other fun with pytest
Page 7: Funcargs & other fun with pytest
Page 8: Funcargs & other fun with pytest

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
Page 10: Funcargs & other fun with pytest

informative error reporting

Page 11: Funcargs & other fun with pytest

informative error reporting

Page 12: Funcargs & other fun with pytest

extensible

● e.g. add in unittest assert methods

Page 13: Funcargs & other fun with pytest

extensible

● e.g. add hook for winpdb

Page 14: Funcargs & other fun with pytest
Page 15: Funcargs & other fun with pytest

skip

Page 16: Funcargs & other fun with pytest

xfail

Page 17: Funcargs & other fun with pytest

custom markers

Page 18: Funcargs & other fun with pytest
Page 19: Funcargs & other fun with pytest

parametrize

Page 20: Funcargs & other fun with pytest
Page 21: Funcargs & other fun with pytest

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

generate generators!

Page 23: Funcargs & other fun with pytest
Page 24: Funcargs & other fun with pytest

monkeypatch

Page 25: Funcargs & other fun with pytest
Page 26: Funcargs & other fun with pytest
Page 27: Funcargs & other fun with pytest
Page 28: Funcargs & other fun with pytest
Page 29: Funcargs & other fun with pytest

funcargs!

Page 30: Funcargs & other fun with pytest

funcargs – idealised db example

Page 31: Funcargs & other fun with pytest

funcargs – trivial django example

Page 32: Funcargs & other fun with pytest

funcargs – trivial GAE examples

Page 33: Funcargs & other fun with pytest

funcargs – trivial GAE examples

Page 34: Funcargs & other fun with pytest
Page 35: Funcargs & other fun with pytest

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
Page 37: Funcargs & other fun with pytest

funcargs – a real DB example

Page 38: Funcargs & other fun with pytest

funcargs – a real DB example

Page 39: Funcargs & other fun with pytest

funcargs – to express prereq's

● for when a decorator is not powerful enough

Page 40: Funcargs & other fun with pytest
Page 41: Funcargs & other fun with pytest

funcargs – to hide monkeypatch :)

Page 42: Funcargs & other fun with pytest
Page 43: Funcargs & other fun with pytest

generate + funcarg - anypython

Page 44: Funcargs & other fun with pytest
Page 45: Funcargs & other fun with pytest
Page 46: Funcargs & other fun with pytest

pytest – the future

Page 47: Funcargs & other fun with pytest
Page 48: Funcargs & other fun with pytest

test evolution -v0

Page 49: Funcargs & other fun with pytest
Page 50: Funcargs & other fun with pytest

test evolution - v1

Page 51: Funcargs & other fun with pytest

test evolution - v2

Page 52: Funcargs & other fun with pytest

test evolution – v3

Page 53: Funcargs & other fun with pytest

test evolution - v4

Page 54: Funcargs & other fun with pytest
Page 55: Funcargs & other fun with pytest

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
Page 57: Funcargs & other fun with pytest
Page 58: Funcargs & other fun with pytest

● 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

pytest.org

Page 60: Funcargs & other fun with pytest

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