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
|
|
|
|
can change the pattern by issuing::
|
|
|
|
|
|
|
|
py.test --doctest-glob='*.rst'
|
|
|
|
|
|
|
|
on the command line. You can also trigger running of doctests
|
|
|
|
from docstrings in all python modules (including regular
|
|
|
|
python test modules)::
|
|
|
|
|
|
|
|
py.test --doctest-modules
|
|
|
|
|
|
|
|
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
|
|
|
|
2010-10-11 16:07:14 +08:00
|
|
|
If you then have a text file like this::
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
then you can just invoke ``py.test`` without command line options::
|
|
|
|
|
|
|
|
$ py.test
|
2015-06-07 05:30:49 +08:00
|
|
|
======= test session starts ========
|
|
|
|
platform linux2 -- Python 2.7.9, pytest-2.8.0.dev4, py-1.4.28, pluggy-0.3.0
|
|
|
|
rootdir: $REGENDOC_TMPDIR, inifile: pytest.ini
|
2012-10-07 19:06:17 +08:00
|
|
|
collected 1 items
|
2010-10-11 16:07:14 +08:00
|
|
|
|
2010-11-26 20:26:56 +08:00
|
|
|
mymodule.py .
|
|
|
|
|
2015-06-07 05:30:49 +08:00
|
|
|
======= 1 passed in 0.12 seconds ========
|
2013-03-21 00:54:38 +08:00
|
|
|
|
2013-03-21 19:04:14 +08:00
|
|
|
It is possible to use fixtures using the ``getfixture`` helper::
|
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
|
|
|
|
strictness of doctest tests. In py.test You can enable those flags those flags
|
|
|
|
using the configuration file. To make pytest ignore trailing whitespaces and
|
|
|
|
ignore lengthy exception stack traces you can just write::
|
|
|
|
|
|
|
|
# content of pytest.ini
|
|
|
|
[pytest]
|
|
|
|
doctest_optionflags= NORMALIZE_WHITESPACE IGNORE_EXCEPTION_DETAIL
|
2015-08-13 09:13:42 +08:00
|
|
|
|
|
|
|
|
|
|
|
py.test also introduces a new ``ALLOW_UNICODE`` option flag: when enabled, the
|
|
|
|
``u`` prefix is stripped from unicode strings in expected doctest output. This
|
|
|
|
allows doctests which use unicode to run in Python 2 and 3 unchanged.
|
|
|
|
|
|
|
|
As with any other option flag, this flag can be enabled in ``pytest.ini`` using
|
|
|
|
the ``doctest_optionflags`` ini option or by an inline comment in the doc test
|
|
|
|
itself::
|
|
|
|
|
|
|
|
# content of example.rst
|
|
|
|
>>> get_unicode_greeting() # doctest: +ALLOW_UNICODE
|
|
|
|
'Hello'
|
|
|
|
|
|
|
|
|