Skip to content

ignore valid setup.py during --doctest-modules #2834

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ Jason R. Coombs
Javier Domingo Cansino
Javier Romero
Jeff Widman
John Eddie Ayson
John Towler
Jon Sonesen
Jonas Obrist
Expand Down
9 changes: 8 additions & 1 deletion _pytest/doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,19 @@ def pytest_addoption(parser):
def pytest_collect_file(path, parent):
config = parent.config
if path.ext == ".py":
if config.option.doctestmodules:
if config.option.doctestmodules and not _is_setup_py(config, path, parent):
return DoctestModule(path, parent)
elif _is_doctest(config, path, parent):
return DoctestTextfile(path, parent)


def _is_setup_py(config, path, parent):
if path.basename != "setup.py":
return False
contents = path.read()
return 'setuptools' in contents or 'distutils' in contents


def _is_doctest(config, path, parent):
if path.ext in ('.txt', '.rst') and parent.session.isinitpath(path):
return True
Expand Down
1 change: 1 addition & 0 deletions changelog/502.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Implement feature to skip ``setup.py`` files when ran with ``--doctest-modules``.
28 changes: 28 additions & 0 deletions testing/test_doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,34 @@ def foo(x):
reportinfo = items[0].reportinfo()
assert reportinfo[1] == 1

def test_valid_setup_py(self, testdir):
'''
Test to make sure that pytest ignores valid setup.py files when ran
with --doctest-modules
'''
p = testdir.makepyfile(setup="""
from setuptools import setup, find_packages
setup(name='sample',
version='0.0',
description='description',
packages=find_packages()
)
""")
result = testdir.runpytest(p, '--doctest-modules')
result.stdout.fnmatch_lines(['*collected 0 items*'])

def test_invalid_setup_py(self, testdir):
'''
Test to make sure that pytest reads setup.py files that are not used
for python packages when ran with --doctest-modules
'''
p = testdir.makepyfile(setup="""
def test_foo():
return 'bar'
""")
result = testdir.runpytest(p, '--doctest-modules')
result.stdout.fnmatch_lines(['*collected 1 item*'])


class TestLiterals(object):

Expand Down