Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,7 @@ Sadra Barikbin
Saiprasad Kale
Samuel Colvin
Samuel Dion-Girardeau
Samuel Gaist
Samuel Jirovec
Samuel Searles-Bryant
Samuel Therrien (Avasam)
Expand Down
3 changes: 3 additions & 0 deletions changelog/13330.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
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.

-- by :user:`sgaist`
9 changes: 8 additions & 1 deletion src/_pytest/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,14 @@ def pytest_report_header(self, config: Config) -> list[str]:
result = [f"rootdir: {config.rootpath}"]

if config.inipath:
result.append("configfile: " + bestrelpath(config.rootpath, config.inipath))
warning = ""
if config.inipath.name in ["pytest.ini", ".pytest.ini"]:
pyproject = config.rootpath / "pyproject.toml"
if pyproject.exists():
warning = " (WARNING: ignoring pytest config in pyproject.toml!)"
result.append(
"configfile: " + bestrelpath(config.rootpath, config.inipath) + warning
)

if config.args_source == Config.ArgsSource.TESTPATHS:
testpaths: list[str] = config.getini("testpaths")
Expand Down
41 changes: 41 additions & 0 deletions testing/test_terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -2893,6 +2893,47 @@ def test_format_trimmed() -> None:
assert _format_trimmed(" ({}) ", msg, len(msg) + 3) == " (unconditional ...) "


def test_warning_when_init_trumps_pyproject_toml(
pytester: Pytester, monkeypatch: MonkeyPatch
) -> None:
"""Regression test for #7814."""
tests = pytester.path.joinpath("tests")
tests.mkdir()
pytester.makepyprojecttoml(
f"""
[tool.pytest.ini_options]
testpaths = ['{tests}']
"""
)
pytester.makefile(".ini", pytest="")
result = pytester.runpytest()
result.stdout.fnmatch_lines(
[
"configfile: pytest.ini (WARNING: ignoring pytest config in pyproject.toml!)",
]
)


def test_no_warning_on_terminal_with_a_single_config_file(
pytester: Pytester, monkeypatch: MonkeyPatch
) -> None:
"""Regression test for #7814."""
tests = pytester.path.joinpath("tests")
tests.mkdir()
pytester.makepyprojecttoml(
f"""
[tool.pytest.ini_options]
testpaths = ['{tests}']
"""
)
result = pytester.runpytest()
result.stdout.fnmatch_lines(
[
"configfile: pyproject.toml",
]
)


class TestFineGrainedTestCase:
DEFAULT_FILE_CONTENTS = """
import pytest
Expand Down