Merge pull request #4045 from nicoddemus/root-conftest-warning-workaround-4039

Do not issue non-top-level conftest warning when --pyargs is used
This commit is contained in:
Bruno Oliveira 2018-09-27 09:26:11 -03:00 committed by GitHub
commit d8d7f73e1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 15 deletions

View File

@ -0,0 +1,2 @@
No longer issue warnings about using ``pytest_plugins`` in non-top-level directories when using ``--pyargs``: the
current ``--pyargs`` mechanism is not reliable and might give false negatives.

View File

@ -351,6 +351,7 @@ class PytestPluginManager(PluginManager):
else None else None
) )
self._noconftest = namespace.noconftest self._noconftest = namespace.noconftest
self._using_pyargs = namespace.pyargs
testpaths = namespace.file_or_dir testpaths = namespace.file_or_dir
foundanchor = False foundanchor = False
for path in testpaths: for path in testpaths:
@ -416,7 +417,11 @@ class PytestPluginManager(PluginManager):
_ensure_removed_sysmodule(conftestpath.purebasename) _ensure_removed_sysmodule(conftestpath.purebasename)
try: try:
mod = conftestpath.pyimport() mod = conftestpath.pyimport()
if hasattr(mod, "pytest_plugins") and self._configured: if (
hasattr(mod, "pytest_plugins")
and self._configured
and not self._using_pyargs
):
from _pytest.deprecated import ( from _pytest.deprecated import (
PYTEST_PLUGINS_FROM_NON_TOP_LEVEL_CONFTEST PYTEST_PLUGINS_FROM_NON_TOP_LEVEL_CONFTEST
) )

View File

@ -178,21 +178,12 @@ def test_pytest_catchlog_deprecated(testdir, plugin):
def test_pytest_plugins_in_non_top_level_conftest_deprecated(testdir): def test_pytest_plugins_in_non_top_level_conftest_deprecated(testdir):
from _pytest.deprecated import PYTEST_PLUGINS_FROM_NON_TOP_LEVEL_CONFTEST from _pytest.deprecated import PYTEST_PLUGINS_FROM_NON_TOP_LEVEL_CONFTEST
subdirectory = testdir.tmpdir.join("subdirectory") testdir.makepyfile(
subdirectory.mkdir() **{
# create the inner conftest with makeconftest and then move it to the subdirectory "subdirectory/conftest.py": """
testdir.makeconftest(
"""
pytest_plugins=['capture'] pytest_plugins=['capture']
""" """
) }
testdir.tmpdir.join("conftest.py").move(subdirectory.join("conftest.py"))
# make the top level conftest
testdir.makeconftest(
"""
import warnings
warnings.filterwarnings('always', category=DeprecationWarning)
"""
) )
testdir.makepyfile( testdir.makepyfile(
""" """
@ -200,7 +191,7 @@ def test_pytest_plugins_in_non_top_level_conftest_deprecated(testdir):
pass pass
""" """
) )
res = testdir.runpytest_subprocess() res = testdir.runpytest()
assert res.ret == 0 assert res.ret == 0
msg = str(PYTEST_PLUGINS_FROM_NON_TOP_LEVEL_CONFTEST).splitlines()[0] msg = str(PYTEST_PLUGINS_FROM_NON_TOP_LEVEL_CONFTEST).splitlines()[0]
res.stdout.fnmatch_lines( res.stdout.fnmatch_lines(
@ -210,6 +201,34 @@ def test_pytest_plugins_in_non_top_level_conftest_deprecated(testdir):
) )
@pytest.mark.parametrize("use_pyargs", [True, False])
def test_pytest_plugins_in_non_top_level_conftest_deprecated_pyargs(
testdir, use_pyargs
):
"""When using --pyargs, do not emit the warning about non-top-level conftest warnings (#4039, #4044)"""
from _pytest.deprecated import PYTEST_PLUGINS_FROM_NON_TOP_LEVEL_CONFTEST
files = {
"src/pkg/__init__.py": "",
"src/pkg/conftest.py": "",
"src/pkg/test_root.py": "def test(): pass",
"src/pkg/sub/__init__.py": "",
"src/pkg/sub/conftest.py": "pytest_plugins=['capture']",
"src/pkg/sub/test_bar.py": "def test(): pass",
}
testdir.makepyfile(**files)
testdir.syspathinsert(testdir.tmpdir.join("src"))
args = ("--pyargs", "pkg") if use_pyargs else ()
res = testdir.runpytest(*args)
assert res.ret == 0
msg = str(PYTEST_PLUGINS_FROM_NON_TOP_LEVEL_CONFTEST).splitlines()[0]
if use_pyargs:
assert msg not in res.stdout.str()
else:
res.stdout.fnmatch_lines("*{msg}*".format(msg=msg))
def test_pytest_plugins_in_non_top_level_conftest_deprecated_no_top_level_conftest( def test_pytest_plugins_in_non_top_level_conftest_deprecated_no_top_level_conftest(
testdir testdir
): ):

View File

@ -30,6 +30,7 @@ def conftest_setinitial(conftest, args, confcutdir=None):
self.file_or_dir = args self.file_or_dir = args
self.confcutdir = str(confcutdir) self.confcutdir = str(confcutdir)
self.noconftest = False self.noconftest = False
self.pyargs = False
conftest._set_initial_conftests(Namespace()) conftest._set_initial_conftests(Namespace())