From 5ac4eff09b8514a5b46bdff464605a60051abc83 Mon Sep 17 00:00:00 2001 From: Mick Koch Date: Thu, 1 Nov 2018 08:20:57 -0400 Subject: [PATCH] Fix __init__.py as argument also including other package files --- src/_pytest/main.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/_pytest/main.py b/src/_pytest/main.py index 7e5d096a5..2bb405081 100644 --- a/src/_pytest/main.py +++ b/src/_pytest/main.py @@ -542,7 +542,15 @@ class Session(nodes.FSCollector): col = root._collectfile(argpath) if col: self._node_cache[argpath] = col - for y in self.matchnodes(col, names): + m = self.matchnodes(col, names) + # If __init__.py was the only file requested, then the matched node will be + # the corresponding Package, and the first yielded item will be the __init__ + # Module itself, so just use that. If this special case isn't taken, then all + # the files in the package will be yielded. + if argpath.basename == "__init__.py": + yield next(m[0].collect()) + return + for y in m: yield y def _collectfile(self, path):