Skip to content
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

Collect tests from __init__.py files if they match 'python_files' #3872

Merged
merged 1 commit into from Aug 26, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/3773.bugfix.rst
@@ -0,0 +1 @@
Fix collection of tests from ``__init__.py`` files if they match the ``python_files`` configuration option.
4 changes: 3 additions & 1 deletion src/_pytest/pytester.py
Expand Up @@ -672,7 +672,9 @@ def copy_example(self, name=None):
example_path.copy(result)
return result
else:
raise LookupError("example is not found as a file or directory")
raise LookupError(
'example "{}" is not found as a file or directory'.format(example_path)
)

Session = Session

Expand Down
17 changes: 13 additions & 4 deletions src/_pytest/python.py
Expand Up @@ -201,15 +201,19 @@ def pytest_collect_file(path, parent):
ext = path.ext
if ext == ".py":
if not parent.session.isinitpath(path):
for pat in parent.config.getini("python_files") + ["__init__.py"]:
if path.fnmatch(pat):
break
else:
if not path_matches_patterns(
path, parent.config.getini("python_files") + ["__init__.py"]
):
return
ihook = parent.session.gethookproxy(path)
return ihook.pytest_pycollect_makemodule(path=path, parent=parent)


def path_matches_patterns(path, patterns):
"""Returns True if the given py.path.local matches one of the patterns in the list of globs given"""
return any(path.fnmatch(pattern) for pattern in patterns)


def pytest_pycollect_makemodule(path, parent):
if path.basename == "__init__.py":
return Package(path, parent)
Expand Down Expand Up @@ -590,6 +594,11 @@ def collect(self):
self.session.config.pluginmanager._duplicatepaths.remove(path)

this_path = self.fspath.dirpath()
init_module = this_path.join("__init__.py")
if init_module.check(file=1) and path_matches_patterns(
init_module, self.config.getini("python_files")
):
yield Module(init_module, self)
pkg_prefixes = set()
for path in this_path.visit(rec=self._recurse, bf=True, sort=True):
# we will visit our own __init__.py file, in which case we skip it
Expand Down
2 changes: 2 additions & 0 deletions testing/example_scripts/collect/collect_init_tests/pytest.ini
@@ -0,0 +1,2 @@
[pytest]
python_files = *.py
@@ -0,0 +1,2 @@
def test_init():
pass
@@ -0,0 +1,2 @@
def test_foo():
pass
14 changes: 14 additions & 0 deletions testing/test_collection.py
Expand Up @@ -938,3 +938,17 @@ def fix():
"*1 passed, 1 error*",
]
)


def test_collect_init_tests(testdir):
"""Check that we collect files from __init__.py files when they patch the 'python_files' (#3773)"""
p = testdir.copy_example("collect/collect_init_tests")
result = testdir.runpytest(p, "--collect-only")
result.stdout.fnmatch_lines(
[
"*<Module '__init__.py'>",
"*<Function 'test_init'>",
"*<Module 'test_foo.py'>",
"*<Function 'test_foo'>",
]
)