Skip to content

Instantly share code, notes, and snippets.

@drocco007
Last active July 6, 2016 13:51
Show Gist options
  • Save drocco007/1d06ee0331e34cd03b5ce6198c947af7 to your computer and use it in GitHub Desktop.
Save drocco007/1d06ee0331e34cd03b5ce6198c947af7 to your computer and use it in GitHub Desktop.

Hack for py.test to programmatically add doctests from an explicitly specified module (as opposed to a package or glob pattern specified on the command line).

Define a conftest.py which instructs py.test to collect the doctests from a specific module when the conftest.py file itself is collected.

def pytest_collect_file(parent, path):
"""Add doctests from a specific module when this suite runs"""
# This guard ensures the doctests are only added once: add the doctests
# when *this* file (conftest.py) is "collected" by py.test, which only
# happens once
if __file__ == str(path):
from pkg_resources import resource_filename
from _pytest.doctest import DoctestModule
module_file = resource_filename('package.subpackage', 'module.py')
return DoctestModule(module_file, parent)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment