13. py.test
http://pytest.org/
The pytest testing tool makes it easy to write small tests, yet scales to support complex functional
testing. It provides
•auto-discovery of test modules and functions,
•detailed info on failing assert statements (no need to remember self.assert* names)
•fixtures for managing small or parametrized long-lived test resources.
•you can use pytest to run test suites based on unittest (or trial), nose
•single-source compatibility from Python2.6 all the way up to Python3.4, PyPy-2.3
•many external plugins.
14. py.test test discovery
pytest implements the following standard test discovery:
•collection starts from the initial command line arguments which may be directories, filenames or
test ids.
•recurse into directories, unless they match norecursedirs
•test_*.py or *_test.py files, imported by their test package name.
•Test prefixed test classes (without an __init__ method)
•test_ prefixed test functions or methods are test items
16. py.test asserts
================================= FAILURES =================================
___________________________ test_set_comparison ____________________________
def test_set_comparison():
set1 = set("1308")
set2 = set("8035")
> assert set1 == set2
E assert set(['0', '1', '3', '8']) == set(['0', '3', '5', '8'])
E Extra items in the left set:
E '1'
E Extra items in the right set:
E '5'
E Use -v to get the full diff
test_assert2.py:5: AssertionError
========================= 1 failed in 0.01 seconds =========================
17. py.test asserts
Special comparisons are done for a number of cases:
•comparing long strings: a context diff is shown
•comparing long sequences: first failing indices
•comparing dicts: different entries
18. py.test fixtures
The purpose of test fixtures is to provide a fixed baseline upon which tests
can reliably and repeatedly execute. pytest fixtures offer advantage over
the classic xUnit style of setup/teardown functions:
•fixtures have explicit names and are activated by declaring their use from
test functions, modules, classes or whole projects.
•fixtures are implemented in a modular manner, as each fixture name
triggers a fixture function which can itself use other fixtures.
•fixture management scales from simple unit to complex functional testing,
allowing to parametrize fixtures and tests according to configuration and
component options, or to re-use fixtures across class, module or whole test
session scopes.
19. py.test fixtures discovery
The discovery of fixtures functions starts at test classes, then test modules,
then conftest.py files and finally built-in and third party plugins.
23. py.test pytest-xdist
The pytest-xdist plugin extends py.test with some unique test execution modes:
•test run parallelization: if you have multiple CPUs or hosts you can use those for a
combined test run. This allows to speed up development or to use special resources of
remote machines.
•--boxed: (not available on Windows) run each test in a boxed subprocess to survive
SEGFAULTS or otherwise dying processes
•--looponfail: run your tests repeatedly in a subprocess. After each run py.test waits until a
file in your project changes and then re-runs the previously failing tests. This is repeated
until all tests pass after which again a full run is performed.
•Multi-Platform coverage: you can specify different Python interpreters or different
platforms and run tests in parallel on all of them.
25. py.test CI-Travis
Travis CI is a hosted continuous integration service. It is integrated with GitHub.
Travis CI's build environment provides different runtimes for different languages, for
instance multiple versions of Python, Ruby, PHP, Node.js. It also comes preinstalled
with a variety of data stores and common tools like message brokers.