project developer of py.test, tox and execnet Python since around 2000 Test-driven development since 2001 operates a small company “merlinux.eu” doing open source, Python and test-oriented consulting H. Krekel (PyCon DE 2012, Leipzig) py.test - rapid testing Oct 31st, 2012 1 / 27
confidence that code works to specify and document behaviour collaborative and faster development cycles H. Krekel (PyCon DE 2012, Leipzig) py.test - rapid testing Oct 31st, 2012 2 / 27
integration: components co-operate nicely functional: code changes work out in user environments H. Krekel (PyCon DE 2012, Leipzig) py.test - rapid testing Oct 31st, 2012 3 / 27
information when a test fails cross-project, many options and plugins modular and parametrizable fixtures distribute tests to multiple hosts H. Krekel (PyCon DE 2012, Leipzig) py.test - rapid testing Oct 31st, 2012 7 / 27
tool. project specific conftest.py files testing starts from files/dirs or current dir examples: py.test test_file.py py.test path/to/tests H. Krekel (PyCon DE 2012, Leipzig) py.test - rapid testing Oct 31st, 2012 8 / 27
test_*.py test files discovers test_ functions and Test classes automatic discovery avoids boilerplate H. Krekel (PyCon DE 2012, Leipzig) py.test - rapid testing Oct 31st, 2012 9 / 27
of test_module.py def test_something(): x = 3 assert x == 4 class TestSomething: def test_something(self): x = 1 assert x == 5 H. Krekel (PyCon DE 2012, Leipzig) py.test - rapid testing Oct 31st, 2012 10 / 27
are autodiscovered there is no need to subclass anything test classes are for logic grouping of your tests H. Krekel (PyCon DE 2012, Leipzig) py.test - rapid testing Oct 31st, 2012 11 / 27
assertTrue() assert x == 1 # assertEqual(x, 1) assert x != 2 # assertNotEqual(x, 2 assert not x # assertFalse(x) assert x < 3 and y >5 #? H. Krekel (PyCon DE 2012, Leipzig) py.test - rapid testing Oct 31st, 2012 12 / 27
or apps for testing provides test code with “base” app objects very important to avoid repetetive test code in pytest realized via dependency injection: fixture functions create fixtures values test functions and classes can use them H. Krekel (PyCon DE 2012, Leipzig) py.test - rapid testing Oct 31st, 2012 15 / 27
function each fixture has a name (the function name) test functions get it injected as an argument by name next: fixtures can be cached on per scope basis fixture functions can be parametrized classes can use @pytest.mark.usefixtures(name1,...). H. Krekel (PyCon DE 2012, Leipzig) py.test - rapid testing Oct 31st, 2012 17 / 27
fixtures: import pytest @pytest.fixture(params=[1,2]) def answer(request): return request.param now any tests using the answer fixture will run three times! H. Krekel (PyCon DE 2012, Leipzig) py.test - rapid testing Oct 31st, 2012 19 / 27
themselves: import pytest @pytest.fixture def answer10(answer): return answer * 10 If you add this to the previous example, you get a answer10 fixture which internally uses the answer fixture (including parametrization!). Modularity for the win! H. Krekel (PyCon DE 2012, Leipzig) py.test - rapid testing Oct 31st, 2012 20 / 27
usefixtures marker to mark all methods of a test class: import pytest @pytest.mark.usefixtures("cleandir") class TestCommandline: def test_method1(self): ... def test_method2(self): ... H. Krekel (PyCon DE 2012, Leipzig) py.test - rapid testing Oct 31st, 2012 21 / 27
test method where it is an argument or where a usefixture marker is present. it might look like this: # content of conftest.py import pytest @pytest.fixture def cleandir(request, tmpdir): old = tmpdir.chdir() request.addfinalizer(old.chdir) H. Krekel (PyCon DE 2012, Leipzig) py.test - rapid testing Oct 31st, 2012 22 / 27
plugins since developed separately shared many ideas and features since 2008 pytest also has plugins pytest fixtures/asserts not supported in nose pytest can run most nose-based tests suites H. Krekel (PyCon DE 2012, Leipzig) py.test - rapid testing Oct 31st, 2012 24 / 27
assert statement, >30 assertXYZ methods implicit fixtures (setup/teardown methods) extensions are incompatible to each other no parametrized testing no distributed testing pytest can run most unittest-test suites (and trial) H. Krekel (PyCon DE 2012, Leipzig) py.test - rapid testing Oct 31st, 2012 25 / 27
with twisted integration pytest-timeout: time out tests pytest-coverage: test coverage reporting pytest-django: django database testing integration pytest-twisted: twisted testing integration pytest-pep8: configurable pep8-checks pytest-quickcheck: pass typed random data ... H. Krekel (PyCon DE 2012, Leipzig) py.test - rapid testing Oct 31st, 2012 26 / 27
me: holger krekel [email protected] @hpk42 on twitter available for teaching and consulting H. Krekel (PyCon DE 2012, Leipzig) py.test - rapid testing Oct 31st, 2012 27 / 27