tutorial starts, please make sure you have installed the latest pytest. You need either: setuptools: http: //pypi.python.org/pypi/setuptools distribute: http: //pypi.python.org/pypi/distribute and can then type: easy_install py # or pip install py () October 29, 2012 1 / 38
py.test 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 () October 29, 2012 2 / 38
information when a test fails cross-project, many options and plugins cross-python, python2.5 through to 3.3 modular and parametrizable fixtures distribute tests to multiple hosts lots of online and PDF documentation. () October 29, 2012 13 / 38
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 () October 29, 2012 14 / 38
or: hg clone https://bitbucket.org/hpk42/pytest/ run “python setup.py” with “install” or “develop” More notes and info: http://pytest.org/latest/ getting-started.html Exercise: install and run “py.test --help“* Exercise: Write test_module.py with a “test_“ function and a TestClass with test methods and run it () October 29, 2012 20 / 38
of stdout/stderr -x exit instantly on first failure -k run tests whose names contain a keyword -l show locals in tracebacks. --pdb start Python debugger on errors --tb/fulltrace - control traceback generation. Exercise: run tests from last example with these options () October 29, 2012 25 / 38
function using it: # content of test_module.py import pytest @pytest.fixture def somevalue(): return 42 def test_function(somevalue): assert somevalue == 42 Exercise: write a fixture function returning a dummy class instance instead of a simple value () October 29, 2012 28 / 38
each fixture has a unique name (the function name) test functions can get it injected as an argument by name tests choose which fixtures they need move fixture function to a conftest.py file to make it available for all test modules () October 29, 2012 29 / 38
time, pytest @pytest.fixture(scope="module") def answer(): time.sleep(2) return 42 def test_one(answer): assert answer == 42 def test_two(answer): assert answer == 42 Exercise: try it - how long does the test run take? () October 29, 2012 30 / 38
import time, pytest @pytest.fixture(params=[1,2,3]) def answer(request): return request.param the request object allows to query test invocation information, in this case parameters. now any tests using the answer fixture will run three times! Exercise: write a test function accepting an “answer” argument and run it, also write a test that fails () October 29, 2012 31 / 38
functions can use fixtures 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!). () October 29, 2012 32 / 38
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): ... () October 29, 2012 33 / 38
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) () October 29, 2012 34 / 38
plugins since developed separately shares many ideas and features since 2008 pytest also has plugins nose no enriched tracebacks, no plain asserts, no named fixtures, otherwise similar pytest can run most nose-based tests suites by default (unittest as well) () October 29, 2012 37 / 38
on: #pylib on irc.freenode.net or @hpk42 on twitter on stackoverflow with a “pytest” question on the mailing list http://codespeak.net/ mailman/listinfo/py-dev () October 29, 2012 38 / 38