Skip to content

Commit 89305e7

Browse files
Improve output for missing config keys (#7572)
Co-authored-by: Bruno Oliveira <[email protected]>
1 parent 9bfd14a commit 89305e7

File tree

3 files changed

+40
-30
lines changed

3 files changed

+40
-30
lines changed

changelog/7572.improvement.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
When a plugin listed in ``required_plugins`` is missing, a simple error message is now shown instead of a stacktrace.
1+
When a plugin listed in ``required_plugins`` is missing or an unknown config key is used with ``--strict-config``, a simple error message is now shown instead of a stacktrace.

src/_pytest/config/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1252,7 +1252,7 @@ def _validate_plugins(self) -> None:
12521252

12531253
def _warn_or_fail_if_strict(self, message: str) -> None:
12541254
if self.known_args_namespace.strict_config:
1255-
fail(message, pytrace=False)
1255+
raise UsageError(message)
12561256

12571257
self.issue_config_time_warning(PytestConfigWarning(message), stacklevel=3)
12581258

testing/test_config.py

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -181,61 +181,66 @@ def test_confcutdir(self, testdir):
181181
@pytest.mark.parametrize(
182182
"ini_file_text, invalid_keys, warning_output, exception_text",
183183
[
184-
(
184+
pytest.param(
185185
"""
186-
[pytest]
187-
unknown_ini = value1
188-
another_unknown_ini = value2
189-
""",
186+
[pytest]
187+
unknown_ini = value1
188+
another_unknown_ini = value2
189+
""",
190190
["unknown_ini", "another_unknown_ini"],
191191
[
192192
"=*= warnings summary =*=",
193193
"*PytestConfigWarning:*Unknown config option: another_unknown_ini",
194194
"*PytestConfigWarning:*Unknown config option: unknown_ini",
195195
],
196196
"Unknown config option: another_unknown_ini",
197+
id="2-unknowns",
197198
),
198-
(
199+
pytest.param(
199200
"""
200-
[pytest]
201-
unknown_ini = value1
202-
minversion = 5.0.0
203-
""",
201+
[pytest]
202+
unknown_ini = value1
203+
minversion = 5.0.0
204+
""",
204205
["unknown_ini"],
205206
[
206207
"=*= warnings summary =*=",
207208
"*PytestConfigWarning:*Unknown config option: unknown_ini",
208209
],
209210
"Unknown config option: unknown_ini",
211+
id="1-unknown",
210212
),
211-
(
213+
pytest.param(
212214
"""
213-
[some_other_header]
214-
unknown_ini = value1
215-
[pytest]
216-
minversion = 5.0.0
217-
""",
215+
[some_other_header]
216+
unknown_ini = value1
217+
[pytest]
218+
minversion = 5.0.0
219+
""",
218220
[],
219221
[],
220222
"",
223+
id="unknown-in-other-header",
221224
),
222-
(
225+
pytest.param(
223226
"""
224-
[pytest]
225-
minversion = 5.0.0
226-
""",
227+
[pytest]
228+
minversion = 5.0.0
229+
""",
227230
[],
228231
[],
229232
"",
233+
id="no-unknowns",
230234
),
231-
(
235+
pytest.param(
232236
"""
233-
[pytest]
234-
conftest_ini_key = 1
235-
""",
237+
[pytest]
238+
conftest_ini_key = 1
239+
""",
236240
[],
237241
[],
238242
"",
243+
id="1-known",
239244
),
240245
],
241246
)
@@ -247,19 +252,24 @@ def test_invalid_config_options(
247252
"""
248253
def pytest_addoption(parser):
249254
parser.addini("conftest_ini_key", "")
250-
"""
255+
"""
251256
)
252-
testdir.tmpdir.join("pytest.ini").write(textwrap.dedent(ini_file_text))
257+
testdir.makepyfile("def test(): pass")
258+
testdir.makeini(ini_file_text)
253259

254260
config = testdir.parseconfig()
255261
assert sorted(config._get_unknown_ini_keys()) == sorted(invalid_keys)
256262

257263
result = testdir.runpytest()
258264
result.stdout.fnmatch_lines(warning_output)
259265

266+
result = testdir.runpytest("--strict-config")
260267
if exception_text:
261-
result = testdir.runpytest("--strict-config")
262-
result.stdout.fnmatch_lines("INTERNALERROR>*" + exception_text)
268+
result.stderr.fnmatch_lines("ERROR: " + exception_text)
269+
assert result.ret == pytest.ExitCode.USAGE_ERROR
270+
else:
271+
result.stderr.no_fnmatch_line(exception_text)
272+
assert result.ret == pytest.ExitCode.OK
263273

264274
@pytest.mark.filterwarnings("default")
265275
def test_silence_unknown_key_warning(self, testdir: Testdir) -> None:

0 commit comments

Comments
 (0)