2010-10-11 05:45:45 +08:00
|
|
|
|
2011-09-06 17:43:42 +08:00
|
|
|
Doctest integration for modules and test files
|
2010-10-11 05:45:45 +08:00
|
|
|
=========================================================
|
|
|
|
|
|
|
|
By default all files matching the ``test*.txt`` pattern will
|
|
|
|
be run through the python standard ``doctest`` module. You
|
2019-02-13 23:08:49 +08:00
|
|
|
can change the pattern by issuing:
|
2010-10-11 05:45:45 +08:00
|
|
|
|
2019-02-13 23:08:49 +08:00
|
|
|
.. code-block:: bash
|
|
|
|
|
2019-02-15 20:09:37 +08:00
|
|
|
pytest --doctest-glob='*.rst'
|
2010-10-11 05:45:45 +08:00
|
|
|
|
2015-12-31 04:19:08 +08:00
|
|
|
on the command line. Since version ``2.9``, ``--doctest-glob``
|
|
|
|
can be given multiple times in the command-line.
|
|
|
|
|
2016-12-01 01:01:00 +08:00
|
|
|
.. versionadded:: 3.1
|
2016-11-29 18:51:56 +08:00
|
|
|
|
2016-12-01 01:01:00 +08:00
|
|
|
You can specify the encoding that will be used for those doctest files
|
|
|
|
using the ``doctest_encoding`` ini option:
|
2016-11-30 21:17:54 +08:00
|
|
|
|
2016-12-01 01:01:00 +08:00
|
|
|
.. code-block:: ini
|
|
|
|
|
|
|
|
# content of pytest.ini
|
|
|
|
[pytest]
|
|
|
|
doctest_encoding = latin1
|
2016-11-29 18:51:56 +08:00
|
|
|
|
2016-12-01 01:01:00 +08:00
|
|
|
The default encoding is UTF-8.
|
2016-11-29 18:51:56 +08:00
|
|
|
|
2015-12-31 04:19:08 +08:00
|
|
|
You can also trigger running of doctests
|
2010-10-11 05:45:45 +08:00
|
|
|
from docstrings in all python modules (including regular
|
2019-02-13 23:08:49 +08:00
|
|
|
python test modules):
|
|
|
|
|
|
|
|
.. code-block:: bash
|
2010-10-11 05:45:45 +08:00
|
|
|
|
2019-02-15 20:09:37 +08:00
|
|
|
pytest --doctest-modules
|
2016-08-23 10:35:41 +08:00
|
|
|
|
2010-10-11 05:45:45 +08:00
|
|
|
You can make these changes permanent in your project by
|
2015-07-10 08:50:38 +08:00
|
|
|
putting them into a pytest.ini file like this:
|
|
|
|
|
|
|
|
.. code-block:: ini
|
2010-10-11 05:45:45 +08:00
|
|
|
|
2010-11-26 20:26:56 +08:00
|
|
|
# content of pytest.ini
|
|
|
|
[pytest]
|
|
|
|
addopts = --doctest-modules
|
2010-10-11 05:45:45 +08:00
|
|
|
|
2019-02-13 23:08:49 +08:00
|
|
|
If you then have a text file like this:
|
|
|
|
|
|
|
|
.. code-block:: text
|
2010-10-11 16:07:14 +08:00
|
|
|
|
|
|
|
# content of example.rst
|
|
|
|
|
|
|
|
hello this is a doctest
|
|
|
|
>>> x = 3
|
|
|
|
>>> x
|
|
|
|
3
|
|
|
|
|
|
|
|
and another like this::
|
|
|
|
|
|
|
|
# content of mymodule.py
|
|
|
|
def something():
|
|
|
|
""" a doctest in a docstring
|
2010-11-26 20:26:56 +08:00
|
|
|
>>> something()
|
2010-10-11 16:07:14 +08:00
|
|
|
42
|
|
|
|
"""
|
|
|
|
return 42
|
|
|
|
|
2018-11-24 13:41:22 +08:00
|
|
|
then you can just invoke ``pytest`` without command line options:
|
|
|
|
|
|
|
|
.. code-block:: pytest
|
2010-10-11 16:07:14 +08:00
|
|
|
|
2016-06-21 22:16:57 +08:00
|
|
|
$ pytest
|
2017-11-23 23:33:41 +08:00
|
|
|
=========================== test session starts ============================
|
2018-11-24 04:09:57 +08:00
|
|
|
platform linux -- Python 3.x.y, pytest-4.x.y, py-1.x.y, pluggy-0.x.y
|
2019-01-31 00:25:38 +08:00
|
|
|
cachedir: $PYTHON_PREFIX/.pytest_cache
|
2019-04-04 00:11:14 +08:00
|
|
|
rootdir: /home/sweet/project, inifile: pytest.ini
|
2017-07-04 07:29:13 +08:00
|
|
|
collected 1 item
|
2018-05-18 16:19:46 +08:00
|
|
|
|
2017-11-23 23:33:41 +08:00
|
|
|
mymodule.py . [100%]
|
2018-05-18 16:19:46 +08:00
|
|
|
|
2017-11-23 23:33:41 +08:00
|
|
|
========================= 1 passed in 0.12 seconds =========================
|
2013-03-21 00:54:38 +08:00
|
|
|
|
2019-02-13 23:08:49 +08:00
|
|
|
It is possible to use fixtures using the ``getfixture`` helper:
|
|
|
|
|
|
|
|
.. code-block:: text
|
2013-03-21 00:54:38 +08:00
|
|
|
|
|
|
|
# content of example.rst
|
2013-03-21 19:04:14 +08:00
|
|
|
>>> tmp = getfixture('tmpdir')
|
2013-03-21 00:54:38 +08:00
|
|
|
>>> ...
|
2013-11-22 22:35:20 +08:00
|
|
|
>>>
|
|
|
|
|
|
|
|
Also, :ref:`usefixtures` and :ref:`autouse` fixtures are supported
|
|
|
|
when executing text doctest files.
|
2014-10-08 21:48:41 +08:00
|
|
|
|
|
|
|
The standard ``doctest`` module provides some setting flags to configure the
|
2018-01-04 21:47:05 +08:00
|
|
|
strictness of doctest tests. In pytest, you can enable those flags using the
|
|
|
|
configuration file. To make pytest ignore trailing whitespaces and ignore
|
|
|
|
lengthy exception stack traces you can just write:
|
2015-12-30 07:05:11 +08:00
|
|
|
|
|
|
|
.. code-block:: ini
|
2014-10-08 21:48:41 +08:00
|
|
|
|
|
|
|
[pytest]
|
|
|
|
doctest_optionflags= NORMALIZE_WHITESPACE IGNORE_EXCEPTION_DETAIL
|
2015-08-13 09:13:42 +08:00
|
|
|
|
2016-06-21 22:16:57 +08:00
|
|
|
pytest also introduces new options to allow doctests to run in Python 2 and
|
2015-12-30 07:05:11 +08:00
|
|
|
Python 3 unchanged:
|
|
|
|
|
|
|
|
* ``ALLOW_UNICODE``: when enabled, the ``u`` prefix is stripped from unicode
|
|
|
|
strings in expected doctest output.
|
|
|
|
|
|
|
|
* ``ALLOW_BYTES``: when enabled, the ``b`` prefix is stripped from byte strings
|
|
|
|
in expected doctest output.
|
|
|
|
|
|
|
|
As with any other option flag, these flags can be enabled in ``pytest.ini`` using
|
|
|
|
the ``doctest_optionflags`` ini option:
|
|
|
|
|
|
|
|
.. code-block:: ini
|
|
|
|
|
|
|
|
[pytest]
|
|
|
|
doctest_optionflags = ALLOW_UNICODE ALLOW_BYTES
|
2015-08-13 09:13:42 +08:00
|
|
|
|
|
|
|
|
2015-12-30 07:05:11 +08:00
|
|
|
Alternatively, it can be enabled by an inline comment in the doc test
|
2019-02-13 23:08:49 +08:00
|
|
|
itself:
|
|
|
|
|
|
|
|
.. code-block:: rst
|
2015-08-13 09:13:42 +08:00
|
|
|
|
|
|
|
# content of example.rst
|
|
|
|
>>> get_unicode_greeting() # doctest: +ALLOW_UNICODE
|
|
|
|
'Hello'
|
|
|
|
|
2018-02-24 12:42:22 +08:00
|
|
|
By default, pytest would report only the first failure for a given doctest. If
|
2019-02-13 23:08:49 +08:00
|
|
|
you want to continue the test even when you have failures, do:
|
|
|
|
|
|
|
|
.. code-block:: bash
|
2018-02-24 12:42:22 +08:00
|
|
|
|
2019-02-15 20:09:37 +08:00
|
|
|
pytest --doctest-modules --doctest-continue-on-failure
|
2018-02-24 12:42:22 +08:00
|
|
|
|
2016-07-23 20:40:25 +08:00
|
|
|
|
2018-02-09 06:08:44 +08:00
|
|
|
.. _`doctest_namespace`:
|
|
|
|
|
2016-03-02 20:43:57 +08:00
|
|
|
The 'doctest_namespace' fixture
|
|
|
|
-------------------------------
|
2015-08-13 09:13:42 +08:00
|
|
|
|
2016-07-13 08:02:40 +08:00
|
|
|
.. versionadded:: 3.0
|
2016-03-03 10:37:51 +08:00
|
|
|
|
2016-03-02 20:43:57 +08:00
|
|
|
The ``doctest_namespace`` fixture can be used to inject items into the
|
|
|
|
namespace in which your doctests run. It is intended to be used within
|
|
|
|
your own fixtures to provide the tests that use them with context.
|
|
|
|
|
|
|
|
``doctest_namespace`` is a standard ``dict`` object into which you
|
|
|
|
place the objects you want to appear in the doctest namespace::
|
|
|
|
|
|
|
|
# content of conftest.py
|
|
|
|
import numpy
|
|
|
|
@pytest.fixture(autouse=True)
|
2016-03-02 21:02:15 +08:00
|
|
|
def add_np(doctest_namespace):
|
|
|
|
doctest_namespace['np'] = numpy
|
2016-03-02 20:43:57 +08:00
|
|
|
|
|
|
|
which can then be used in your doctests directly::
|
|
|
|
|
|
|
|
# content of numpy.py
|
|
|
|
def arange():
|
|
|
|
"""
|
|
|
|
>>> a = np.arange(10)
|
|
|
|
>>> len(a)
|
|
|
|
10
|
|
|
|
"""
|
|
|
|
pass
|
2016-07-23 20:40:25 +08:00
|
|
|
|
2018-12-05 17:25:52 +08:00
|
|
|
Note that like the normal ``conftest.py``, the fixtures are discovered in the directory tree conftest is in.
|
|
|
|
Meaning that if you put your doctest with your source code, the relevant conftest.py needs to be in the same directory tree.
|
|
|
|
Fixtures will not be discovered in a sibling directory tree!
|
2016-07-23 20:40:25 +08:00
|
|
|
|
|
|
|
Output format
|
|
|
|
-------------
|
|
|
|
|
2016-07-23 21:30:06 +08:00
|
|
|
.. versionadded:: 3.0
|
|
|
|
|
2016-07-23 20:40:25 +08:00
|
|
|
You can change the diff output format on failure for your doctests
|
|
|
|
by using one of standard doctest modules format in options
|
|
|
|
(see :data:`python:doctest.REPORT_UDIFF`, :data:`python:doctest.REPORT_CDIFF`,
|
2019-02-13 23:08:49 +08:00
|
|
|
:data:`python:doctest.REPORT_NDIFF`, :data:`python:doctest.REPORT_ONLY_FIRST_FAILURE`):
|
|
|
|
|
|
|
|
.. code-block:: bash
|
2016-07-23 20:40:25 +08:00
|
|
|
|
2019-02-15 20:09:37 +08:00
|
|
|
pytest --doctest-modules --doctest-report none
|
|
|
|
pytest --doctest-modules --doctest-report udiff
|
|
|
|
pytest --doctest-modules --doctest-report cdiff
|
|
|
|
pytest --doctest-modules --doctest-report ndiff
|
|
|
|
pytest --doctest-modules --doctest-report only_first_failure
|