Skip to main content

Pytest plugin with advanced doctest features.

Project description

This package contains a plugin for the pytest framework that provides advanced doctest support and enables the testing of reStructuredText (“.rst”) files. It was originally part of the astropy core package, but has been moved to a separate package in order to be of more general use.

Motivation

This plugin provides advanced features for testing example Python code that is included in Python docstrings and in standalone documentation files.

Good documentation for developers contains example code. This is true of both standalone documentation and of documentation that is integrated with the code itself. Python provides a mechanism for testing code snippets that are provided in Python docstrings. The unit test framework pytest provides a mechanism for running doctests against both docstrings in source code and in standalone documentation files.

This plugin augments the functionality provided by Python and pytest by providing the following features:

Installation

The pytest-doctestplus plugin can be installed using pip:

$ pip install pytest-doctestplus

It is also possible to install the latest development version from the source repository:

$ git clone https://github.com/astropy/pytest-doctestplus
$ cd pytest-doctestplus
$ python ./setup.py install

In either case, the plugin will automatically be registered for use with pytest.

Usage

Setup and Configuration

This plugin provides two command line options: --doctest-plus for enabling the advanced features mentioned above, and --doctest-rst for including *.rst files in doctest collection.

This plugin can also be enabled by default by adding doctest_plus = enabled to the [tool:pytest] section of the package’s setup.cfg file.

The plugin is applied to all directories and files that pytest collects. This means that configuring testpaths and norecursedirs in setup.cfg also affects the files that will be discovered by pytest-doctestplus. In addition, this plugin provides a doctest_norecursedirs configuration variable that indicates directories that should be ignored by pytest-doctestplus but do not need to be ignored by other pytest features.

Using pytest’s built-in --doctest-modules option will override the behavior of this plugin, even if doctest_plus = enabled in setup.cfg, and will cause the default doctest plugin to be used. However, if for some reason both --doctest-modules and --doctest-plus are given, the pytest-doctestplus plugin will be used, regardless of the contents of setup.cfg.

This plugin respects the doctest options that are used by the built-in doctest plugin and are set in doctest_optionflags in setup.cfg. By default, ELLIPSIS and NORMALIZE_WHITESPACE are used. For a description of all doctest settings, see the doctest documentation.

Doctest Directives

The pytest-doctestplus plugin defines doctest directives that are used to control the behavior of particular features. For general information on directives and how they are used, consult the documentation. The specifics of the directives that this plugin defines are described in the sections below.

Floating Point Comparison

Some doctests may produce output that contains string representations of floating point values. Floating point representations are often not exact and contain roundoffs in their least significant digits. Depending on the platform the tests are being run on (different Python versions, different OS, etc.) the exact number of digits shown can differ. Because doctests work by comparing strings this can cause such tests to fail.

To address this issue, the pytest-doctestplus plugin provides support for a FLOAT_CMP flag that can be used with doctests. For example:

>>> 1.0 / 3.0  # doctest: +FLOAT_CMP
0.333333333333333311

When this flag is used, the expected and actual outputs are both parsed to find any floating point values in the strings. Those are then converted to actual Python float objects and compared numerically. This means that small differences in representation of roundoff digits will be ignored by the doctest. The values are otherwise compared exactly, so more significant (albeit possibly small) differences will still be caught by these tests.

This flag can be enabled globally by adding it to setup.cfg as in

doctest_optionflags =
    NORMALIZE_WHITESPACE
    ELLIPSIS
    FLOAT_CMP

Ignoring warnings

If code in a doctest emits a warning and you want to make sure that warning is silenced, you can make use of the IGNORE_WARNINGS flag. For example:

>>> import numpy as np
>>> np.mean([])  # doctest: +IGNORE_WARNINGS
np.nan

Skipping Tests

Doctest provides the +SKIP directive for skipping statements that should not be executed when testing documentation.

>>> open('file.txt') # doctest: +SKIP

In Sphinx .rst documentation, whole code example blocks can be skipped with the directive

.. doctest-skip::

    >>> import asdf
    >>> asdf.open('file.asdf')

However, it is often useful to be able to skip docstrings associated with particular functions, methods, classes, or even entire files.

Skip Unconditionally

The pytest-doctestplus plugin provides a way to indicate that certain docstrings should be skipped altogether. This is configured by defining the variable __doctest_skip__ in each module where tests should be skipped. The value of __doctest_skip__ should be a list of wildcard patterns for all functions/classes whose doctests should be skipped. For example:

__doctest_skip__ = ['myfunction', 'MyClass', 'MyClass.*']

skips the doctests in a function called myfunction, the doctest for a class called MyClass, and all methods of MyClass.

Module docstrings may contain doctests as well. To skip the module-level doctests:

__doctest_skip__  = ['.', 'myfunction', 'MyClass']

To skip all doctests in a module:

__doctest_skip__ = ['*']

Doctest Dependencies

It is also possible to skip certain doctests depending on whether particular dependencies are available. This is configured by defining the variable __doctest_requires__ at the module level. The value of this variable is a dictionary that indicates the modules that are required to run the doctests associated with particular functions, classes, and methods.

The keys in the dictionary are wildcard patterns like those described above, or tuples of wildcard patterns, indicating which docstrings should be skipped. The values in the dictionary are lists of module names that are required in order for the given doctests to be executed.

Consider the following example:

__doctest_requires__ = {('func1', 'func2'): ['scipy']}

Having this module-level variable will require scipy to be importable in order to run the doctests for functions func1 and func2 in that module.

Similarly, in Sphinx .rst documentation, whole code example blocks can be conditionally skipped if a dependency is not available.

.. doctest-requires:: asdf

    >>> import asdf
    >>> asdf.open('file.asdf')

Finally, it is possible to skip collecting doctests in entire subpackages by using the doctest_subpackage_requires in the [tool:pytest] section of the package’s setup.cfg file. The syntax for this option is a list of path = requirements, e.g.:

doctest_subpackage_requires =
    astropy/wcs/* = scipy>2.0;numpy>1.14
    astropy/cosmology/* = scipy>1.0

Multiple requirements can be specified if separated by semicolons.

Remote Data

The pytest-doctestplus plugin can be used in conjunction with the pytest-remotedata plugin in order to control doctest code that requires access to data from the internet. In order to make use of these features, the pytest-remotedata plugin must be installed, and remote data access must be enabled using the --remote-data command line option to pytest. See the pytest-remotedata plugin documentation for more details.

The following example illustrates how a doctest that uses remote data should be marked:

>>> from urlib.request import urlopen
>>> url = urlopen('http://astropy.org') # doctest: +REMOTE_DATA

The +REMOTE_DATA directive indicates that the marked statement should only be executed if the --remote-data option is given. By default, all statements marked with --remote-data will be skipped.

Development Status

Travis CI Status

Questions, bug reports, and feature requests can be submitted on github.

License

This plugin is licensed under a 3-clause BSD style license - see the LICENSE.rst file.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest-doctestplus-0.7.0.tar.gz (26.6 kB view details)

Uploaded Source

Built Distribution

pytest_doctestplus-0.7.0-py3-none-any.whl (17.9 kB view details)

Uploaded Python 3

File details

Details for the file pytest-doctestplus-0.7.0.tar.gz.

File metadata

  • Download URL: pytest-doctestplus-0.7.0.tar.gz
  • Upload date:
  • Size: 26.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.2

File hashes

Hashes for pytest-doctestplus-0.7.0.tar.gz
Algorithm Hash digest
SHA256 ed440f43e33191f09aed7bbc4f60db3dfb8f295ab33e04c59302af7eda9e29aa
MD5 85c4d8a5fcbe7ab4d513e523c9c99342
BLAKE2b-256 95388b400df16d73bcef3766f216e51da06cec1bc859d0c7ec1ba7636130ffc9

See more details on using hashes here.

File details

Details for the file pytest_doctestplus-0.7.0-py3-none-any.whl.

File metadata

  • Download URL: pytest_doctestplus-0.7.0-py3-none-any.whl
  • Upload date:
  • Size: 17.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.2

File hashes

Hashes for pytest_doctestplus-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7ff3da460c92636cbe7121bb6a1f2783ef11d8559f453ff21479580268450878
MD5 6bf3a6a4d1ee378dc404a846aaa19141
BLAKE2b-256 331503c98e99a19382e2f34f2f28502442d8a22726559a962521a271da75d8a2

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page