Skip to content

Commit fce5184

Browse files
committed
feat: add warning when pytest.ini and pyproject.toml are present
The behaviour does not change however users will be notified that pytest.ini is selected over pyproject.toml and any configuration related to pytest it may contain.
1 parent 1bed1d7 commit fce5184

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ Sadra Barikbin
403403
Saiprasad Kale
404404
Samuel Colvin
405405
Samuel Dion-Girardeau
406+
Samuel Gaist
406407
Samuel Jirovec
407408
Samuel Searles-Bryant
408409
Samuel Therrien (Avasam)

changelog/13330.improvement.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Having both ``pytest.ini`` and ``pyproject.toml`` will now print a warning to make it clearer to the user that the former takes precedence over the latter.

src/_pytest/terminal.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,14 @@ def pytest_report_header(self, config: Config) -> list[str]:
879879
result = [f"rootdir: {config.rootpath}"]
880880

881881
if config.inipath:
882-
result.append("configfile: " + bestrelpath(config.rootpath, config.inipath))
882+
warning = ""
883+
if config.inipath.name in ["pytest.ini", ".pytest.ini"]:
884+
pyproject = config.rootpath / "pyproject.toml"
885+
if pyproject.exists():
886+
warning = " (WARNING: ignoring pytest config in pyproject.toml!)"
887+
result.append(
888+
"configfile: " + bestrelpath(config.rootpath, config.inipath) + warning
889+
)
883890

884891
if config.args_source == Config.ArgsSource.TESTPATHS:
885892
testpaths: list[str] = config.getini("testpaths")

testing/test_terminal.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2893,6 +2893,27 @@ def test_format_trimmed() -> None:
28932893
assert _format_trimmed(" ({}) ", msg, len(msg) + 3) == " (unconditional ...) "
28942894

28952895

2896+
def test_warning_when_init_trumps_pyproject_toml(
2897+
pytester: Pytester, monkeypatch: MonkeyPatch
2898+
) -> None:
2899+
"""Regression test for #7814."""
2900+
tests = pytester.path.joinpath("tests")
2901+
tests.mkdir()
2902+
pytester.makepyprojecttoml(
2903+
f"""
2904+
[tool.pytest.ini_options]
2905+
testpaths = ['{tests}']
2906+
"""
2907+
)
2908+
pytest_ini = pytester.makefile(".ini", pytest="")
2909+
result = pytester.runpytest()
2910+
result.stdout.fnmatch_lines(
2911+
[
2912+
"configfile: pytest.ini (WARNING: ignoring pytest config in pyproject.toml!)",
2913+
]
2914+
)
2915+
2916+
28962917
class TestFineGrainedTestCase:
28972918
DEFAULT_FILE_CONTENTS = """
28982919
import pytest

0 commit comments

Comments
 (0)