2
2
.. _`unittest.TestCase` :
3
3
.. _`unittest` :
4
4
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
+ =========================
43
7
44
- .. note ::
8
+ .. _ `unittest.py style` : https://docs.python.org/3/library/unittest.html
45
9
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).
10
+ ``pytest `` supports running Python `unittest.py style `_ tests out of the box.
11
+ It's meant for leveraging existing ``unittest ``-style test suites
12
+ to use pytest as a test runner and also allow to incrementally adapt
13
+ the test suite to take full advantage of pytest features.
14
+
15
+ To run an existing ``unittest ``-style test suite using ``pytest ``, simply type::
16
+
17
+ pytest tests
18
+
19
+
20
+ pytest will automatically collect ``unittest.TestCase `` subclasses and
21
+ their ``test `` methods in ``test_*.py `` or ``*_test.py `` files.
22
+
23
+ Almost all ``unittest `` features are supported, like ``@unittest.skip `` style decorators, ``setUp/tearDown ``,
24
+ ``setUpClass/tearDownClass() ``, etc (see :ref: `unittest-limitations `).
25
+
26
+ Benefits
27
+ --------
28
+
29
+ You can make use of several pytest features, most without changing any existing code:
30
+
31
+ * Use :ref: `plain assert-statements <assert >` instead of ``self.assert* `` functions (`unittest2pytest
32
+ <https://pypi.python.org/pypi/unittest2pytest/> `__ is immensely helpful in this);
33
+ * Obtain :ref: `more informative tracebacks <tbreportdemo >`;
34
+ * :ref: `stdout and stderr <captures >` capturing;
35
+ * :ref: `Test selection options <select-tests >` using ``-k `` and ``-m `` flags;
36
+ * :ref: `maxfail `;
37
+ * :ref: `--pdb <pdb-option >` command-line option for debugging on test failures
38
+ (see :ref: `note <pdb-unittest-note >` below);
39
+ * Distribute tests to multiple CPUs using the `pytest-xdist <http://pypi.python.org/pypi/pytest-xdist >`_ plugin;
40
+
41
+ .. _unittest-limitations :
42
+
43
+ Limitations
44
+ -----------
45
+
46
+ .. _`load_tests protocol` : https://docs.python.org/3/library/unittest.html#load-tests-protocol
47
+ .. _`setUpModule/tearDownModule` : https://docs.python.org/3/library/unittest.html#setupmodule-and-teardownmodule
48
+ .. _`subtests` : https://docs.python.org/3/library/unittest.html#distinguishing-test-iterations-using-subtests
49
+
50
+ Pytest currently does not support the following ``unittest `` features:
51
+
52
+ * `load_tests protocol `_;
53
+ * `setUpModule/tearDownModule `_;
54
+ * `subtests `_;
54
55
55
- Mixing pytest fixtures into unittest.TestCase style tests
56
- -----------------------------------------------------------
56
+
57
+ pytest features in ``unittest.TestCase `` subclasses
58
+ ---------------------------------------------------
59
+
60
+ The following pytest features work in ``unittest.TestCase `` subclasses:
61
+
62
+ * :ref: `Marks <mark >`: :ref: `skip <skip >`, :ref: `skipif <skipif >`, :ref: `xfail <xfail >`;
63
+ * :ref: `Auto-use fixtures <mixing-fixtures >`;
64
+
65
+ The following pytest features **do not ** work, and probably
66
+ never will due to different design philosophies:
67
+
68
+ * :ref: `Fixtures <fixture >` (except for ``autouse `` fixtures, see :ref: `below <mixing-fixtures >`);
69
+ * :ref: `Parametrization <parametrize >`;
70
+ * :ref: `Custom hooks <writing-plugins >`;
71
+
72
+
73
+ Third party plugins may or may not work well, depending on the plugin and the test suite.
74
+
75
+ .. _mixing-fixtures :
76
+
77
+ Mixing pytest fixtures into ``unittest.TestCase `` subclasses using marks
78
+ ------------------------------------------------------------------------
57
79
58
80
Running your unittest with ``pytest `` allows you to use its
59
81
:ref: `fixture mechanism <fixture >` with ``unittest.TestCase `` style
@@ -143,8 +165,8 @@ share the same ``self.db`` instance which was our intention
143
165
when writing the class-scoped fixture function above.
144
166
145
167
146
- autouse fixtures and accessing other fixtures
147
- -------------------------------------------------------------------
168
+ Using autouse fixtures and accessing other fixtures
169
+ ---------------------------------------------------
148
170
149
171
Although it's usually better to explicitly declare use of fixtures you need
150
172
for a given test, you may sometimes want to have fixtures that are
@@ -165,6 +187,7 @@ creation of a per-test temporary directory::
165
187
import unittest
166
188
167
189
class MyTest(unittest.TestCase):
190
+
168
191
@pytest.fixture(autouse=True)
169
192
def initdir(self, tmpdir):
170
193
tmpdir.chdir() # change to pytest-provided temporary directory
@@ -200,3 +223,16 @@ was executed ahead of the ``test_method``.
200
223
201
224
You can also gradually move away from subclassing from ``unittest.TestCase `` to *plain asserts *
202
225
and then start to benefit from the full pytest feature set step by step.
226
+
227
+ .. _pdb-unittest-note :
228
+
229
+ .. note ::
230
+
231
+ Running tests from ``unittest.TestCase `` subclasses with ``--pdb `` will
232
+ disable tearDown and cleanup methods for the case that an Exception
233
+ occurs. This allows proper post mortem debugging for all applications
234
+ which have significant logic in their tearDown machinery. However,
235
+ supporting this feature has the following side effect: If people
236
+ overwrite ``unittest.TestCase `` ``__call__ `` or ``run ``, they need to
237
+ to overwrite ``debug `` in the same way (this is also true for standard
238
+ unittest).
0 commit comments