Skip to content

Commit dee520e

Browse files
authored
Merge pull request #5008 from blueyed/setup-cfg-tool-pytest
setup.cfg: use existing [tool:pytest] (ignoring [pytest])
2 parents 6b5cddc + bfda2a0 commit dee520e

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

changelog/5008.feature.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
If a ``setup.cfg`` file contains ``[tool:pytest]`` and also the no longer supported ``[pytest]`` section, pytest will use ``[tool:pytest]`` ignoring ``[pytest]``. Previously it would unconditionally error out.
2+
3+
This makes it simpler for plugins to support old pytest versions.

src/_pytest/config/findpaths.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,19 @@ def getcfg(args, config=None):
3333
p = base.join(inibasename)
3434
if exists(p):
3535
iniconfig = py.iniconfig.IniConfig(p)
36-
if "pytest" in iniconfig.sections:
36+
if (
37+
inibasename == "setup.cfg"
38+
and "tool:pytest" in iniconfig.sections
39+
):
40+
return base, p, iniconfig["tool:pytest"]
41+
elif "pytest" in iniconfig.sections:
3742
if inibasename == "setup.cfg" and config is not None:
3843

3944
fail(
4045
CFG_PYTEST_SECTION.format(filename=inibasename),
4146
pytrace=False,
4247
)
4348
return base, p, iniconfig["pytest"]
44-
if (
45-
inibasename == "setup.cfg"
46-
and "tool:pytest" in iniconfig.sections
47-
):
48-
return base, p, iniconfig["tool:pytest"]
4949
elif inibasename == "pytest.ini":
5050
# allowed to be empty
5151
return base, p, {}

testing/test_config.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,22 @@ def test_getcfg_empty_path(self):
4646
"""correctly handle zero length arguments (a la pytest '')"""
4747
getcfg([""])
4848

49+
def test_setupcfg_uses_toolpytest_with_pytest(self, testdir):
50+
p1 = testdir.makepyfile("def test(): pass")
51+
testdir.makefile(
52+
".cfg",
53+
setup="""
54+
[tool:pytest]
55+
testpaths=%s
56+
[pytest]
57+
testpaths=ignored
58+
"""
59+
% p1.basename,
60+
)
61+
result = testdir.runpytest()
62+
result.stdout.fnmatch_lines(["*, inifile: setup.cfg, *", "* 1 passed in *"])
63+
assert result.ret == 0
64+
4965
def test_append_parse_args(self, testdir, tmpdir, monkeypatch):
5066
monkeypatch.setenv("PYTEST_ADDOPTS", '--color no -rs --tb="short"')
5167
tmpdir.join("pytest.ini").write(

0 commit comments

Comments
 (0)