Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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 changelog/14638.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Top-level options in ``pytest.toml`` and ``.pytest.toml`` files now raise an error instead of being silently ignored when no ``[pytest]`` table is present.
14 changes: 11 additions & 3 deletions src/_pytest/config/findpaths.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,21 @@ def load_config_dict_from_file(

# pytest.toml and .pytest.toml use [pytest] table directly.
if filepath.name in ("pytest.toml", ".pytest.toml"):
pytest_config = config.get("pytest", {})
if pytest_config:
if "pytest" in config:
# TOML mode - preserve native TOML types.
return {
k: ConfigValue(v, origin="file", mode="toml")
for k, v in pytest_config.items()
for k, v in config["pytest"].items()
}
top_level_options = [
key for key, value in config.items() if not isinstance(value, dict)
]
if top_level_options:
raise UsageError(
f"{filepath}: pytest configuration must be under a "
f"[pytest] table (found top-level options: "
f"{', '.join(top_level_options)})"
)
# "pytest.toml" files are always the source of configuration, even if empty.
return {}

Expand Down
22 changes: 22 additions & 0 deletions testing/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,28 @@ def test_toml_config_names(self, pytester: Pytester, name: str) -> None:
config = pytester.parseconfig()
assert config.getini("minversion") == "3.36"

@pytest.mark.parametrize("name", ["pytest.toml", ".pytest.toml"])
def test_toml_config_names_without_section_errors(
self, pytester: Pytester, name: str
) -> None:
config_path = pytester.path.joinpath(name)
config_path.write_text(
textwrap.dedent(
"""
minversion = "3.36"
addopts = ["-v"]
"""
),
encoding="utf-8",
)
with pytest.raises(UsageError) as excinfo:
pytester.parseconfig()
assert str(excinfo.value) == (
f"{config_path}: "
"pytest configuration must be under a [pytest] table "
"(found top-level options: minversion, addopts)"
)

def test_pyproject_toml(self, pytester: Pytester) -> None:
pyproject_toml = pytester.makepyprojecttoml(
"""
Expand Down
Loading