Skip to content

Commit 15cdf13

Browse files
committed
Document which pytest features work with unittest
Fix #2626
1 parent 8969bd4 commit 15cdf13

File tree

5 files changed

+97
-51
lines changed

5 files changed

+97
-51
lines changed

changelog/2626.doc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Explicitly document which pytest features work with ``unittest``.

doc/en/mark.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ By using the ``pytest.mark`` helper you can easily set
1010
metadata on your test functions. There are
1111
some builtin markers, for example:
1212

13+
* :ref:`skip <skip>` - always skip a test function
1314
* :ref:`skipif <skipif>` - skip a test function if a certain condition is met
1415
* :ref:`xfail <xfail>` - produce an "expected failure" outcome if a certain
1516
condition is met

doc/en/skipping.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ corresponding to the "short" letters shown in the test progress::
2727
(See :ref:`how to change command line options defaults`)
2828

2929
.. _skipif:
30+
.. _skip:
3031
.. _`condition booleans`:
3132

3233
Skipping test functions

doc/en/unittest.rst

Lines changed: 87 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,80 @@
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+
.. _`unittest.py style`: https://docs.python.org/3/library/unittest.html
459

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`_;
5455

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+
------------------------------------------------------------------------
5779

5880
Running your unittest with ``pytest`` allows you to use its
5981
:ref:`fixture mechanism <fixture>` with ``unittest.TestCase`` style
@@ -143,8 +165,8 @@ share the same ``self.db`` instance which was our intention
143165
when writing the class-scoped fixture function above.
144166

145167

146-
autouse fixtures and accessing other fixtures
147-
-------------------------------------------------------------------
168+
Using autouse fixtures and accessing other fixtures
169+
---------------------------------------------------
148170

149171
Although it's usually better to explicitly declare use of fixtures you need
150172
for a given test, you may sometimes want to have fixtures that are
@@ -165,6 +187,7 @@ creation of a per-test temporary directory::
165187
import unittest
166188

167189
class MyTest(unittest.TestCase):
190+
168191
@pytest.fixture(autouse=True)
169192
def initdir(self, tmpdir):
170193
tmpdir.chdir() # change to pytest-provided temporary directory
@@ -200,3 +223,16 @@ was executed ahead of the ``test_method``.
200223

201224
You can also gradually move away from subclassing from ``unittest.TestCase`` to *plain asserts*
202225
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).

doc/en/usage.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ Getting help on version, option names, environment variables
4141
pytest -h | --help # show help on command line and config file options
4242

4343

44+
.. _maxfail:
45+
4446
Stopping after the first (or N) failures
4547
---------------------------------------------------
4648

@@ -49,6 +51,8 @@ To stop the testing process after the first (N) failures::
4951
pytest -x # stop after first failure
5052
pytest --maxfail=2 # stop after two failures
5153

54+
.. _select-tests:
55+
5256
Specifying tests / selecting tests
5357
---------------------------------------------------
5458

@@ -135,6 +139,9 @@ with Ctrl+C to find out where the tests are *hanging*. By default no output
135139
will be shown (because KeyboardInterrupt is caught by pytest). By using this
136140
option you make sure a trace is shown.
137141

142+
143+
.. _pdb-option:
144+
138145
Dropping to PDB_ (Python Debugger) on failures
139146
-----------------------------------------------
140147

0 commit comments

Comments
 (0)