2010-11-06 06:37:25 +08:00
|
|
|
|
|
|
|
.. _`unittest.TestCase`:
|
2016-08-23 10:35:41 +08:00
|
|
|
.. _`unittest`:
|
2010-11-06 06:37:25 +08:00
|
|
|
|
2021-03-15 16:22:11 +08:00
|
|
|
How to use ``unittest``-based tests with pytest
|
|
|
|
===============================================
|
2012-10-12 20:52:36 +08:00
|
|
|
|
2017-08-05 04:56:13 +08:00
|
|
|
``pytest`` supports running Python ``unittest``-based tests out of the box.
|
|
|
|
It's meant for leveraging existing ``unittest``-based test suites
|
2017-08-04 09:00:11 +08:00
|
|
|
to use pytest as a test runner and also allow to incrementally adapt
|
2017-08-05 04:56:13 +08:00
|
|
|
the test suite to take full advantage of pytest's features.
|
2017-08-04 09:00:11 +08:00
|
|
|
|
2019-08-07 07:20:06 +08:00
|
|
|
To run an existing ``unittest``-style test suite using ``pytest``, type:
|
2017-08-04 09:00:11 +08:00
|
|
|
|
2019-08-07 04:25:54 +08:00
|
|
|
.. code-block:: bash
|
|
|
|
|
2017-08-04 09:00:11 +08:00
|
|
|
pytest tests
|
|
|
|
|
|
|
|
|
|
|
|
pytest will automatically collect ``unittest.TestCase`` subclasses and
|
|
|
|
their ``test`` methods in ``test_*.py`` or ``*_test.py`` files.
|
|
|
|
|
2017-08-05 04:56:13 +08:00
|
|
|
Almost all ``unittest`` features are supported:
|
2017-08-04 09:00:11 +08:00
|
|
|
|
2017-08-05 04:56:13 +08:00
|
|
|
* ``@unittest.skip`` style decorators;
|
|
|
|
* ``setUp/tearDown``;
|
2018-10-03 20:36:13 +08:00
|
|
|
* ``setUpClass/tearDownClass``;
|
|
|
|
* ``setUpModule/tearDownModule``;
|
2017-08-04 09:00:11 +08:00
|
|
|
|
2022-05-12 19:16:25 +08:00
|
|
|
.. _`pytest-subtests`: https://github.com/pytest-dev/pytest-subtests
|
2021-09-27 06:42:37 +08:00
|
|
|
.. _`load_tests protocol`: https://docs.python.org/3/library/unittest.html#load-tests-protocol
|
2017-08-04 09:00:11 +08:00
|
|
|
|
2022-05-12 19:16:25 +08:00
|
|
|
Additionally, :ref:`subtests <python:subtests>` are supported by the
|
|
|
|
`pytest-subtests`_ plugin.
|
|
|
|
|
2017-08-05 04:56:13 +08:00
|
|
|
Up to this point pytest does not have support for the following features:
|
2017-08-04 09:00:11 +08:00
|
|
|
|
|
|
|
* `load_tests protocol`_;
|
2016-09-01 04:33:47 +08:00
|
|
|
|
2017-08-05 04:56:13 +08:00
|
|
|
Benefits out of the box
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
By running your test suite with pytest you can make use of several features,
|
|
|
|
in most cases without having to modify existing code:
|
|
|
|
|
|
|
|
* Obtain :ref:`more informative tracebacks <tbreportdemo>`;
|
|
|
|
* :ref:`stdout and stderr <captures>` capturing;
|
|
|
|
* :ref:`Test selection options <select-tests>` using ``-k`` and ``-m`` flags;
|
|
|
|
* :ref:`maxfail`;
|
|
|
|
* :ref:`--pdb <pdb-option>` command-line option for debugging on test failures
|
|
|
|
(see :ref:`note <pdb-unittest-note>` below);
|
2021-10-22 20:47:57 +08:00
|
|
|
* Distribute tests to multiple CPUs using the :pypi:`pytest-xdist` plugin;
|
|
|
|
* Use :ref:`plain assert-statements <assert>` instead of ``self.assert*`` functions
|
|
|
|
(:pypi:`unittest2pytest` is immensely helpful in this);
|
2017-08-05 04:56:13 +08:00
|
|
|
|
2017-08-04 09:00:11 +08:00
|
|
|
|
|
|
|
pytest features in ``unittest.TestCase`` subclasses
|
|
|
|
---------------------------------------------------
|
|
|
|
|
|
|
|
The following pytest features work in ``unittest.TestCase`` subclasses:
|
|
|
|
|
|
|
|
* :ref:`Marks <mark>`: :ref:`skip <skip>`, :ref:`skipif <skipif>`, :ref:`xfail <xfail>`;
|
|
|
|
* :ref:`Auto-use fixtures <mixing-fixtures>`;
|
|
|
|
|
|
|
|
The following pytest features **do not** work, and probably
|
|
|
|
never will due to different design philosophies:
|
|
|
|
|
|
|
|
* :ref:`Fixtures <fixture>` (except for ``autouse`` fixtures, see :ref:`below <mixing-fixtures>`);
|
|
|
|
* :ref:`Parametrization <parametrize>`;
|
|
|
|
* :ref:`Custom hooks <writing-plugins>`;
|
|
|
|
|
|
|
|
|
|
|
|
Third party plugins may or may not work well, depending on the plugin and the test suite.
|
|
|
|
|
|
|
|
.. _mixing-fixtures:
|
|
|
|
|
|
|
|
Mixing pytest fixtures into ``unittest.TestCase`` subclasses using marks
|
|
|
|
------------------------------------------------------------------------
|
2012-10-12 20:52:36 +08:00
|
|
|
|
2014-01-18 19:31:33 +08:00
|
|
|
Running your unittest with ``pytest`` allows you to use its
|
2012-10-19 16:07:11 +08:00
|
|
|
:ref:`fixture mechanism <fixture>` with ``unittest.TestCase`` style
|
|
|
|
tests. Assuming you have at least skimmed the pytest fixture features,
|
|
|
|
let's jump-start into an example that integrates a pytest ``db_class``
|
|
|
|
fixture, setting up a class-cached database object, and then reference
|
2019-08-07 07:20:06 +08:00
|
|
|
it from a unittest-style test:
|
2012-10-12 20:52:36 +08:00
|
|
|
|
2019-08-07 04:25:54 +08:00
|
|
|
.. code-block:: python
|
|
|
|
|
2012-10-12 20:52:36 +08:00
|
|
|
# content of conftest.py
|
|
|
|
|
2012-10-19 16:07:11 +08:00
|
|
|
# we define a fixture function below and it will be "used" by
|
|
|
|
# referencing its name from tests
|
2012-10-12 20:52:36 +08:00
|
|
|
|
|
|
|
import pytest
|
2010-10-12 16:59:04 +08:00
|
|
|
|
2019-08-07 04:34:58 +08:00
|
|
|
|
2012-10-12 20:52:36 +08:00
|
|
|
@pytest.fixture(scope="class")
|
|
|
|
def db_class(request):
|
2017-02-17 02:41:51 +08:00
|
|
|
class DummyDB:
|
2012-10-12 20:52:36 +08:00
|
|
|
pass
|
2019-08-07 04:34:58 +08:00
|
|
|
|
2018-05-18 16:19:46 +08:00
|
|
|
# set a class attribute on the invoking test context
|
2012-10-12 20:52:36 +08:00
|
|
|
request.cls.db = DummyDB()
|
|
|
|
|
2018-05-18 16:19:46 +08:00
|
|
|
This defines a fixture function ``db_class`` which - if used - is
|
|
|
|
called once for each test class and which sets the class-level
|
2012-10-12 20:52:36 +08:00
|
|
|
``db`` attribute to a ``DummyDB`` instance. The fixture function
|
|
|
|
achieves this by receiving a special ``request`` object which gives
|
|
|
|
access to :ref:`the requesting test context <request-context>` such
|
2018-05-18 16:19:46 +08:00
|
|
|
as the ``cls`` attribute, denoting the class from which the fixture
|
2012-10-12 20:52:36 +08:00
|
|
|
is used. This architecture de-couples fixture writing from actual test
|
|
|
|
code and allows re-use of the fixture by a minimal reference, the fixture
|
2018-05-18 16:19:46 +08:00
|
|
|
name. So let's write an actual ``unittest.TestCase`` class using our
|
2019-08-07 07:20:06 +08:00
|
|
|
fixture definition:
|
2012-10-12 20:52:36 +08:00
|
|
|
|
2019-08-07 04:25:54 +08:00
|
|
|
.. code-block:: python
|
|
|
|
|
2012-10-12 20:52:36 +08:00
|
|
|
# content of test_unittest_db.py
|
2010-10-12 16:59:04 +08:00
|
|
|
|
|
|
|
import unittest
|
2022-04-28 22:30:16 +08:00
|
|
|
|
2012-10-12 20:52:36 +08:00
|
|
|
import pytest
|
|
|
|
|
2019-08-07 04:34:58 +08:00
|
|
|
|
2012-10-12 20:52:36 +08:00
|
|
|
@pytest.mark.usefixtures("db_class")
|
2010-10-12 16:59:04 +08:00
|
|
|
class MyTest(unittest.TestCase):
|
2012-10-12 20:52:36 +08:00
|
|
|
def test_method1(self):
|
|
|
|
assert hasattr(self, "db")
|
|
|
|
assert 0, self.db # fail for demo purposes
|
|
|
|
|
|
|
|
def test_method2(self):
|
|
|
|
assert 0, self.db # fail for demo purposes
|
|
|
|
|
2018-05-18 16:19:46 +08:00
|
|
|
The ``@pytest.mark.usefixtures("db_class")`` class-decorator makes sure that
|
2012-10-19 16:07:11 +08:00
|
|
|
the pytest fixture function ``db_class`` is called once per class.
|
2012-10-18 18:24:50 +08:00
|
|
|
Due to the deliberately failing assert statements, we can take a look at
|
2018-11-24 13:41:22 +08:00
|
|
|
the ``self.db`` values in the traceback:
|
|
|
|
|
|
|
|
.. code-block:: pytest
|
2010-10-12 16:59:04 +08:00
|
|
|
|
2016-06-21 22:16:57 +08:00
|
|
|
$ pytest test_unittest_db.py
|
2017-11-23 23:33:41 +08:00
|
|
|
=========================== test session starts ============================
|
2024-01-02 16:58:20 +08:00
|
|
|
platform linux -- Python 3.x.y, pytest-8.x.y, pluggy-1.x.y
|
2021-10-04 14:56:26 +08:00
|
|
|
rootdir: /home/sweet/project
|
2012-10-12 20:52:36 +08:00
|
|
|
collected 2 items
|
2018-05-18 16:19:46 +08:00
|
|
|
|
2017-11-23 23:33:41 +08:00
|
|
|
test_unittest_db.py FF [100%]
|
2018-05-18 16:19:46 +08:00
|
|
|
|
2017-11-23 23:33:41 +08:00
|
|
|
================================= FAILURES =================================
|
|
|
|
___________________________ MyTest.test_method1 ____________________________
|
2018-05-18 16:19:46 +08:00
|
|
|
|
2012-10-12 20:52:36 +08:00
|
|
|
self = <test_unittest_db.MyTest testMethod=test_method1>
|
2018-05-18 16:19:46 +08:00
|
|
|
|
2012-10-12 20:52:36 +08:00
|
|
|
def test_method1(self):
|
|
|
|
assert hasattr(self, "db")
|
2019-08-16 08:00:09 +08:00
|
|
|
> assert 0, self.db # fail for demo purposes
|
2021-10-04 14:56:26 +08:00
|
|
|
E AssertionError: <conftest.db_class.<locals>.DummyDB object at 0xdeadbeef0001>
|
2014-09-24 20:46:56 +08:00
|
|
|
E assert 0
|
2018-05-18 16:19:46 +08:00
|
|
|
|
2022-10-25 19:12:55 +08:00
|
|
|
test_unittest_db.py:11: AssertionError
|
2017-11-23 23:33:41 +08:00
|
|
|
___________________________ MyTest.test_method2 ____________________________
|
2018-05-18 16:19:46 +08:00
|
|
|
|
2012-10-12 20:52:36 +08:00
|
|
|
self = <test_unittest_db.MyTest testMethod=test_method2>
|
2018-05-18 16:19:46 +08:00
|
|
|
|
2012-10-12 20:52:36 +08:00
|
|
|
def test_method2(self):
|
2019-08-16 08:00:09 +08:00
|
|
|
> assert 0, self.db # fail for demo purposes
|
2021-10-04 14:56:26 +08:00
|
|
|
E AssertionError: <conftest.db_class.<locals>.DummyDB object at 0xdeadbeef0001>
|
2014-09-24 20:46:56 +08:00
|
|
|
E assert 0
|
2018-05-18 16:19:46 +08:00
|
|
|
|
2022-10-25 19:12:55 +08:00
|
|
|
test_unittest_db.py:14: AssertionError
|
2020-03-11 22:23:25 +08:00
|
|
|
========================= short test summary info ==========================
|
|
|
|
FAILED test_unittest_db.py::MyTest::test_method1 - AssertionError: <conft...
|
|
|
|
FAILED test_unittest_db.py::MyTest::test_method2 - AssertionError: <conft...
|
2019-08-30 23:43:47 +08:00
|
|
|
============================ 2 failed in 0.12s =============================
|
2012-10-12 20:52:36 +08:00
|
|
|
|
2012-10-19 16:07:11 +08:00
|
|
|
This default pytest traceback shows that the two test methods
|
2012-10-18 18:24:50 +08:00
|
|
|
share the same ``self.db`` instance which was our intention
|
|
|
|
when writing the class-scoped fixture function above.
|
2010-10-12 16:59:04 +08:00
|
|
|
|
|
|
|
|
2017-08-04 09:00:11 +08:00
|
|
|
Using autouse fixtures and accessing other fixtures
|
|
|
|
---------------------------------------------------
|
2012-09-18 16:54:12 +08:00
|
|
|
|
2015-11-28 14:46:45 +08:00
|
|
|
Although it's usually better to explicitly declare use of fixtures you need
|
2018-05-18 16:19:46 +08:00
|
|
|
for a given test, you may sometimes want to have fixtures that are
|
|
|
|
automatically used in a given context. After all, the traditional
|
2012-10-19 16:07:11 +08:00
|
|
|
style of unittest-setup mandates the use of this implicit fixture writing
|
2018-05-18 16:19:46 +08:00
|
|
|
and chances are, you are used to it or like it.
|
2012-10-19 16:07:11 +08:00
|
|
|
|
|
|
|
You can flag fixture functions with ``@pytest.fixture(autouse=True)``
|
|
|
|
and define the fixture function in the context where you want it used.
|
|
|
|
Let's look at an ``initdir`` fixture which makes all test methods of a
|
|
|
|
``TestCase`` class execute in a temporary directory with a
|
|
|
|
pre-initialized ``samplefile.ini``. Our ``initdir`` fixture itself uses
|
2021-03-14 03:22:54 +08:00
|
|
|
the pytest builtin :fixture:`tmp_path` fixture to delegate the
|
2019-08-07 07:20:06 +08:00
|
|
|
creation of a per-test temporary directory:
|
2012-10-12 20:52:36 +08:00
|
|
|
|
2019-08-07 04:25:54 +08:00
|
|
|
.. code-block:: python
|
|
|
|
|
2012-10-12 20:52:36 +08:00
|
|
|
# content of test_unittest_cleandir.py
|
2012-09-18 16:54:12 +08:00
|
|
|
import unittest
|
|
|
|
|
2022-04-28 22:30:16 +08:00
|
|
|
import pytest
|
|
|
|
|
2017-08-04 09:00:11 +08:00
|
|
|
|
2012-09-18 16:54:12 +08:00
|
|
|
class MyTest(unittest.TestCase):
|
2012-10-12 20:52:36 +08:00
|
|
|
@pytest.fixture(autouse=True)
|
2021-03-14 03:22:54 +08:00
|
|
|
def initdir(self, tmp_path, monkeypatch):
|
|
|
|
monkeypatch.chdir(tmp_path) # change to pytest-provided temporary directory
|
2023-07-09 02:40:05 +08:00
|
|
|
tmp_path.joinpath("samplefile.ini").write_text("# testdata", encoding="utf-8")
|
2012-09-18 16:54:12 +08:00
|
|
|
|
|
|
|
def test_method(self):
|
2023-07-09 02:40:05 +08:00
|
|
|
with open("samplefile.ini", encoding="utf-8") as f:
|
2017-05-16 07:25:53 +08:00
|
|
|
s = f.read()
|
2012-09-18 16:54:12 +08:00
|
|
|
assert "testdata" in s
|
|
|
|
|
2012-10-18 18:24:50 +08:00
|
|
|
Due to the ``autouse`` flag the ``initdir`` fixture function will be
|
|
|
|
used for all methods of the class where it is defined. This is a
|
2012-10-19 16:07:11 +08:00
|
|
|
shortcut for using a ``@pytest.mark.usefixtures("initdir")`` marker
|
|
|
|
on the class like in the previous example.
|
2012-09-18 16:54:12 +08:00
|
|
|
|
2018-11-24 13:41:22 +08:00
|
|
|
Running this test module ...:
|
|
|
|
|
|
|
|
.. code-block:: pytest
|
2012-09-18 16:54:12 +08:00
|
|
|
|
2016-06-21 22:16:57 +08:00
|
|
|
$ pytest -q test_unittest_cleandir.py
|
2017-11-23 23:33:41 +08:00
|
|
|
. [100%]
|
2019-09-18 21:11:59 +08:00
|
|
|
1 passed in 0.12s
|
2012-10-07 19:06:17 +08:00
|
|
|
|
2012-10-12 20:52:36 +08:00
|
|
|
... gives us one passed test because the ``initdir`` fixture function
|
|
|
|
was executed ahead of the ``test_method``.
|
2012-10-07 19:06:17 +08:00
|
|
|
|
2012-10-12 20:52:36 +08:00
|
|
|
.. note::
|
2012-10-07 19:06:17 +08:00
|
|
|
|
2017-07-28 07:02:18 +08:00
|
|
|
``unittest.TestCase`` methods cannot directly receive fixture
|
|
|
|
arguments as implementing that is likely to inflict
|
2012-10-12 20:52:36 +08:00
|
|
|
on the ability to run general unittest.TestCase test suites.
|
2017-07-28 07:02:18 +08:00
|
|
|
|
|
|
|
The above ``usefixtures`` and ``autouse`` examples should help to mix in
|
|
|
|
pytest fixtures into unittest suites.
|
|
|
|
|
2017-07-29 10:15:59 +08:00
|
|
|
You can also gradually move away from subclassing from ``unittest.TestCase`` to *plain asserts*
|
|
|
|
and then start to benefit from the full pytest feature set step by step.
|
2017-08-04 09:00:11 +08:00
|
|
|
|
|
|
|
.. _pdb-unittest-note:
|
|
|
|
|
2017-10-22 06:12:49 +08:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
Due to architectural differences between the two frameworks, setup and
|
|
|
|
teardown for ``unittest``-based tests is performed during the ``call`` phase
|
|
|
|
of testing instead of in ``pytest``'s standard ``setup`` and ``teardown``
|
|
|
|
stages. This can be important to understand in some situations, particularly
|
|
|
|
when reasoning about errors. For example, if a ``unittest``-based suite
|
|
|
|
exhibits errors during setup, ``pytest`` will report no errors during its
|
|
|
|
``setup`` phase and will instead raise the error during ``call``.
|