Skip to content

Commit b375937

Browse files
authored
Merge pull request #5082 from blueyed/pytester-raise_on_kwargs
pytester: improve/fix kwargs validation
2 parents f5d2b19 + 12133d4 commit b375937

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

changelog/5082.trivial.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improved validation of kwargs for various methods in the pytester plugin.

src/_pytest/pytester.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,11 @@ def pytest_configure(config):
7676

7777

7878
def raise_on_kwargs(kwargs):
79+
__tracebackhide__ = True
7980
if kwargs:
80-
raise TypeError("Unexpected arguments: {}".format(", ".join(sorted(kwargs))))
81+
raise TypeError(
82+
"Unexpected keyword arguments: {}".format(", ".join(sorted(kwargs)))
83+
)
8184

8285

8386
class LsofFdLeakChecker(object):
@@ -803,12 +806,15 @@ def inline_run(self, *args, **kwargs):
803806
804807
:param args: command line arguments to pass to :py:func:`pytest.main`
805808
806-
:param plugin: (keyword-only) extra plugin instances the
809+
:param plugins: (keyword-only) extra plugin instances the
807810
``pytest.main()`` instance should use
808811
809812
:return: a :py:class:`HookRecorder` instance
810-
811813
"""
814+
plugins = kwargs.pop("plugins", [])
815+
no_reraise_ctrlc = kwargs.pop("no_reraise_ctrlc", None)
816+
raise_on_kwargs(kwargs)
817+
812818
finalizers = []
813819
try:
814820
# Do not load user config (during runs only).
@@ -848,7 +854,6 @@ class Collect(object):
848854
def pytest_configure(x, config):
849855
rec.append(self.make_hook_recorder(config.pluginmanager))
850856

851-
plugins = kwargs.get("plugins") or []
852857
plugins.append(Collect())
853858
ret = pytest.main(list(args), plugins=plugins)
854859
if len(rec) == 1:
@@ -862,7 +867,7 @@ class reprec(object):
862867

863868
# typically we reraise keyboard interrupts from the child run
864869
# because it's our user requesting interruption of the testing
865-
if ret == EXIT_INTERRUPTED and not kwargs.get("no_reraise_ctrlc"):
870+
if ret == EXIT_INTERRUPTED and not no_reraise_ctrlc:
866871
calls = reprec.getcalls("pytest_keyboard_interrupt")
867872
if calls and calls[-1].excinfo.type == KeyboardInterrupt:
868873
raise KeyboardInterrupt()
@@ -874,9 +879,10 @@ class reprec(object):
874879
def runpytest_inprocess(self, *args, **kwargs):
875880
"""Return result of running pytest in-process, providing a similar
876881
interface to what self.runpytest() provides.
877-
878882
"""
879-
if kwargs.get("syspathinsert"):
883+
syspathinsert = kwargs.pop("syspathinsert", False)
884+
885+
if syspathinsert:
880886
self.syspathinsert()
881887
now = time.time()
882888
capture = MultiCapture(Capture=SysCapture)
@@ -1201,9 +1207,10 @@ def runpytest_subprocess(self, *args, **kwargs):
12011207
:py:class:`Testdir.TimeoutExpired`
12021208
12031209
Returns a :py:class:`RunResult`.
1204-
12051210
"""
12061211
__tracebackhide__ = True
1212+
timeout = kwargs.pop("timeout", None)
1213+
raise_on_kwargs(kwargs)
12071214

12081215
p = py.path.local.make_numbered_dir(
12091216
prefix="runpytest-", keep=None, rootdir=self.tmpdir
@@ -1213,7 +1220,7 @@ def runpytest_subprocess(self, *args, **kwargs):
12131220
if plugins:
12141221
args = ("-p", plugins[0]) + args
12151222
args = self._getpytestargs() + args
1216-
return self.run(*args, timeout=kwargs.get("timeout"))
1223+
return self.run(*args, timeout=timeout)
12171224

12181225
def spawn_pytest(self, string, expect_timeout=10.0):
12191226
"""Run pytest using pexpect.

testing/test_capture.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -819,15 +819,15 @@ def test_error_during_readouterr(testdir):
819819
testdir.makepyfile(
820820
pytest_xyz="""
821821
from _pytest.capture import FDCapture
822+
822823
def bad_snap(self):
823824
raise Exception('boom')
825+
824826
assert FDCapture.snap
825827
FDCapture.snap = bad_snap
826828
"""
827829
)
828-
result = testdir.runpytest_subprocess(
829-
"-p", "pytest_xyz", "--version", syspathinsert=True
830-
)
830+
result = testdir.runpytest_subprocess("-p", "pytest_xyz", "--version")
831831
result.stderr.fnmatch_lines(
832832
["*in bad_snap", " raise Exception('boom')", "Exception: boom"]
833833
)

0 commit comments

Comments
 (0)