Skip to content

Commit d5f4496

Browse files
Merge pull request #2656 from nicoddemus/unittest-features
Document which pytest features work with `unittest`
2 parents 12e6095 + 37353a8 commit d5f4496

File tree

5 files changed

+94
-51
lines changed

5 files changed

+94
-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: 84 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,58 +2,77 @@
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

5877
Running 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
143162
when 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

149168
Although it's usually better to explicitly declare use of fixtures you need
150169
for 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).

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)