22.. _`unittest.TestCase` :
33.. _`unittest` :
44
5- Support for unittest.TestCase / Integration of fixtures
6- =====================================================================
7-
8- .. _`unittest.py style` : http://docs.python.org/library/unittest.html
9-
10- ``pytest `` has support for running Python `unittest.py style `_ tests.
11- It's meant for leveraging existing unittest-style projects
12- to use pytest features. Concretely, pytest will automatically
13- collect ``unittest.TestCase `` subclasses and their ``test `` methods in
14- test files. It will invoke typical setup/teardown methods and
15- generally try to make test suites written to run on unittest, to also
16- run using ``pytest ``. We assume here that you are familiar with writing
17- ``unittest.TestCase `` style tests and rather focus on
18- integration aspects.
19-
20- Note that this is meant as a provisional way of running your test code
21- until you fully convert to pytest-style tests. To fully take advantage of
22- :ref: `fixtures <fixture >`, :ref: `parametrization <parametrize >` and
23- :ref: `hooks <writing-plugins >` you should convert (tools like `unittest2pytest
24- <https://pypi.python.org/pypi/unittest2pytest/> `__ are helpful).
25- Also, not all 3rd party pluging are expected to work best with
26- ``unittest.TestCase `` style tests.
27-
28- Usage
29- -------------------------------------------------------------------
30-
31- After :ref: `installation ` type::
32-
33- pytest
34-
35- and you should be able to run your unittest-style tests if they
36- are contained in ``test_* `` modules. If that works for you then
37- you can make use of most :ref: `pytest features <features >`, for example
38- ``--pdb `` debugging in failures, using :ref: `plain assert-statements <assert >`,
39- :ref: `more informative tracebacks <tbreportdemo >`, stdout-capturing or
40- distributing tests to multiple CPUs via the ``-nNUM `` option if you
41- installed the ``pytest-xdist `` plugin. Please refer to
42- the general ``pytest `` documentation for many more examples.
5+ unittest.TestCase Support
6+ =========================
437
44- .. note ::
8+ ``pytest `` supports running Python ``unittest ``-based tests out of the box.
9+ It's meant for leveraging existing ``unittest ``-based test suites
10+ to use pytest as a test runner and also allow to incrementally adapt
11+ the test suite to take full advantage of pytest's features.
12+
13+ To run an existing ``unittest ``-style test suite using ``pytest ``, type::
14+
15+ pytest tests
16+
17+
18+ pytest will automatically collect ``unittest.TestCase `` subclasses and
19+ their ``test `` methods in ``test_*.py `` or ``*_test.py `` files.
20+
21+ Almost all ``unittest `` features are supported:
22+
23+ * ``@unittest.skip `` style decorators;
24+ * ``setUp/tearDown ``;
25+ * ``setUpClass/tearDownClass() ``;
26+
27+ .. _`load_tests protocol` : https://docs.python.org/3/library/unittest.html#load-tests-protocol
28+ .. _`setUpModule/tearDownModule` : https://docs.python.org/3/library/unittest.html#setupmodule-and-teardownmodule
29+ .. _`subtests` : https://docs.python.org/3/library/unittest.html#distinguishing-test-iterations-using-subtests
30+
31+ Up to this point pytest does not have support for the following features:
32+
33+ * `load_tests protocol `_;
34+ * `setUpModule/tearDownModule `_;
35+ * `subtests `_;
36+
37+ Benefits out of the box
38+ -----------------------
39+
40+ By running your test suite with pytest you can make use of several features,
41+ in most cases without having to modify existing code:
42+
43+ * Obtain :ref: `more informative tracebacks <tbreportdemo >`;
44+ * :ref: `stdout and stderr <captures >` capturing;
45+ * :ref: `Test selection options <select-tests >` using ``-k `` and ``-m `` flags;
46+ * :ref: `maxfail `;
47+ * :ref: `--pdb <pdb-option >` command-line option for debugging on test failures
48+ (see :ref: `note <pdb-unittest-note >` below);
49+ * Distribute tests to multiple CPUs using the `pytest-xdist <http://pypi.python.org/pypi/pytest-xdist >`_ plugin;
50+ * Use :ref: `plain assert-statements <assert >` instead of ``self.assert* `` functions (`unittest2pytest
51+ <https://pypi.python.org/pypi/unittest2pytest/> `__ is immensely helpful in this);
4552
46- Running tests from ``unittest.TestCase `` subclasses with ``--pdb `` will
47- disable tearDown and cleanup methods for the case that an Exception
48- occurs. This allows proper post mortem debugging for all applications
49- which have significant logic in their tearDown machinery. However,
50- supporting this feature has the following side effect: If people
51- overwrite ``unittest.TestCase `` ``__call__ `` or ``run ``, they need to
52- to overwrite ``debug `` in the same way (this is also true for standard
53- unittest).
5453
55- Mixing pytest fixtures into unittest.TestCase style tests
56- -----------------------------------------------------------
54+ pytest features in ``unittest.TestCase `` subclasses
55+ ---------------------------------------------------
56+
57+ The following pytest features work in ``unittest.TestCase `` subclasses:
58+
59+ * :ref: `Marks <mark >`: :ref: `skip <skip >`, :ref: `skipif <skipif >`, :ref: `xfail <xfail >`;
60+ * :ref: `Auto-use fixtures <mixing-fixtures >`;
61+
62+ The following pytest features **do not ** work, and probably
63+ never will due to different design philosophies:
64+
65+ * :ref: `Fixtures <fixture >` (except for ``autouse `` fixtures, see :ref: `below <mixing-fixtures >`);
66+ * :ref: `Parametrization <parametrize >`;
67+ * :ref: `Custom hooks <writing-plugins >`;
68+
69+
70+ Third party plugins may or may not work well, depending on the plugin and the test suite.
71+
72+ .. _mixing-fixtures :
73+
74+ Mixing pytest fixtures into ``unittest.TestCase `` subclasses using marks
75+ ------------------------------------------------------------------------
5776
5877Running your unittest with ``pytest `` allows you to use its
5978:ref: `fixture mechanism <fixture >` with ``unittest.TestCase `` style
@@ -143,8 +162,8 @@ share the same ``self.db`` instance which was our intention
143162when writing the class-scoped fixture function above.
144163
145164
146- autouse fixtures and accessing other fixtures
147- -------------------------------------------------------------------
165+ Using autouse fixtures and accessing other fixtures
166+ ---------------------------------------------------
148167
149168Although it's usually better to explicitly declare use of fixtures you need
150169for a given test, you may sometimes want to have fixtures that are
@@ -165,6 +184,7 @@ creation of a per-test temporary directory::
165184 import unittest
166185
167186 class MyTest(unittest.TestCase):
187+
168188 @pytest.fixture(autouse=True)
169189 def initdir(self, tmpdir):
170190 tmpdir.chdir() # change to pytest-provided temporary directory
@@ -200,3 +220,16 @@ was executed ahead of the ``test_method``.
200220
201221 You can also gradually move away from subclassing from ``unittest.TestCase `` to *plain asserts *
202222 and then start to benefit from the full pytest feature set step by step.
223+
224+ .. _pdb-unittest-note :
225+
226+ .. note ::
227+
228+ Running tests from ``unittest.TestCase `` subclasses with ``--pdb `` will
229+ disable tearDown and cleanup methods for the case that an Exception
230+ occurs. This allows proper post mortem debugging for all applications
231+ which have significant logic in their tearDown machinery. However,
232+ supporting this feature has the following side effect: If people
233+ overwrite ``unittest.TestCase `` ``__call__ `` or ``run ``, they need to
234+ to overwrite ``debug `` in the same way (this is also true for standard
235+ unittest).
0 commit comments