2023-01-20 17:13:36 +08:00
|
|
|
import dataclasses
|
2023-07-01 05:55:42 +08:00
|
|
|
import importlib.metadata
|
2019-06-30 23:15:29 +08:00
|
|
|
import os
|
2020-02-20 20:29:59 +08:00
|
|
|
import re
|
2017-11-29 00:29:52 +08:00
|
|
|
import sys
|
2017-12-27 11:47:26 +08:00
|
|
|
import textwrap
|
2020-10-03 23:08:14 +08:00
|
|
|
from pathlib import Path
|
2023-11-12 00:08:18 +08:00
|
|
|
from typing import Any
|
2020-05-01 19:40:17 +08:00
|
|
|
from typing import Dict
|
|
|
|
from typing import List
|
|
|
|
from typing import Sequence
|
2020-09-04 22:57:15 +08:00
|
|
|
from typing import Tuple
|
2020-10-03 04:02:22 +08:00
|
|
|
from typing import Type
|
2020-12-09 04:20:02 +08:00
|
|
|
from typing import Union
|
2009-03-17 05:17:14 +08:00
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
import _pytest._code
|
2018-10-25 15:01:29 +08:00
|
|
|
import pytest
|
2020-06-23 16:38:21 +08:00
|
|
|
from _pytest.config import _get_plugin_specs_as_list
|
2018-06-05 16:20:36 +08:00
|
|
|
from _pytest.config import _iter_rewritable_modules
|
2020-09-04 22:57:15 +08:00
|
|
|
from _pytest.config import _strtobool
|
2019-10-20 02:54:54 +08:00
|
|
|
from _pytest.config import Config
|
2020-05-17 03:05:12 +08:00
|
|
|
from _pytest.config import ConftestImportFailure
|
2020-02-11 05:43:30 +08:00
|
|
|
from _pytest.config import ExitCode
|
2020-09-04 22:57:15 +08:00
|
|
|
from _pytest.config import parse_warning_filter
|
2023-11-12 00:08:18 +08:00
|
|
|
from _pytest.config.argparsing import get_ini_default_for_type
|
2023-11-19 22:56:29 +08:00
|
|
|
from _pytest.config.argparsing import Parser
|
2019-01-18 05:06:45 +08:00
|
|
|
from _pytest.config.exceptions import UsageError
|
2018-10-25 15:01:29 +08:00
|
|
|
from _pytest.config.findpaths import determine_setup
|
|
|
|
from _pytest.config.findpaths import get_common_ancestor
|
2020-06-08 21:03:10 +08:00
|
|
|
from _pytest.config.findpaths import locate_config
|
2020-08-03 22:46:35 +08:00
|
|
|
from _pytest.monkeypatch import MonkeyPatch
|
2020-12-19 20:49:24 +08:00
|
|
|
from _pytest.pathlib import absolutepath
|
2020-12-09 04:20:02 +08:00
|
|
|
from _pytest.pytester import Pytester
|
2010-10-28 01:35:27 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestParseIni:
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"section, filename", [("pytest", "pytest.ini"), ("tool:pytest", "setup.cfg")]
|
|
|
|
)
|
2020-08-03 22:46:35 +08:00
|
|
|
def test_getcfg_and_config(
|
|
|
|
self,
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester: Pytester,
|
2020-08-03 22:46:35 +08:00
|
|
|
tmp_path: Path,
|
|
|
|
section: str,
|
|
|
|
filename: str,
|
|
|
|
monkeypatch: MonkeyPatch,
|
|
|
|
) -> None:
|
|
|
|
sub = tmp_path / "sub"
|
|
|
|
sub.mkdir()
|
|
|
|
monkeypatch.chdir(sub)
|
|
|
|
(tmp_path / filename).write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
[{section}]
|
|
|
|
name = value
|
|
|
|
""".format(
|
2018-05-23 22:48:46 +08:00
|
|
|
section=section
|
|
|
|
)
|
2020-08-03 22:46:35 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-06-08 21:03:10 +08:00
|
|
|
_, _, cfg = locate_config([sub])
|
2018-05-23 22:48:46 +08:00
|
|
|
assert cfg["name"] == "value"
|
2020-12-09 04:20:02 +08:00
|
|
|
config = pytester.parseconfigure(str(sub))
|
2018-05-23 22:48:46 +08:00
|
|
|
assert config.inicfg["name"] == "value"
|
2010-10-28 01:35:27 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_setupcfg_uses_toolpytest_with_pytest(self, pytester: Pytester) -> None:
|
|
|
|
p1 = pytester.makepyfile("def test(): pass")
|
|
|
|
pytester.makefile(
|
2019-03-28 07:27:23 +08:00
|
|
|
".cfg",
|
|
|
|
setup="""
|
|
|
|
[tool:pytest]
|
|
|
|
testpaths=%s
|
|
|
|
[pytest]
|
|
|
|
testpaths=ignored
|
|
|
|
"""
|
2020-12-09 04:20:02 +08:00
|
|
|
% p1.name,
|
2019-03-28 07:27:23 +08:00
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest()
|
2023-02-12 16:46:07 +08:00
|
|
|
result.stdout.fnmatch_lines(["configfile: setup.cfg", "* 1 passed in *"])
|
2019-03-28 07:27:23 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_append_parse_args(
|
|
|
|
self, pytester: Pytester, tmp_path: Path, monkeypatch: MonkeyPatch
|
|
|
|
) -> None:
|
2018-05-23 22:48:46 +08:00
|
|
|
monkeypatch.setenv("PYTEST_ADDOPTS", '--color no -rs --tb="short"')
|
2020-12-09 04:20:02 +08:00
|
|
|
tmp_path.joinpath("pytest.ini").write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
[pytest]
|
|
|
|
addopts = --verbose
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2023-06-20 19:55:39 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
config = pytester.parseconfig(tmp_path)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert config.option.color == "no"
|
|
|
|
assert config.option.reportchars == "s"
|
|
|
|
assert config.option.tbstyle == "short"
|
2010-10-28 01:35:27 +08:00
|
|
|
assert config.option.verbose
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_tox_ini_wrong_version(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makefile(
|
2018-05-23 22:48:46 +08:00
|
|
|
".ini",
|
|
|
|
tox="""
|
2010-10-28 01:35:27 +08:00
|
|
|
[pytest]
|
2020-06-08 21:03:10 +08:00
|
|
|
minversion=999.0
|
2018-05-23 22:48:46 +08:00
|
|
|
""",
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest()
|
2010-10-28 01:35:27 +08:00
|
|
|
assert result.ret != 0
|
2020-06-08 21:03:10 +08:00
|
|
|
result.stderr.fnmatch_lines(
|
|
|
|
["*tox.ini: 'minversion' requires pytest-999.0, actual pytest-*"]
|
|
|
|
)
|
2018-05-23 22:48:46 +08:00
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"section, name",
|
2022-06-14 19:24:35 +08:00
|
|
|
[
|
|
|
|
("tool:pytest", "setup.cfg"),
|
|
|
|
("pytest", "tox.ini"),
|
|
|
|
("pytest", "pytest.ini"),
|
|
|
|
("pytest", ".pytest.ini"),
|
|
|
|
],
|
2016-08-17 08:30:07 +08:00
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_ini_names(self, pytester: Pytester, name, section) -> None:
|
|
|
|
pytester.path.joinpath(name).write_text(
|
2018-05-23 22:48:46 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""
|
2016-08-17 08:30:07 +08:00
|
|
|
[{section}]
|
2022-06-14 19:24:35 +08:00
|
|
|
minversion = 3.36
|
2018-05-23 22:48:46 +08:00
|
|
|
""".format(
|
|
|
|
section=section
|
|
|
|
)
|
2023-06-20 19:55:39 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
config = pytester.parseconfig()
|
2022-06-14 19:24:35 +08:00
|
|
|
assert config.getini("minversion") == "3.36"
|
2010-11-07 23:10:22 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_pyproject_toml(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyprojecttoml(
|
2020-06-08 21:03:10 +08:00
|
|
|
"""
|
|
|
|
[tool.pytest.ini_options]
|
|
|
|
minversion = "1.0"
|
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
config = pytester.parseconfig()
|
2020-06-08 21:03:10 +08:00
|
|
|
assert config.getini("minversion") == "1.0"
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_toxini_before_lower_pytestini(self, pytester: Pytester) -> None:
|
|
|
|
sub = pytester.mkdir("sub")
|
|
|
|
sub.joinpath("tox.ini").write_text(
|
2018-05-23 22:48:46 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""
|
2010-11-07 23:10:22 +08:00
|
|
|
[pytest]
|
|
|
|
minversion = 2.0
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2023-06-20 19:55:39 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.path.joinpath("pytest.ini").write_text(
|
2018-05-23 22:48:46 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""
|
2010-11-07 23:10:22 +08:00
|
|
|
[pytest]
|
|
|
|
minversion = 1.5
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2023-06-20 19:55:39 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
config = pytester.parseconfigure(sub)
|
2010-11-07 23:10:22 +08:00
|
|
|
assert config.getini("minversion") == "2.0"
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_ini_parse_error(self, pytester: Pytester) -> None:
|
2023-06-20 19:55:39 +08:00
|
|
|
pytester.path.joinpath("pytest.ini").write_text(
|
|
|
|
"addopts = -x", encoding="utf-8"
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest()
|
2019-07-23 21:24:22 +08:00
|
|
|
assert result.ret != 0
|
2022-03-05 19:57:00 +08:00
|
|
|
result.stderr.fnmatch_lines("ERROR: *pytest.ini:1: no section header defined")
|
|
|
|
|
|
|
|
def test_toml_parse_error(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyprojecttoml(
|
|
|
|
"""
|
|
|
|
\\"
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = pytester.runpytest()
|
|
|
|
assert result.ret != 0
|
|
|
|
result.stderr.fnmatch_lines("ERROR: *pyproject.toml: Invalid statement*")
|
2019-07-23 21:24:22 +08:00
|
|
|
|
2023-05-27 23:42:37 +08:00
|
|
|
def test_confcutdir_default_without_configfile(self, pytester: Pytester) -> None:
|
|
|
|
# If --confcutdir is not specified, and there is no configfile, default
|
|
|
|
# to the roothpath.
|
|
|
|
sub = pytester.mkdir("sub")
|
|
|
|
os.chdir(sub)
|
|
|
|
config = pytester.parseconfigure()
|
|
|
|
assert config.pluginmanager._confcutdir == sub
|
|
|
|
|
|
|
|
def test_confcutdir_default_with_configfile(self, pytester: Pytester) -> None:
|
|
|
|
# If --confcutdir is not specified, and there is a configfile, default
|
|
|
|
# to the configfile's directory.
|
|
|
|
pytester.makeini("[pytest]")
|
|
|
|
sub = pytester.mkdir("sub")
|
|
|
|
os.chdir(sub)
|
|
|
|
config = pytester.parseconfigure()
|
|
|
|
assert config.pluginmanager._confcutdir == pytester.path
|
|
|
|
|
2010-11-18 05:12:16 +08:00
|
|
|
@pytest.mark.xfail(reason="probably not needed")
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_confcutdir(self, pytester: Pytester) -> None:
|
|
|
|
sub = pytester.mkdir("sub")
|
|
|
|
os.chdir(sub)
|
|
|
|
pytester.makeini(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2010-11-07 08:10:15 +08:00
|
|
|
[pytest]
|
|
|
|
addopts = --qwe
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.inline_run("--confcutdir=.")
|
2010-11-07 08:10:15 +08:00
|
|
|
assert result.ret == 0
|
2016-12-01 20:20:42 +08:00
|
|
|
|
2020-05-31 08:36:02 +08:00
|
|
|
@pytest.mark.parametrize(
|
2020-06-21 00:15:58 +08:00
|
|
|
"ini_file_text, invalid_keys, warning_output, exception_text",
|
2020-05-31 08:36:02 +08:00
|
|
|
[
|
2020-09-19 23:57:29 +08:00
|
|
|
pytest.param(
|
2020-05-31 08:36:02 +08:00
|
|
|
"""
|
2020-09-19 23:57:29 +08:00
|
|
|
[pytest]
|
|
|
|
unknown_ini = value1
|
|
|
|
another_unknown_ini = value2
|
|
|
|
""",
|
2020-05-31 08:36:02 +08:00
|
|
|
["unknown_ini", "another_unknown_ini"],
|
|
|
|
[
|
2020-06-21 00:15:58 +08:00
|
|
|
"=*= warnings summary =*=",
|
2020-09-04 22:57:15 +08:00
|
|
|
"*PytestConfigWarning:*Unknown config option: another_unknown_ini",
|
|
|
|
"*PytestConfigWarning:*Unknown config option: unknown_ini",
|
2020-05-31 08:36:02 +08:00
|
|
|
],
|
2020-09-04 22:57:15 +08:00
|
|
|
"Unknown config option: another_unknown_ini",
|
2020-09-19 23:57:29 +08:00
|
|
|
id="2-unknowns",
|
2020-05-31 08:36:02 +08:00
|
|
|
),
|
2020-09-19 23:57:29 +08:00
|
|
|
pytest.param(
|
2020-05-31 08:36:02 +08:00
|
|
|
"""
|
2020-09-19 23:57:29 +08:00
|
|
|
[pytest]
|
|
|
|
unknown_ini = value1
|
|
|
|
minversion = 5.0.0
|
|
|
|
""",
|
2020-05-31 08:36:02 +08:00
|
|
|
["unknown_ini"],
|
2020-06-21 00:15:58 +08:00
|
|
|
[
|
|
|
|
"=*= warnings summary =*=",
|
2020-09-04 22:57:15 +08:00
|
|
|
"*PytestConfigWarning:*Unknown config option: unknown_ini",
|
2020-06-21 00:15:58 +08:00
|
|
|
],
|
2020-09-04 22:57:15 +08:00
|
|
|
"Unknown config option: unknown_ini",
|
2020-09-19 23:57:29 +08:00
|
|
|
id="1-unknown",
|
2020-05-31 08:36:02 +08:00
|
|
|
),
|
2020-09-19 23:57:29 +08:00
|
|
|
pytest.param(
|
2020-05-31 08:36:02 +08:00
|
|
|
"""
|
2020-09-19 23:57:29 +08:00
|
|
|
[some_other_header]
|
|
|
|
unknown_ini = value1
|
|
|
|
[pytest]
|
|
|
|
minversion = 5.0.0
|
|
|
|
""",
|
2020-05-31 12:49:21 +08:00
|
|
|
[],
|
|
|
|
[],
|
2020-05-31 14:45:40 +08:00
|
|
|
"",
|
2020-09-19 23:57:29 +08:00
|
|
|
id="unknown-in-other-header",
|
2020-05-31 12:49:21 +08:00
|
|
|
),
|
2020-09-19 23:57:29 +08:00
|
|
|
pytest.param(
|
2020-05-31 12:49:21 +08:00
|
|
|
"""
|
2020-09-19 23:57:29 +08:00
|
|
|
[pytest]
|
|
|
|
minversion = 5.0.0
|
|
|
|
""",
|
2020-06-18 23:58:41 +08:00
|
|
|
[],
|
|
|
|
[],
|
|
|
|
"",
|
2020-09-19 23:57:29 +08:00
|
|
|
id="no-unknowns",
|
2020-06-18 23:58:41 +08:00
|
|
|
),
|
2020-09-19 23:57:29 +08:00
|
|
|
pytest.param(
|
2020-06-18 23:58:41 +08:00
|
|
|
"""
|
2020-09-19 23:57:29 +08:00
|
|
|
[pytest]
|
|
|
|
conftest_ini_key = 1
|
|
|
|
""",
|
2020-05-31 08:36:02 +08:00
|
|
|
[],
|
|
|
|
[],
|
2020-05-31 14:45:40 +08:00
|
|
|
"",
|
2020-09-19 23:57:29 +08:00
|
|
|
id="1-known",
|
2020-05-31 08:36:02 +08:00
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2020-09-04 22:57:15 +08:00
|
|
|
@pytest.mark.filterwarnings("default")
|
|
|
|
def test_invalid_config_options(
|
2020-12-09 04:20:02 +08:00
|
|
|
self,
|
|
|
|
pytester: Pytester,
|
|
|
|
ini_file_text,
|
|
|
|
invalid_keys,
|
|
|
|
warning_output,
|
|
|
|
exception_text,
|
|
|
|
) -> None:
|
|
|
|
pytester.makeconftest(
|
2020-06-18 23:58:41 +08:00
|
|
|
"""
|
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addini("conftest_ini_key", "")
|
2020-09-19 23:57:29 +08:00
|
|
|
"""
|
2020-06-18 23:58:41 +08:00
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makepyfile("def test(): pass")
|
|
|
|
pytester.makeini(ini_file_text)
|
2020-06-18 23:58:41 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
config = pytester.parseconfig()
|
2020-06-03 03:01:47 +08:00
|
|
|
assert sorted(config._get_unknown_ini_keys()) == sorted(invalid_keys)
|
2020-05-31 08:36:02 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest()
|
2020-06-21 00:15:58 +08:00
|
|
|
result.stdout.fnmatch_lines(warning_output)
|
2020-05-31 08:36:02 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest("--strict-config")
|
2020-06-18 23:58:41 +08:00
|
|
|
if exception_text:
|
2020-09-19 23:57:29 +08:00
|
|
|
result.stderr.fnmatch_lines("ERROR: " + exception_text)
|
|
|
|
assert result.ret == pytest.ExitCode.USAGE_ERROR
|
|
|
|
else:
|
|
|
|
result.stderr.no_fnmatch_line(exception_text)
|
|
|
|
assert result.ret == pytest.ExitCode.OK
|
2020-09-04 22:57:15 +08:00
|
|
|
|
|
|
|
@pytest.mark.filterwarnings("default")
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_silence_unknown_key_warning(self, pytester: Pytester) -> None:
|
2020-09-04 22:57:15 +08:00
|
|
|
"""Unknown config key warnings can be silenced using filterwarnings (#7620)"""
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makeini(
|
2020-09-04 22:57:15 +08:00
|
|
|
"""
|
|
|
|
[pytest]
|
|
|
|
filterwarnings =
|
|
|
|
ignore:Unknown config option:pytest.PytestConfigWarning
|
|
|
|
foobar=1
|
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest()
|
2020-09-04 22:57:15 +08:00
|
|
|
result.stdout.no_fnmatch_line("*PytestConfigWarning*")
|
|
|
|
|
2021-05-04 20:27:21 +08:00
|
|
|
@pytest.mark.filterwarnings("default::pytest.PytestConfigWarning")
|
2020-09-04 22:57:15 +08:00
|
|
|
def test_disable_warnings_plugin_disables_config_warnings(
|
2020-12-09 04:20:02 +08:00
|
|
|
self, pytester: Pytester
|
2020-09-04 22:57:15 +08:00
|
|
|
) -> None:
|
|
|
|
"""Disabling 'warnings' plugin also disables config time warnings"""
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makeconftest(
|
2020-09-04 22:57:15 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
def pytest_configure(config):
|
|
|
|
config.issue_config_time_warning(
|
|
|
|
pytest.PytestConfigWarning("custom config warning"),
|
|
|
|
stacklevel=2,
|
|
|
|
)
|
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest("-pno:warnings")
|
2020-09-04 22:57:15 +08:00
|
|
|
result.stdout.no_fnmatch_line("*PytestConfigWarning*")
|
2020-05-31 14:45:40 +08:00
|
|
|
|
2020-06-06 14:38:18 +08:00
|
|
|
@pytest.mark.parametrize(
|
2021-03-22 05:51:12 +08:00
|
|
|
"ini_file_text, plugin_version, exception_text",
|
2020-06-06 14:38:18 +08:00
|
|
|
[
|
2020-07-10 09:09:28 +08:00
|
|
|
pytest.param(
|
2020-06-06 14:38:18 +08:00
|
|
|
"""
|
2020-07-10 09:09:28 +08:00
|
|
|
[pytest]
|
|
|
|
required_plugins = a z
|
|
|
|
""",
|
2021-03-22 05:51:12 +08:00
|
|
|
"1.5",
|
2020-06-10 12:44:22 +08:00
|
|
|
"Missing required plugins: a, z",
|
2020-07-10 09:09:28 +08:00
|
|
|
id="2-missing",
|
2020-06-06 14:38:18 +08:00
|
|
|
),
|
2020-07-10 09:09:28 +08:00
|
|
|
pytest.param(
|
2020-06-13 14:47:15 +08:00
|
|
|
"""
|
2020-07-10 09:09:28 +08:00
|
|
|
[pytest]
|
|
|
|
required_plugins = a z myplugin
|
|
|
|
""",
|
2021-03-22 05:51:12 +08:00
|
|
|
"1.5",
|
2020-07-10 09:09:28 +08:00
|
|
|
"Missing required plugins: a, z",
|
|
|
|
id="2-missing-1-ok",
|
2020-06-13 14:47:15 +08:00
|
|
|
),
|
2020-07-10 09:09:28 +08:00
|
|
|
pytest.param(
|
2020-06-13 14:47:15 +08:00
|
|
|
"""
|
2020-07-10 09:09:28 +08:00
|
|
|
[pytest]
|
|
|
|
required_plugins = myplugin
|
|
|
|
""",
|
2021-03-22 05:51:12 +08:00
|
|
|
"1.5",
|
2020-07-10 09:09:28 +08:00
|
|
|
None,
|
|
|
|
id="1-ok",
|
2020-06-13 14:47:15 +08:00
|
|
|
),
|
2020-07-10 09:09:28 +08:00
|
|
|
pytest.param(
|
2020-06-13 14:47:15 +08:00
|
|
|
"""
|
2020-07-10 09:09:28 +08:00
|
|
|
[pytest]
|
|
|
|
required_plugins = myplugin==1.5
|
|
|
|
""",
|
2021-03-22 05:51:12 +08:00
|
|
|
"1.5",
|
2020-07-10 09:09:28 +08:00
|
|
|
None,
|
|
|
|
id="1-ok-pin-exact",
|
2020-06-13 14:47:15 +08:00
|
|
|
),
|
2020-07-10 09:09:28 +08:00
|
|
|
pytest.param(
|
2020-06-13 14:47:15 +08:00
|
|
|
"""
|
2020-07-10 09:09:28 +08:00
|
|
|
[pytest]
|
|
|
|
required_plugins = myplugin>1.0,<2.0
|
|
|
|
""",
|
2021-03-22 05:51:12 +08:00
|
|
|
"1.5",
|
2020-07-10 09:09:28 +08:00
|
|
|
None,
|
|
|
|
id="1-ok-pin-loose",
|
2020-06-13 14:47:15 +08:00
|
|
|
),
|
2020-07-10 09:09:28 +08:00
|
|
|
pytest.param(
|
2020-06-13 14:47:15 +08:00
|
|
|
"""
|
2020-07-10 09:09:28 +08:00
|
|
|
[pytest]
|
2021-03-22 05:51:12 +08:00
|
|
|
required_plugins = myplugin
|
|
|
|
""",
|
|
|
|
"1.5a1",
|
|
|
|
None,
|
|
|
|
id="1-ok-prerelease",
|
|
|
|
),
|
|
|
|
pytest.param(
|
|
|
|
"""
|
|
|
|
[pytest]
|
|
|
|
required_plugins = myplugin==1.6
|
2020-07-10 09:09:28 +08:00
|
|
|
""",
|
2021-03-22 05:51:12 +08:00
|
|
|
"1.5",
|
|
|
|
"Missing required plugins: myplugin==1.6",
|
2020-07-10 09:09:28 +08:00
|
|
|
id="missing-version",
|
2020-06-13 14:47:15 +08:00
|
|
|
),
|
2020-07-10 09:09:28 +08:00
|
|
|
pytest.param(
|
2020-06-13 14:47:15 +08:00
|
|
|
"""
|
2020-07-10 09:09:28 +08:00
|
|
|
[pytest]
|
2021-03-22 05:51:12 +08:00
|
|
|
required_plugins = myplugin==1.6 other==1.0
|
2020-07-10 09:09:28 +08:00
|
|
|
""",
|
2021-03-22 05:51:12 +08:00
|
|
|
"1.5",
|
|
|
|
"Missing required plugins: myplugin==1.6, other==1.0",
|
2020-07-10 09:09:28 +08:00
|
|
|
id="missing-versions",
|
2020-06-06 14:38:18 +08:00
|
|
|
),
|
2020-07-10 09:09:28 +08:00
|
|
|
pytest.param(
|
2020-06-06 14:38:18 +08:00
|
|
|
"""
|
2020-07-10 09:09:28 +08:00
|
|
|
[some_other_header]
|
2021-12-27 20:23:15 +08:00
|
|
|
required_plugins = won't be triggered
|
2020-07-10 09:09:28 +08:00
|
|
|
[pytest]
|
|
|
|
""",
|
2021-03-22 05:51:12 +08:00
|
|
|
"1.5",
|
2020-07-10 09:09:28 +08:00
|
|
|
None,
|
|
|
|
id="invalid-header",
|
2020-06-06 14:38:18 +08:00
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2020-07-10 09:09:28 +08:00
|
|
|
def test_missing_required_plugins(
|
2020-12-09 04:20:02 +08:00
|
|
|
self,
|
|
|
|
pytester: Pytester,
|
|
|
|
monkeypatch: MonkeyPatch,
|
|
|
|
ini_file_text: str,
|
2021-03-22 05:51:12 +08:00
|
|
|
plugin_version: str,
|
2020-12-09 04:20:02 +08:00
|
|
|
exception_text: str,
|
|
|
|
) -> None:
|
2020-07-10 09:09:28 +08:00
|
|
|
"""Check 'required_plugins' option with various settings.
|
2020-06-06 14:38:18 +08:00
|
|
|
|
2020-07-10 09:09:28 +08:00
|
|
|
This test installs a mock "myplugin-1.5" which is used in the parametrized test cases.
|
|
|
|
"""
|
|
|
|
|
2023-01-20 17:13:36 +08:00
|
|
|
@dataclasses.dataclass
|
2020-07-10 09:09:28 +08:00
|
|
|
class DummyEntryPoint:
|
2023-01-20 17:13:36 +08:00
|
|
|
name: str
|
|
|
|
module: str
|
|
|
|
group: str = "pytest11"
|
2020-07-10 09:09:28 +08:00
|
|
|
|
|
|
|
def load(self):
|
|
|
|
__import__(self.module)
|
|
|
|
return sys.modules[self.module]
|
|
|
|
|
|
|
|
entry_points = [
|
|
|
|
DummyEntryPoint("myplugin1", "myplugin1_module"),
|
|
|
|
]
|
|
|
|
|
2023-01-20 17:13:36 +08:00
|
|
|
@dataclasses.dataclass
|
2020-07-10 09:09:28 +08:00
|
|
|
class DummyDist:
|
2023-01-20 17:13:36 +08:00
|
|
|
entry_points: object
|
|
|
|
files: object = ()
|
|
|
|
version: str = plugin_version
|
2020-07-10 09:09:28 +08:00
|
|
|
|
|
|
|
@property
|
|
|
|
def metadata(self):
|
|
|
|
return {"name": "myplugin"}
|
|
|
|
|
|
|
|
def my_dists():
|
|
|
|
return [DummyDist(entry_points)]
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makepyfile(myplugin1_module="# my plugin module")
|
|
|
|
pytester.syspathinsert()
|
2020-07-10 09:09:28 +08:00
|
|
|
|
2023-07-01 05:55:42 +08:00
|
|
|
monkeypatch.setattr(importlib.metadata, "distributions", my_dists)
|
2020-12-09 04:20:02 +08:00
|
|
|
monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", raising=False)
|
2020-07-10 09:09:28 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makeini(ini_file_text)
|
2020-06-06 14:38:18 +08:00
|
|
|
|
2020-06-10 12:44:22 +08:00
|
|
|
if exception_text:
|
2020-09-16 18:13:17 +08:00
|
|
|
with pytest.raises(pytest.UsageError, match=exception_text):
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.parseconfig()
|
2020-06-10 12:44:22 +08:00
|
|
|
else:
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.parseconfig()
|
2020-05-31 14:45:40 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_early_config_cmdline(
|
|
|
|
self, pytester: Pytester, monkeypatch: MonkeyPatch
|
|
|
|
) -> None:
|
2020-09-20 02:56:52 +08:00
|
|
|
"""early_config contains options registered by third-party plugins.
|
|
|
|
|
|
|
|
This is a regression involving pytest-cov (and possibly others) introduced in #7700.
|
|
|
|
"""
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makepyfile(
|
2020-09-20 02:56:52 +08:00
|
|
|
myplugin="""
|
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addoption('--foo', default=None, dest='foo')
|
|
|
|
|
|
|
|
def pytest_load_initial_conftests(early_config, parser, args):
|
|
|
|
assert early_config.known_args_namespace.foo == "1"
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
monkeypatch.setenv("PYTEST_PLUGINS", "myplugin")
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.syspathinsert()
|
|
|
|
result = pytester.runpytest("--foo=1")
|
2020-09-20 02:56:52 +08:00
|
|
|
result.stdout.fnmatch_lines("* no tests ran in *")
|
|
|
|
|
2023-08-23 17:21:17 +08:00
|
|
|
def test_args_source_args(self, pytester: Pytester):
|
|
|
|
config = pytester.parseconfig("--", "test_filename.py")
|
|
|
|
assert config.args_source == Config.ArgsSource.ARGS
|
|
|
|
|
|
|
|
def test_args_source_invocation_dir(self, pytester: Pytester):
|
|
|
|
config = pytester.parseconfig()
|
|
|
|
assert config.args_source == Config.ArgsSource.INVOCATION_DIR
|
|
|
|
|
|
|
|
def test_args_source_testpaths(self, pytester: Pytester):
|
|
|
|
pytester.makeini(
|
|
|
|
"""
|
|
|
|
[pytest]
|
|
|
|
testpaths=*
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
config = pytester.parseconfig()
|
|
|
|
assert config.args_source == Config.ArgsSource.TESTPATHS
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestConfigCmdlineParsing:
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_parsing_again_fails(self, pytester: Pytester) -> None:
|
|
|
|
config = pytester.parseconfig()
|
2013-10-12 21:39:22 +08:00
|
|
|
pytest.raises(AssertionError, lambda: config.parse([]))
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_explicitly_specified_config_file_is_loaded(
|
|
|
|
self, pytester: Pytester
|
|
|
|
) -> None:
|
|
|
|
pytester.makeconftest(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2014-06-27 23:42:37 +08:00
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addini("custom", "")
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makeini(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2014-06-27 23:42:37 +08:00
|
|
|
[pytest]
|
|
|
|
custom = 0
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makefile(
|
2018-09-02 08:58:48 +08:00
|
|
|
".ini",
|
2018-05-23 22:48:46 +08:00
|
|
|
custom="""
|
2014-06-27 23:42:37 +08:00
|
|
|
[pytest]
|
|
|
|
custom = 1
|
2018-05-23 22:48:46 +08:00
|
|
|
""",
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
config = pytester.parseconfig("-c", "custom.ini")
|
2014-06-27 23:42:37 +08:00
|
|
|
assert config.getini("custom") == "1"
|
2023-05-26 18:56:18 +08:00
|
|
|
config = pytester.parseconfig("--config-file", "custom.ini")
|
|
|
|
assert config.getini("custom") == "1"
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makefile(
|
2018-05-23 22:48:46 +08:00
|
|
|
".cfg",
|
|
|
|
custom_tool_pytest_section="""
|
2018-02-28 00:16:45 +08:00
|
|
|
[tool:pytest]
|
|
|
|
custom = 1
|
2018-05-23 22:48:46 +08:00
|
|
|
""",
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
config = pytester.parseconfig("-c", "custom_tool_pytest_section.cfg")
|
2018-02-28 00:16:45 +08:00
|
|
|
assert config.getini("custom") == "1"
|
2023-05-26 18:56:18 +08:00
|
|
|
config = pytester.parseconfig("--config-file", "custom_tool_pytest_section.cfg")
|
|
|
|
assert config.getini("custom") == "1"
|
2018-02-28 00:16:45 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makefile(
|
2020-06-08 21:03:10 +08:00
|
|
|
".toml",
|
|
|
|
custom="""
|
|
|
|
[tool.pytest.ini_options]
|
|
|
|
custom = 1
|
|
|
|
value = [
|
|
|
|
] # this is here on purpose, as it makes this an invalid '.ini' file
|
|
|
|
""",
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
config = pytester.parseconfig("-c", "custom.toml")
|
2020-06-08 21:03:10 +08:00
|
|
|
assert config.getini("custom") == "1"
|
2023-05-26 18:56:18 +08:00
|
|
|
config = pytester.parseconfig("--config-file", "custom.toml")
|
|
|
|
assert config.getini("custom") == "1"
|
2020-06-08 21:03:10 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_absolute_win32_path(self, pytester: Pytester) -> None:
|
|
|
|
temp_ini_file = pytester.makefile(
|
2018-09-02 08:58:48 +08:00
|
|
|
".ini",
|
2018-05-23 22:48:46 +08:00
|
|
|
custom="""
|
2016-04-20 01:27:37 +08:00
|
|
|
[pytest]
|
|
|
|
addopts = --version
|
2018-05-23 22:48:46 +08:00
|
|
|
""",
|
|
|
|
)
|
2016-04-20 01:27:37 +08:00
|
|
|
from os.path import normpath
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
temp_ini_file_norm = normpath(str(temp_ini_file))
|
|
|
|
ret = pytest.main(["-c", temp_ini_file_norm])
|
2019-06-07 18:58:51 +08:00
|
|
|
assert ret == ExitCode.OK
|
2023-05-26 18:56:18 +08:00
|
|
|
ret = pytest.main(["--config-file", temp_ini_file_norm])
|
|
|
|
assert ret == ExitCode.OK
|
2016-04-20 01:27:37 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestConfigAPI:
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_config_trace(self, pytester: Pytester) -> None:
|
|
|
|
config = pytester.parseconfig()
|
2020-10-06 09:13:05 +08:00
|
|
|
values: List[str] = []
|
2017-11-04 23:17:20 +08:00
|
|
|
config.trace.root.setwriter(values.append)
|
2010-11-06 06:37:31 +08:00
|
|
|
config.trace("hello")
|
2017-11-04 23:17:20 +08:00
|
|
|
assert len(values) == 1
|
|
|
|
assert values[0] == "hello [config]\n"
|
2010-11-06 06:37:31 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_config_getoption(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2012-11-06 21:09:12 +08:00
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addoption("--hello", "-X", dest="hello")
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
config = pytester.parseconfig("--hello=this")
|
2012-11-06 21:09:12 +08:00
|
|
|
for x in ("hello", "--hello", "-X"):
|
|
|
|
assert config.getoption(x) == "this"
|
2018-11-23 02:05:10 +08:00
|
|
|
pytest.raises(ValueError, config.getoption, "qweqwe")
|
2012-11-06 21:09:12 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_config_getoption_unicode(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2013-10-01 20:20:20 +08:00
|
|
|
def pytest_addoption(parser):
|
2017-03-21 09:01:22 +08:00
|
|
|
parser.addoption('--hello', type=str)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
config = pytester.parseconfig("--hello=this")
|
2018-05-23 22:48:46 +08:00
|
|
|
assert config.getoption("hello") == "this"
|
2013-10-01 20:20:20 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_config_getvalueorskip(self, pytester: Pytester) -> None:
|
|
|
|
config = pytester.parseconfig()
|
2018-11-23 02:05:10 +08:00
|
|
|
pytest.raises(pytest.skip.Exception, config.getvalueorskip, "hello")
|
2009-03-02 19:14:59 +08:00
|
|
|
verbose = config.getvalueorskip("verbose")
|
|
|
|
assert verbose == config.option.verbose
|
2014-04-03 04:30:45 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_config_getvalueorskip_None(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2014-05-14 13:36:31 +08:00
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addoption("--hello")
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
config = pytester.parseconfig()
|
2014-06-16 17:27:32 +08:00
|
|
|
with pytest.raises(pytest.skip.Exception):
|
2018-05-23 22:48:46 +08:00
|
|
|
config.getvalueorskip("hello")
|
2014-05-14 13:36:31 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_getoption(self, pytester: Pytester) -> None:
|
|
|
|
config = pytester.parseconfig()
|
2014-04-03 04:30:45 +08:00
|
|
|
with pytest.raises(ValueError):
|
2018-05-23 22:48:46 +08:00
|
|
|
config.getvalue("x")
|
2014-04-03 04:30:45 +08:00
|
|
|
assert config.getoption("x", 1) == 1
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2020-12-11 19:40:37 +08:00
|
|
|
def test_getconftest_pathlist(self, pytester: Pytester, tmp_path: Path) -> None:
|
|
|
|
somepath = tmp_path.joinpath("x", "y", "z")
|
|
|
|
p = tmp_path.joinpath("conftest.py")
|
2023-06-20 19:55:39 +08:00
|
|
|
p.write_text(f"mylist = {['.', str(somepath)]}", encoding="utf-8")
|
2020-12-09 04:20:02 +08:00
|
|
|
config = pytester.parseconfigure(p)
|
2022-01-09 19:58:29 +08:00
|
|
|
assert config._getconftest_pathlist("notexist", path=tmp_path) is None
|
|
|
|
assert config._getconftest_pathlist("mylist", path=tmp_path) == [
|
|
|
|
tmp_path,
|
|
|
|
somepath,
|
|
|
|
]
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2020-10-08 05:56:54 +08:00
|
|
|
@pytest.mark.parametrize("maybe_type", ["not passed", "None", '"string"'])
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_addini(self, pytester: Pytester, maybe_type: str) -> None:
|
2020-10-08 05:56:54 +08:00
|
|
|
if maybe_type == "not passed":
|
|
|
|
type_string = ""
|
|
|
|
else:
|
|
|
|
type_string = f", {maybe_type}"
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makeconftest(
|
2020-10-08 05:56:54 +08:00
|
|
|
f"""
|
2010-11-01 00:41:58 +08:00
|
|
|
def pytest_addoption(parser):
|
2020-10-08 05:56:54 +08:00
|
|
|
parser.addini("myname", "my new ini value"{type_string})
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makeini(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2010-11-01 00:41:58 +08:00
|
|
|
[pytest]
|
|
|
|
myname=hello
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
config = pytester.parseconfig()
|
2010-11-01 00:41:58 +08:00
|
|
|
val = config.getini("myname")
|
|
|
|
assert val == "hello"
|
2018-05-23 22:48:46 +08:00
|
|
|
pytest.raises(ValueError, config.getini, "other")
|
2010-11-01 00:41:58 +08:00
|
|
|
|
2021-04-26 22:46:26 +08:00
|
|
|
@pytest.mark.parametrize("config_type", ["ini", "pyproject"])
|
2021-10-16 16:21:02 +08:00
|
|
|
def test_addini_paths(self, pytester: Pytester, config_type: str) -> None:
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makeconftest(
|
2021-10-16 16:21:02 +08:00
|
|
|
"""
|
2010-11-01 00:41:58 +08:00
|
|
|
def pytest_addoption(parser):
|
2021-10-16 16:21:02 +08:00
|
|
|
parser.addini("paths", "my new ini value", type="paths")
|
2010-11-01 00:41:58 +08:00
|
|
|
parser.addini("abc", "abc value")
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2021-04-26 22:46:26 +08:00
|
|
|
if config_type == "ini":
|
|
|
|
inipath = pytester.makeini(
|
|
|
|
"""
|
|
|
|
[pytest]
|
|
|
|
paths=hello world/sub.py
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2021-04-26 22:46:26 +08:00
|
|
|
)
|
|
|
|
elif config_type == "pyproject":
|
|
|
|
inipath = pytester.makepyprojecttoml(
|
|
|
|
"""
|
|
|
|
[tool.pytest.ini_options]
|
|
|
|
paths=["hello", "world/sub.py"]
|
2020-06-08 21:03:10 +08:00
|
|
|
"""
|
2021-04-26 22:46:26 +08:00
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
config = pytester.parseconfig()
|
2017-11-04 23:17:20 +08:00
|
|
|
values = config.getini("paths")
|
|
|
|
assert len(values) == 2
|
2021-04-26 22:46:26 +08:00
|
|
|
assert values[0] == inipath.parent.joinpath("hello")
|
|
|
|
assert values[1] == inipath.parent.joinpath("world/sub.py")
|
2018-05-23 22:48:46 +08:00
|
|
|
pytest.raises(ValueError, config.getini, "other")
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def make_conftest_for_args(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2010-11-05 06:21:26 +08:00
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addini("args", "new args", type="args")
|
|
|
|
parser.addini("a2", "", "args", default="1 2 3".split())
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-06-08 21:03:10 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_addini_args_ini_files(self, pytester: Pytester) -> None:
|
|
|
|
self.make_conftest_for_args(pytester)
|
|
|
|
pytester.makeini(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2010-11-05 06:21:26 +08:00
|
|
|
[pytest]
|
|
|
|
args=123 "123 hello" "this"
|
2020-06-08 21:03:10 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
self.check_config_args(pytester)
|
2020-06-08 21:03:10 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_addini_args_pyproject_toml(self, pytester: Pytester) -> None:
|
|
|
|
self.make_conftest_for_args(pytester)
|
|
|
|
pytester.makepyprojecttoml(
|
2020-06-08 21:03:10 +08:00
|
|
|
"""
|
|
|
|
[tool.pytest.ini_options]
|
|
|
|
args = ["123", "123 hello", "this"]
|
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
self.check_config_args(pytester)
|
2020-06-08 21:03:10 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def check_config_args(self, pytester: Pytester) -> None:
|
|
|
|
config = pytester.parseconfig()
|
2017-11-04 23:17:20 +08:00
|
|
|
values = config.getini("args")
|
|
|
|
assert values == ["123", "123 hello", "this"]
|
|
|
|
values = config.getini("a2")
|
|
|
|
assert values == list("123")
|
2010-11-05 06:21:26 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def make_conftest_for_linelist(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2010-11-06 06:37:31 +08:00
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addini("xy", "", type="linelist")
|
|
|
|
parser.addini("a2", "", "linelist")
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-06-08 21:03:10 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_addini_linelist_ini_files(self, pytester: Pytester) -> None:
|
|
|
|
self.make_conftest_for_linelist(pytester)
|
|
|
|
pytester.makeini(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2010-11-06 06:37:31 +08:00
|
|
|
[pytest]
|
|
|
|
xy= 123 345
|
|
|
|
second line
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
self.check_config_linelist(pytester)
|
2020-06-08 21:03:10 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_addini_linelist_pprojecttoml(self, pytester: Pytester) -> None:
|
|
|
|
self.make_conftest_for_linelist(pytester)
|
|
|
|
pytester.makepyprojecttoml(
|
2020-06-08 21:03:10 +08:00
|
|
|
"""
|
|
|
|
[tool.pytest.ini_options]
|
|
|
|
xy = ["123 345", "second line"]
|
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
self.check_config_linelist(pytester)
|
2020-06-08 21:03:10 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def check_config_linelist(self, pytester: Pytester) -> None:
|
|
|
|
config = pytester.parseconfig()
|
2017-11-04 23:17:20 +08:00
|
|
|
values = config.getini("xy")
|
|
|
|
assert len(values) == 2
|
|
|
|
assert values == ["123 345", "second line"]
|
|
|
|
values = config.getini("a2")
|
|
|
|
assert values == []
|
2010-11-06 06:37:31 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"str_val, bool_val", [("True", True), ("no", False), ("no-ini", True)]
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_addini_bool(
|
|
|
|
self, pytester: Pytester, str_val: str, bool_val: bool
|
|
|
|
) -> None:
|
|
|
|
pytester.makeconftest(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2016-02-15 05:27:48 +08:00
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addini("strip", "", type="bool", default=True)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
if str_val != "no-ini":
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makeini(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2016-02-15 05:27:48 +08:00
|
|
|
[pytest]
|
|
|
|
strip=%s
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
% str_val
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
config = pytester.parseconfig()
|
2016-02-15 05:27:48 +08:00
|
|
|
assert config.getini("strip") is bool_val
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_addinivalue_line_existing(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2011-11-12 06:56:08 +08:00
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addini("xy", "", type="linelist")
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makeini(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2011-11-12 06:56:08 +08:00
|
|
|
[pytest]
|
|
|
|
xy= 123
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
config = pytester.parseconfig()
|
2017-11-04 23:17:20 +08:00
|
|
|
values = config.getini("xy")
|
|
|
|
assert len(values) == 1
|
|
|
|
assert values == ["123"]
|
2011-11-12 06:56:08 +08:00
|
|
|
config.addinivalue_line("xy", "456")
|
2017-11-04 23:17:20 +08:00
|
|
|
values = config.getini("xy")
|
|
|
|
assert len(values) == 2
|
|
|
|
assert values == ["123", "456"]
|
2011-11-12 06:56:08 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_addinivalue_line_new(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2011-11-12 06:56:08 +08:00
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addini("xy", "", type="linelist")
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
config = pytester.parseconfig()
|
2011-11-12 06:56:08 +08:00
|
|
|
assert not config.getini("xy")
|
|
|
|
config.addinivalue_line("xy", "456")
|
2017-11-04 23:17:20 +08:00
|
|
|
values = config.getini("xy")
|
|
|
|
assert len(values) == 1
|
|
|
|
assert values == ["456"]
|
2011-11-12 06:56:08 +08:00
|
|
|
config.addinivalue_line("xy", "123")
|
2017-11-04 23:17:20 +08:00
|
|
|
values = config.getini("xy")
|
|
|
|
assert len(values) == 2
|
|
|
|
assert values == ["456", "123"]
|
2011-11-12 06:56:08 +08:00
|
|
|
|
2023-11-12 00:08:18 +08:00
|
|
|
def test_addini_default_values(self, pytester: Pytester) -> None:
|
|
|
|
"""Tests the default values for configuration based on
|
|
|
|
config type
|
|
|
|
"""
|
|
|
|
|
|
|
|
pytester.makeconftest(
|
|
|
|
"""
|
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addini("linelist1", "", type="linelist")
|
|
|
|
parser.addini("paths1", "", type="paths")
|
|
|
|
parser.addini("pathlist1", "", type="pathlist")
|
|
|
|
parser.addini("args1", "", type="args")
|
|
|
|
parser.addini("bool1", "", type="bool")
|
|
|
|
parser.addini("string1", "", type="string")
|
|
|
|
parser.addini("none_1", "", type="linelist", default=None)
|
|
|
|
parser.addini("none_2", "", default=None)
|
|
|
|
parser.addini("no_type", "")
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
|
|
|
config = pytester.parseconfig()
|
|
|
|
# default for linelist, paths, pathlist and args is []
|
|
|
|
value = config.getini("linelist1")
|
|
|
|
assert value == []
|
|
|
|
value = config.getini("paths1")
|
|
|
|
assert value == []
|
|
|
|
value = config.getini("pathlist1")
|
|
|
|
assert value == []
|
|
|
|
value = config.getini("args1")
|
|
|
|
assert value == []
|
|
|
|
# default for bool is False
|
|
|
|
value = config.getini("bool1")
|
|
|
|
assert value is False
|
|
|
|
# default for string is ""
|
|
|
|
value = config.getini("string1")
|
|
|
|
assert value == ""
|
|
|
|
# should return None if None is explicity set as default value
|
|
|
|
# irrespective of the type argument
|
|
|
|
value = config.getini("none_1")
|
|
|
|
assert value is None
|
|
|
|
value = config.getini("none_2")
|
|
|
|
assert value is None
|
|
|
|
# in case no type is provided and no default set
|
|
|
|
# treat it as string and default value will be ""
|
|
|
|
value = config.getini("no_type")
|
|
|
|
assert value == ""
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"type, expected",
|
|
|
|
[
|
|
|
|
pytest.param(None, "", id="None"),
|
|
|
|
pytest.param("string", "", id="string"),
|
|
|
|
pytest.param("paths", [], id="paths"),
|
|
|
|
pytest.param("pathlist", [], id="pathlist"),
|
|
|
|
pytest.param("args", [], id="args"),
|
|
|
|
pytest.param("linelist", [], id="linelist"),
|
|
|
|
pytest.param("bool", False, id="bool"),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_get_ini_default_for_type(self, type: Any, expected: Any) -> None:
|
|
|
|
assert get_ini_default_for_type(type) == expected
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_confcutdir_check_isdir(self, pytester: Pytester) -> None:
|
2016-11-23 19:47:36 +08:00
|
|
|
"""Give an error if --confcutdir is not a valid directory (#2078)"""
|
2020-02-20 20:29:59 +08:00
|
|
|
exp_match = r"^--confcutdir must be a directory, given: "
|
|
|
|
with pytest.raises(pytest.UsageError, match=exp_match):
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.parseconfig("--confcutdir", pytester.path.joinpath("file"))
|
2020-02-20 20:29:59 +08:00
|
|
|
with pytest.raises(pytest.UsageError, match=exp_match):
|
2021-12-27 20:23:15 +08:00
|
|
|
pytester.parseconfig("--confcutdir", pytester.path.joinpath("nonexistent"))
|
2020-12-09 04:20:02 +08:00
|
|
|
|
|
|
|
p = pytester.mkdir("dir")
|
|
|
|
config = pytester.parseconfig("--confcutdir", p)
|
|
|
|
assert config.getoption("confcutdir") == str(p)
|
2018-05-23 22:48:46 +08:00
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"names, expected",
|
|
|
|
[
|
2019-12-04 06:49:20 +08:00
|
|
|
# dist-info based distributions root are files as will be put in PYTHONPATH
|
2018-05-23 22:48:46 +08:00
|
|
|
(["bar.py"], ["bar"]),
|
2019-12-04 06:49:20 +08:00
|
|
|
(["foo/bar.py"], ["bar"]),
|
|
|
|
(["foo/bar.pyc"], []),
|
|
|
|
(["foo/__init__.py"], ["foo"]),
|
|
|
|
(["bar/__init__.py", "xz.py"], ["bar", "xz"]),
|
|
|
|
(["setup.py"], []),
|
|
|
|
# egg based distributions root contain the files from the dist root
|
|
|
|
(["src/bar/__init__.py"], ["bar"]),
|
|
|
|
(["src/bar/__init__.py", "setup.py"], ["bar"]),
|
|
|
|
(["source/python/bar/__init__.py", "setup.py"], ["bar"]),
|
2022-08-26 20:46:47 +08:00
|
|
|
# editable installation finder modules
|
|
|
|
(["__editable___xyz_finder.py"], []),
|
|
|
|
(["bar/__init__.py", "__editable___xyz_finder.py"], ["bar"]),
|
2018-05-23 22:48:46 +08:00
|
|
|
],
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_iter_rewritable_modules(self, names, expected) -> None:
|
2019-12-04 06:49:20 +08:00
|
|
|
assert list(_iter_rewritable_modules(names)) == expected
|
2017-07-29 03:03:07 +08:00
|
|
|
|
2015-09-25 05:13:36 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestConfigFromdictargs:
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_basic_behavior(self, _sys_snapshot) -> None:
|
2018-05-23 22:48:46 +08:00
|
|
|
option_dict = {"verbose": 444, "foo": "bar", "capture": "no"}
|
|
|
|
args = ["a", "b"]
|
2015-09-25 05:13:36 +08:00
|
|
|
|
|
|
|
config = Config.fromdictargs(option_dict, args)
|
|
|
|
with pytest.raises(AssertionError):
|
2018-05-23 22:48:46 +08:00
|
|
|
config.parse(["should refuse to parse again"])
|
2015-09-25 08:52:53 +08:00
|
|
|
assert config.option.verbose == 444
|
2018-05-23 22:48:46 +08:00
|
|
|
assert config.option.foo == "bar"
|
|
|
|
assert config.option.capture == "no"
|
2015-09-25 05:13:36 +08:00
|
|
|
assert config.args == args
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
def test_invocation_params_args(self, _sys_snapshot) -> None:
|
2015-09-25 05:13:36 +08:00
|
|
|
"""Show that fromdictargs can handle args in their "orig" format"""
|
2020-10-06 09:13:05 +08:00
|
|
|
option_dict: Dict[str, object] = {}
|
2018-05-23 22:48:46 +08:00
|
|
|
args = ["-vvvv", "-s", "a", "b"]
|
2015-09-25 05:13:36 +08:00
|
|
|
|
|
|
|
config = Config.fromdictargs(option_dict, args)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert config.args == ["a", "b"]
|
2019-10-20 02:54:54 +08:00
|
|
|
assert config.invocation_params.args == tuple(args)
|
2015-09-25 05:13:36 +08:00
|
|
|
assert config.option.verbose == 4
|
2018-05-23 22:48:46 +08:00
|
|
|
assert config.option.capture == "no"
|
2015-09-25 08:52:53 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_inifilename(self, tmp_path: Path) -> None:
|
|
|
|
d1 = tmp_path.joinpath("foo")
|
|
|
|
d1.mkdir()
|
|
|
|
p1 = d1.joinpath("bar.ini")
|
|
|
|
p1.touch()
|
|
|
|
p1.write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
[pytest]
|
|
|
|
name = value
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2023-06-20 19:55:39 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2015-09-25 05:13:36 +08:00
|
|
|
|
2020-12-19 20:49:24 +08:00
|
|
|
inifilename = "../../foo/bar.ini"
|
|
|
|
option_dict = {"inifilename": inifilename, "capture": "no"}
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
cwd = tmp_path.joinpath("a/b")
|
|
|
|
cwd.mkdir(parents=True)
|
|
|
|
p2 = cwd.joinpath("pytest.ini")
|
|
|
|
p2.touch()
|
|
|
|
p2.write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
[pytest]
|
|
|
|
name = wrong-value
|
|
|
|
should_not_be_set = true
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2023-06-20 19:55:39 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
with MonkeyPatch.context() as mp:
|
|
|
|
mp.chdir(cwd)
|
2015-09-25 08:52:53 +08:00
|
|
|
config = Config.fromdictargs(option_dict, ())
|
2020-12-19 20:49:24 +08:00
|
|
|
inipath = absolutepath(inifilename)
|
2015-09-25 08:52:53 +08:00
|
|
|
|
|
|
|
assert config.args == [str(cwd)]
|
2020-12-19 20:49:24 +08:00
|
|
|
assert config.option.inifilename == inifilename
|
2018-05-23 22:48:46 +08:00
|
|
|
assert config.option.capture == "no"
|
2015-09-25 05:13:36 +08:00
|
|
|
|
|
|
|
# this indicates this is the file used for getting configuration values
|
2020-12-19 20:49:24 +08:00
|
|
|
assert config.inipath == inipath
|
2018-05-23 22:48:46 +08:00
|
|
|
assert config.inicfg.get("name") == "value"
|
|
|
|
assert config.inicfg.get("should_not_be_set") is None
|
2015-09-25 05:13:36 +08:00
|
|
|
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_options_on_small_file_do_not_blow_up(pytester: Pytester) -> None:
|
2020-05-01 19:40:17 +08:00
|
|
|
def runfiletest(opts: Sequence[str]) -> None:
|
2020-12-09 04:20:02 +08:00
|
|
|
reprec = pytester.inline_run(*opts)
|
2009-05-21 20:33:09 +08:00
|
|
|
passed, skipped, failed = reprec.countoutcomes()
|
2010-07-27 03:15:15 +08:00
|
|
|
assert failed == 2
|
2009-02-28 05:32:49 +08:00
|
|
|
assert skipped == passed == 0
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
path = str(
|
|
|
|
pytester.makepyfile(
|
|
|
|
"""
|
2009-02-28 05:32:49 +08:00
|
|
|
def test_f1(): assert 0
|
|
|
|
def test_f2(): assert 0
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2020-12-09 04:20:02 +08:00
|
|
|
)
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2009-02-28 05:32:49 +08:00
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
runfiletest([path])
|
|
|
|
runfiletest(["-l", path])
|
|
|
|
runfiletest(["-s", path])
|
|
|
|
runfiletest(["--tb=no", path])
|
|
|
|
runfiletest(["--tb=short", path])
|
|
|
|
runfiletest(["--tb=long", path])
|
|
|
|
runfiletest(["--fulltrace", path])
|
|
|
|
runfiletest(["--traceconfig", path])
|
|
|
|
runfiletest(["-v", path])
|
|
|
|
runfiletest(["-v", "-v", path])
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_preparse_ordering_with_setuptools(
|
|
|
|
pytester: Pytester, monkeypatch: MonkeyPatch
|
|
|
|
) -> None:
|
2018-12-09 18:53:41 +08:00
|
|
|
monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", raising=False)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class EntryPoint:
|
2019-04-07 08:32:47 +08:00
|
|
|
name = "mytestplugin"
|
|
|
|
group = "pytest11"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2019-04-07 08:32:47 +08:00
|
|
|
def load(self):
|
2019-06-03 06:32:00 +08:00
|
|
|
class PseudoPlugin:
|
2019-04-07 08:32:47 +08:00
|
|
|
x = 42
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2019-04-07 08:32:47 +08:00
|
|
|
return PseudoPlugin()
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class Dist:
|
2019-04-07 08:32:47 +08:00
|
|
|
files = ()
|
2020-06-10 12:44:22 +08:00
|
|
|
metadata = {"name": "foo"}
|
2019-04-07 08:32:47 +08:00
|
|
|
entry_points = (EntryPoint(),)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2019-04-07 08:32:47 +08:00
|
|
|
def my_dists():
|
|
|
|
return (Dist,)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2023-07-01 05:55:42 +08:00
|
|
|
monkeypatch.setattr(importlib.metadata, "distributions", my_dists)
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makeconftest(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2010-01-03 00:17:13 +08:00
|
|
|
pytest_plugins = "mytestplugin",
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2010-01-03 00:17:13 +08:00
|
|
|
monkeypatch.setenv("PYTEST_PLUGINS", "mytestplugin")
|
2020-12-09 04:20:02 +08:00
|
|
|
config = pytester.parseconfig()
|
2010-01-03 00:17:13 +08:00
|
|
|
plugin = config.pluginmanager.getplugin("mytestplugin")
|
|
|
|
assert plugin.x == 42
|
2010-11-01 00:41:58 +08:00
|
|
|
|
2016-05-23 14:13:41 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_setuptools_importerror_issue1479(
|
|
|
|
pytester: Pytester, monkeypatch: MonkeyPatch
|
|
|
|
) -> None:
|
2018-12-09 18:53:41 +08:00
|
|
|
monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", raising=False)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class DummyEntryPoint:
|
2019-04-07 08:32:47 +08:00
|
|
|
name = "mytestplugin"
|
|
|
|
group = "pytest11"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2019-04-07 08:32:47 +08:00
|
|
|
def load(self):
|
|
|
|
raise ImportError("Don't hide me!")
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class Distribution:
|
2019-04-07 08:32:47 +08:00
|
|
|
version = "1.0"
|
|
|
|
files = ("foo.txt",)
|
2020-06-10 12:44:22 +08:00
|
|
|
metadata = {"name": "foo"}
|
2019-04-07 08:32:47 +08:00
|
|
|
entry_points = (DummyEntryPoint(),)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2019-04-07 08:32:47 +08:00
|
|
|
def distributions():
|
|
|
|
return (Distribution(),)
|
2016-05-23 14:13:41 +08:00
|
|
|
|
2023-07-01 05:55:42 +08:00
|
|
|
monkeypatch.setattr(importlib.metadata, "distributions", distributions)
|
2016-05-23 14:13:41 +08:00
|
|
|
with pytest.raises(ImportError):
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.parseconfig()
|
2016-05-23 14:13:41 +08:00
|
|
|
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_importlib_metadata_broken_distribution(
|
|
|
|
pytester: Pytester, monkeypatch: MonkeyPatch
|
|
|
|
) -> None:
|
2019-06-05 04:04:15 +08:00
|
|
|
"""Integration test for broken distributions with 'files' metadata being None (#5389)"""
|
|
|
|
monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", raising=False)
|
|
|
|
|
|
|
|
class DummyEntryPoint:
|
|
|
|
name = "mytestplugin"
|
|
|
|
group = "pytest11"
|
|
|
|
|
|
|
|
def load(self):
|
|
|
|
return object()
|
|
|
|
|
|
|
|
class Distribution:
|
|
|
|
version = "1.0"
|
|
|
|
files = None
|
2020-06-10 12:44:22 +08:00
|
|
|
metadata = {"name": "foo"}
|
2019-06-05 04:04:15 +08:00
|
|
|
entry_points = (DummyEntryPoint(),)
|
|
|
|
|
|
|
|
def distributions():
|
|
|
|
return (Distribution(),)
|
|
|
|
|
2023-07-01 05:55:42 +08:00
|
|
|
monkeypatch.setattr(importlib.metadata, "distributions", distributions)
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.parseconfig()
|
2019-06-05 04:04:15 +08:00
|
|
|
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.parametrize("block_it", [True, False])
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_plugin_preparse_prevents_setuptools_loading(
|
|
|
|
pytester: Pytester, monkeypatch: MonkeyPatch, block_it: bool
|
|
|
|
) -> None:
|
2018-12-09 18:53:41 +08:00
|
|
|
monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", raising=False)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2017-11-29 07:29:58 +08:00
|
|
|
plugin_module_placeholder = object()
|
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class DummyEntryPoint:
|
2019-04-07 08:32:47 +08:00
|
|
|
name = "mytestplugin"
|
|
|
|
group = "pytest11"
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2019-04-07 08:32:47 +08:00
|
|
|
def load(self):
|
|
|
|
return plugin_module_placeholder
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class Distribution:
|
2019-04-07 08:32:47 +08:00
|
|
|
version = "1.0"
|
|
|
|
files = ("foo.txt",)
|
2020-06-10 12:44:22 +08:00
|
|
|
metadata = {"name": "foo"}
|
2019-04-07 08:32:47 +08:00
|
|
|
entry_points = (DummyEntryPoint(),)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2019-04-07 08:32:47 +08:00
|
|
|
def distributions():
|
|
|
|
return (Distribution(),)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2023-07-01 05:55:42 +08:00
|
|
|
monkeypatch.setattr(importlib.metadata, "distributions", distributions)
|
2017-11-29 07:29:58 +08:00
|
|
|
args = ("-p", "no:mytestplugin") if block_it else ()
|
2020-12-09 04:20:02 +08:00
|
|
|
config = pytester.parseconfig(*args)
|
2017-11-29 00:29:52 +08:00
|
|
|
config.pluginmanager.import_plugin("mytestplugin")
|
2017-11-29 07:29:58 +08:00
|
|
|
if block_it:
|
|
|
|
assert "mytestplugin" not in sys.modules
|
2018-05-23 22:48:46 +08:00
|
|
|
assert config.pluginmanager.get_plugin("mytestplugin") is None
|
2017-11-29 07:29:58 +08:00
|
|
|
else:
|
2018-06-26 21:35:27 +08:00
|
|
|
assert (
|
|
|
|
config.pluginmanager.get_plugin("mytestplugin") is plugin_module_placeholder
|
|
|
|
)
|
2010-12-07 19:14:12 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2018-08-07 04:17:24 +08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"parse_args,should_load", [(("-p", "mytestplugin"), True), ((), False)]
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_disable_plugin_autoload(
|
|
|
|
pytester: Pytester,
|
|
|
|
monkeypatch: MonkeyPatch,
|
|
|
|
parse_args: Union[Tuple[str, str], Tuple[()]],
|
|
|
|
should_load: bool,
|
|
|
|
) -> None:
|
2019-06-03 06:32:00 +08:00
|
|
|
class DummyEntryPoint:
|
2019-04-07 08:32:47 +08:00
|
|
|
project_name = name = "mytestplugin"
|
|
|
|
group = "pytest11"
|
2019-02-08 06:59:10 +08:00
|
|
|
version = "1.0"
|
|
|
|
|
|
|
|
def load(self):
|
|
|
|
return sys.modules[self.name]
|
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class Distribution:
|
2020-06-10 12:44:22 +08:00
|
|
|
metadata = {"name": "foo"}
|
2019-04-07 08:32:47 +08:00
|
|
|
entry_points = (DummyEntryPoint(),)
|
|
|
|
files = ()
|
2018-08-07 04:17:24 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class PseudoPlugin:
|
2018-08-07 04:17:24 +08:00
|
|
|
x = 42
|
|
|
|
|
2020-02-03 21:10:54 +08:00
|
|
|
attrs_used = []
|
|
|
|
|
|
|
|
def __getattr__(self, name):
|
|
|
|
assert name == "__loader__"
|
|
|
|
self.attrs_used.append(name)
|
|
|
|
return object()
|
|
|
|
|
2019-04-07 08:32:47 +08:00
|
|
|
def distributions():
|
|
|
|
return (Distribution(),)
|
|
|
|
|
2018-08-07 04:17:24 +08:00
|
|
|
monkeypatch.setenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", "1")
|
2023-07-01 05:55:42 +08:00
|
|
|
monkeypatch.setattr(importlib.metadata, "distributions", distributions)
|
2020-12-09 04:20:02 +08:00
|
|
|
monkeypatch.setitem(sys.modules, "mytestplugin", PseudoPlugin()) # type: ignore[misc]
|
|
|
|
config = pytester.parseconfig(*parse_args)
|
2018-08-07 04:17:24 +08:00
|
|
|
has_loaded = config.pluginmanager.get_plugin("mytestplugin") is not None
|
|
|
|
assert has_loaded == should_load
|
2020-02-03 21:10:54 +08:00
|
|
|
if should_load:
|
|
|
|
assert PseudoPlugin.attrs_used == ["__loader__"]
|
|
|
|
else:
|
|
|
|
assert PseudoPlugin.attrs_used == []
|
2018-08-07 04:17:24 +08:00
|
|
|
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_plugin_loading_order(pytester: Pytester) -> None:
|
2020-01-22 20:04:30 +08:00
|
|
|
"""Test order of plugin loading with `-p`."""
|
2020-12-09 04:20:02 +08:00
|
|
|
p1 = pytester.makepyfile(
|
2020-01-22 20:04:30 +08:00
|
|
|
"""
|
|
|
|
def test_terminal_plugin(request):
|
|
|
|
import myplugin
|
2020-02-03 21:10:54 +08:00
|
|
|
assert myplugin.terminal_plugin == [False, True]
|
2020-01-22 20:04:30 +08:00
|
|
|
""",
|
|
|
|
**{
|
|
|
|
"myplugin": """
|
|
|
|
terminal_plugin = []
|
|
|
|
|
|
|
|
def pytest_configure(config):
|
|
|
|
terminal_plugin.append(bool(config.pluginmanager.get_plugin("terminalreporter")))
|
|
|
|
|
|
|
|
def pytest_sessionstart(session):
|
|
|
|
config = session.config
|
|
|
|
terminal_plugin.append(bool(config.pluginmanager.get_plugin("terminalreporter")))
|
|
|
|
"""
|
2020-03-07 08:40:48 +08:00
|
|
|
},
|
2020-01-22 20:04:30 +08:00
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.syspathinsert()
|
|
|
|
result = pytester.runpytest("-p", "myplugin", str(p1))
|
2020-01-22 20:04:30 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_cmdline_processargs_simple(pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2010-12-07 19:34:18 +08:00
|
|
|
def pytest_cmdline_preparse(args):
|
2010-12-07 19:14:12 +08:00
|
|
|
args.append("-h")
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2023-06-23 19:36:48 +08:00
|
|
|
result = pytester.runpytest("-Wignore::pytest.PytestRemovedIn8Warning")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*pytest*", "*-h*"])
|
2012-11-06 21:09:12 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_invalid_options_show_extra_information(pytester: Pytester) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""Display extra information when pytest exits due to unrecognized
|
|
|
|
options in the command-line."""
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makeini(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2015-07-09 08:22:08 +08:00
|
|
|
[pytest]
|
|
|
|
addopts = --invalid-option
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stderr.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*error: unrecognized arguments: --invalid-option*",
|
2020-12-09 04:20:02 +08:00
|
|
|
"* inifile: %s*" % pytester.path.joinpath("tox.ini"),
|
|
|
|
"* rootdir: %s*" % pytester.path,
|
2018-05-23 22:48:46 +08:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"args",
|
|
|
|
[
|
|
|
|
["dir1", "dir2", "-v"],
|
|
|
|
["dir1", "-v", "dir2"],
|
|
|
|
["dir2", "-v", "dir1"],
|
|
|
|
["-v", "dir2", "dir1"],
|
|
|
|
],
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_consider_args_after_options_for_rootdir(
|
|
|
|
pytester: Pytester, args: List[str]
|
|
|
|
) -> None:
|
2015-08-26 09:08:05 +08:00
|
|
|
"""
|
2019-03-02 22:31:09 +08:00
|
|
|
Consider all arguments in the command-line for rootdir
|
2015-08-26 09:08:05 +08:00
|
|
|
discovery, even if they happen to occur after an option. #949
|
|
|
|
"""
|
|
|
|
# replace "dir1" and "dir2" from "args" into their real directory
|
2020-12-09 04:20:02 +08:00
|
|
|
root = pytester.mkdir("myroot")
|
|
|
|
d1 = root.joinpath("dir1")
|
|
|
|
d1.mkdir()
|
|
|
|
d2 = root.joinpath("dir2")
|
|
|
|
d2.mkdir()
|
2015-08-26 09:08:05 +08:00
|
|
|
for i, arg in enumerate(args):
|
2018-05-23 22:48:46 +08:00
|
|
|
if arg == "dir1":
|
2020-12-09 04:20:02 +08:00
|
|
|
args[i] = str(d1)
|
2018-05-23 22:48:46 +08:00
|
|
|
elif arg == "dir2":
|
2020-12-09 04:20:02 +08:00
|
|
|
args[i] = str(d2)
|
|
|
|
with MonkeyPatch.context() as mp:
|
|
|
|
mp.chdir(root)
|
|
|
|
result = pytester.runpytest(*args)
|
2019-03-02 22:31:09 +08:00
|
|
|
result.stdout.fnmatch_lines(["*rootdir: *myroot"])
|
2015-08-26 09:08:05 +08:00
|
|
|
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_toolongargs_issue224(pytester: Pytester) -> None:
|
|
|
|
result = pytester.runpytest("-m", "hello" * 500)
|
2019-06-07 18:58:51 +08:00
|
|
|
assert result.ret == ExitCode.NO_TESTS_COLLECTED
|
2013-09-30 19:14:14 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_config_in_subdirectory_colon_command_line_issue2148(
|
|
|
|
pytester: Pytester,
|
|
|
|
) -> None:
|
2018-05-23 22:48:46 +08:00
|
|
|
conftest_source = """
|
2016-12-20 18:22:18 +08:00
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addini('foo', 'foo')
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2016-12-20 18:22:18 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makefile(
|
2018-05-23 22:48:46 +08:00
|
|
|
".ini",
|
2020-03-07 08:40:48 +08:00
|
|
|
**{"pytest": "[pytest]\nfoo = root", "subdir/pytest": "[pytest]\nfoo = subdir"},
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2016-12-20 18:22:18 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
**{
|
|
|
|
"conftest": conftest_source,
|
|
|
|
"subdir/conftest": conftest_source,
|
2019-06-28 01:24:29 +08:00
|
|
|
"subdir/test_foo": """\
|
2016-12-20 18:22:18 +08:00
|
|
|
def test_foo(pytestconfig):
|
|
|
|
assert pytestconfig.getini('foo') == 'subdir'
|
2019-06-28 01:24:29 +08:00
|
|
|
""",
|
2018-05-23 22:48:46 +08:00
|
|
|
}
|
|
|
|
)
|
2016-12-20 18:22:18 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest("subdir/test_foo.py::test_foo")
|
2016-12-20 18:22:18 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_notify_exception(pytester: Pytester, capfd) -> None:
|
|
|
|
config = pytester.parseconfig()
|
2018-11-23 02:05:10 +08:00
|
|
|
with pytest.raises(ValueError) as excinfo:
|
|
|
|
raise ValueError(1)
|
2019-04-18 02:19:19 +08:00
|
|
|
config.notify_exception(excinfo, config.option)
|
2020-01-17 02:26:09 +08:00
|
|
|
_, err = capfd.readouterr()
|
2013-09-30 19:14:14 +08:00
|
|
|
assert "ValueError" in err
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class A:
|
2020-01-17 02:26:09 +08:00
|
|
|
def pytest_internalerror(self):
|
2013-09-30 19:14:14 +08:00
|
|
|
return True
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2013-09-30 19:14:14 +08:00
|
|
|
config.pluginmanager.register(A())
|
2019-04-18 02:19:19 +08:00
|
|
|
config.notify_exception(excinfo, config.option)
|
2020-01-17 02:26:09 +08:00
|
|
|
_, err = capfd.readouterr()
|
2013-09-30 19:14:14 +08:00
|
|
|
assert not err
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
config = pytester.parseconfig("-p", "no:terminal")
|
2019-04-18 02:19:19 +08:00
|
|
|
with pytest.raises(ValueError) as excinfo:
|
|
|
|
raise ValueError(1)
|
|
|
|
config.notify_exception(excinfo, config.option)
|
2020-01-17 02:26:09 +08:00
|
|
|
_, err = capfd.readouterr()
|
2019-04-18 02:19:19 +08:00
|
|
|
assert "ValueError" in err
|
|
|
|
|
2013-09-30 19:14:16 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_no_terminal_discovery_error(pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile("raise TypeError('oops!')")
|
|
|
|
result = pytester.runpytest("-p", "no:terminal", "--collect-only")
|
2019-06-28 01:24:29 +08:00
|
|
|
assert result.ret == ExitCode.INTERRUPTED
|
|
|
|
|
|
|
|
|
2020-01-17 02:26:09 +08:00
|
|
|
def test_load_initial_conftest_last_ordering(_config_for_test):
|
2019-03-28 05:34:37 +08:00
|
|
|
pm = _config_for_test.pluginmanager
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class My:
|
2013-09-30 19:14:16 +08:00
|
|
|
def pytest_load_initial_conftests(self):
|
|
|
|
pass
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2013-09-30 19:14:16 +08:00
|
|
|
m = My()
|
|
|
|
pm.register(m)
|
2015-04-25 17:29:11 +08:00
|
|
|
hc = pm.hook.pytest_load_initial_conftests
|
2022-01-14 23:49:38 +08:00
|
|
|
hookimpls = [
|
|
|
|
(
|
|
|
|
hookimpl.function.__module__,
|
2023-06-13 03:30:06 +08:00
|
|
|
"wrapper" if (hookimpl.wrapper or hookimpl.hookwrapper) else "nonwrapper",
|
2022-01-14 23:49:38 +08:00
|
|
|
)
|
|
|
|
for hookimpl in hc.get_hookimpls()
|
2021-12-08 22:49:04 +08:00
|
|
|
]
|
2022-01-14 23:49:38 +08:00
|
|
|
assert hookimpls == [
|
|
|
|
("_pytest.config", "nonwrapper"),
|
|
|
|
(m.__module__, "nonwrapper"),
|
|
|
|
("_pytest.legacypath", "nonwrapper"),
|
2022-02-09 15:58:38 +08:00
|
|
|
("_pytest.python_path", "nonwrapper"),
|
2022-01-14 23:49:38 +08:00
|
|
|
("_pytest.capture", "wrapper"),
|
|
|
|
("_pytest.warnings", "wrapper"),
|
2021-10-05 14:36:38 +08:00
|
|
|
]
|
2016-06-20 20:45:13 +08:00
|
|
|
|
2013-09-30 19:14:16 +08:00
|
|
|
|
2020-06-23 16:38:21 +08:00
|
|
|
def test_get_plugin_specs_as_list() -> None:
|
|
|
|
def exp_match(val: object) -> str:
|
2020-02-20 20:29:59 +08:00
|
|
|
return (
|
2020-06-23 16:38:21 +08:00
|
|
|
"Plugins may be specified as a sequence or a ','-separated string of plugin names. Got: %s"
|
|
|
|
% re.escape(repr(val))
|
2020-02-20 20:29:59 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
with pytest.raises(pytest.UsageError, match=exp_match({"foo"})):
|
2020-06-23 16:38:21 +08:00
|
|
|
_get_plugin_specs_as_list({"foo"}) # type: ignore[arg-type]
|
2020-02-20 20:29:59 +08:00
|
|
|
with pytest.raises(pytest.UsageError, match=exp_match({})):
|
2020-06-23 16:38:21 +08:00
|
|
|
_get_plugin_specs_as_list(dict()) # type: ignore[arg-type]
|
2017-01-13 00:23:09 +08:00
|
|
|
|
|
|
|
assert _get_plugin_specs_as_list(None) == []
|
2018-05-23 22:48:46 +08:00
|
|
|
assert _get_plugin_specs_as_list("") == []
|
|
|
|
assert _get_plugin_specs_as_list("foo") == ["foo"]
|
|
|
|
assert _get_plugin_specs_as_list("foo,bar") == ["foo", "bar"]
|
|
|
|
assert _get_plugin_specs_as_list(["foo", "bar"]) == ["foo", "bar"]
|
|
|
|
assert _get_plugin_specs_as_list(("foo", "bar")) == ["foo", "bar"]
|
2017-01-13 00:23:09 +08:00
|
|
|
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_collect_pytest_prefix_bug_integration(pytester: Pytester) -> None:
|
2018-08-05 03:35:24 +08:00
|
|
|
"""Integration test for issue #3775"""
|
2020-12-09 04:20:02 +08:00
|
|
|
p = pytester.copy_example("config/collect_pytest_prefix")
|
|
|
|
result = pytester.runpytest(p)
|
2019-03-23 18:36:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["* 1 passed *"])
|
2018-08-05 03:35:24 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_collect_pytest_prefix_bug(pytestconfig):
|
|
|
|
"""Ensure we collect only actual functions from conftest files (#3775)"""
|
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class Dummy:
|
|
|
|
class pytest_something:
|
2018-08-05 03:35:24 +08:00
|
|
|
pass
|
|
|
|
|
|
|
|
pm = pytestconfig.pluginmanager
|
|
|
|
assert pm.parse_hookimpl_opts(Dummy(), "pytest_something") is None
|
|
|
|
|
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestRootdir:
|
2020-08-03 22:46:35 +08:00
|
|
|
def test_simple_noini(self, tmp_path: Path, monkeypatch: MonkeyPatch) -> None:
|
|
|
|
assert get_common_ancestor([tmp_path]) == tmp_path
|
|
|
|
a = tmp_path / "a"
|
|
|
|
a.mkdir()
|
|
|
|
assert get_common_ancestor([a, tmp_path]) == tmp_path
|
|
|
|
assert get_common_ancestor([tmp_path, a]) == tmp_path
|
|
|
|
monkeypatch.chdir(tmp_path)
|
|
|
|
assert get_common_ancestor([]) == tmp_path
|
|
|
|
no_path = tmp_path / "does-not-exist"
|
|
|
|
assert get_common_ancestor([no_path]) == tmp_path
|
|
|
|
assert get_common_ancestor([no_path / "a"]) == tmp_path
|
2015-02-27 04:56:44 +08:00
|
|
|
|
2020-06-08 21:03:10 +08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"name, contents",
|
|
|
|
[
|
|
|
|
pytest.param("pytest.ini", "[pytest]\nx=10", id="pytest.ini"),
|
|
|
|
pytest.param(
|
|
|
|
"pyproject.toml", "[tool.pytest.ini_options]\nx=10", id="pyproject.toml"
|
|
|
|
),
|
|
|
|
pytest.param("tox.ini", "[pytest]\nx=10", id="tox.ini"),
|
|
|
|
pytest.param("setup.cfg", "[tool:pytest]\nx=10", id="setup.cfg"),
|
|
|
|
],
|
|
|
|
)
|
2020-08-03 22:46:35 +08:00
|
|
|
def test_with_ini(self, tmp_path: Path, name: str, contents: str) -> None:
|
|
|
|
inipath = tmp_path / name
|
2023-06-20 19:55:39 +08:00
|
|
|
inipath.write_text(contents, encoding="utf-8")
|
2020-08-03 22:46:35 +08:00
|
|
|
|
|
|
|
a = tmp_path / "a"
|
|
|
|
a.mkdir()
|
|
|
|
b = a / "b"
|
|
|
|
b.mkdir()
|
|
|
|
for args in ([str(tmp_path)], [str(a)], [str(b)]):
|
|
|
|
rootpath, parsed_inipath, _ = determine_setup(None, args)
|
|
|
|
assert rootpath == tmp_path
|
|
|
|
assert parsed_inipath == inipath
|
|
|
|
rootpath, parsed_inipath, ini_config = determine_setup(None, [str(b), str(a)])
|
|
|
|
assert rootpath == tmp_path
|
|
|
|
assert parsed_inipath == inipath
|
2020-06-08 21:03:10 +08:00
|
|
|
assert ini_config == {"x": "10"}
|
2015-02-27 04:56:44 +08:00
|
|
|
|
2020-08-03 22:46:35 +08:00
|
|
|
@pytest.mark.parametrize("name", ["setup.cfg", "tox.ini"])
|
|
|
|
def test_pytestini_overrides_empty_other(self, tmp_path: Path, name: str) -> None:
|
|
|
|
inipath = tmp_path / "pytest.ini"
|
|
|
|
inipath.touch()
|
|
|
|
a = tmp_path / "a"
|
|
|
|
a.mkdir()
|
|
|
|
(a / name).touch()
|
|
|
|
rootpath, parsed_inipath, _ = determine_setup(None, [str(a)])
|
|
|
|
assert rootpath == tmp_path
|
|
|
|
assert parsed_inipath == inipath
|
|
|
|
|
|
|
|
def test_setuppy_fallback(self, tmp_path: Path) -> None:
|
|
|
|
a = tmp_path / "a"
|
|
|
|
a.mkdir()
|
|
|
|
(a / "setup.cfg").touch()
|
|
|
|
(tmp_path / "setup.py").touch()
|
|
|
|
rootpath, inipath, inicfg = determine_setup(None, [str(a)])
|
|
|
|
assert rootpath == tmp_path
|
|
|
|
assert inipath is None
|
2015-02-27 04:56:44 +08:00
|
|
|
assert inicfg == {}
|
|
|
|
|
2020-08-03 22:46:35 +08:00
|
|
|
def test_nothing(self, tmp_path: Path, monkeypatch: MonkeyPatch) -> None:
|
|
|
|
monkeypatch.chdir(tmp_path)
|
|
|
|
rootpath, inipath, inicfg = determine_setup(None, [str(tmp_path)])
|
|
|
|
assert rootpath == tmp_path
|
|
|
|
assert inipath is None
|
2015-02-27 04:56:44 +08:00
|
|
|
assert inicfg == {}
|
|
|
|
|
2020-06-08 21:03:10 +08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"name, contents",
|
|
|
|
[
|
|
|
|
# pytest.param("pytest.ini", "[pytest]\nx=10", id="pytest.ini"),
|
|
|
|
pytest.param(
|
|
|
|
"pyproject.toml", "[tool.pytest.ini_options]\nx=10", id="pyproject.toml"
|
|
|
|
),
|
|
|
|
# pytest.param("tox.ini", "[pytest]\nx=10", id="tox.ini"),
|
|
|
|
# pytest.param("setup.cfg", "[tool:pytest]\nx=10", id="setup.cfg"),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_with_specific_inifile(
|
2020-08-03 22:46:35 +08:00
|
|
|
self, tmp_path: Path, name: str, contents: str
|
2020-06-08 21:03:10 +08:00
|
|
|
) -> None:
|
2020-08-03 22:46:35 +08:00
|
|
|
p = tmp_path / name
|
|
|
|
p.touch()
|
2023-06-20 19:55:39 +08:00
|
|
|
p.write_text(contents, encoding="utf-8")
|
2020-08-03 22:46:35 +08:00
|
|
|
rootpath, inipath, ini_config = determine_setup(str(p), [str(tmp_path)])
|
|
|
|
assert rootpath == tmp_path
|
|
|
|
assert inipath == p
|
2020-06-08 21:03:10 +08:00
|
|
|
assert ini_config == {"x": "10"}
|
2016-06-25 22:12:42 +08:00
|
|
|
|
2021-04-17 01:38:35 +08:00
|
|
|
def test_explicit_config_file_sets_rootdir(
|
|
|
|
self, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
|
|
|
) -> None:
|
|
|
|
tests_dir = tmp_path / "tests"
|
|
|
|
tests_dir.mkdir()
|
|
|
|
|
|
|
|
monkeypatch.chdir(tmp_path)
|
|
|
|
|
|
|
|
# No config file is explicitly given: rootdir is determined to be cwd.
|
|
|
|
rootpath, found_inipath, *_ = determine_setup(None, [str(tests_dir)])
|
|
|
|
assert rootpath == tmp_path
|
|
|
|
assert found_inipath is None
|
|
|
|
|
|
|
|
# Config file is explicitly given: rootdir is determined to be inifile's directory.
|
|
|
|
inipath = tmp_path / "pytest.ini"
|
|
|
|
inipath.touch()
|
|
|
|
rootpath, found_inipath, *_ = determine_setup(str(inipath), [str(tests_dir)])
|
|
|
|
assert rootpath == tmp_path
|
|
|
|
assert found_inipath == inipath
|
|
|
|
|
2020-08-03 22:46:35 +08:00
|
|
|
def test_with_arg_outside_cwd_without_inifile(
|
|
|
|
self, tmp_path: Path, monkeypatch: MonkeyPatch
|
|
|
|
) -> None:
|
|
|
|
monkeypatch.chdir(tmp_path)
|
|
|
|
a = tmp_path / "a"
|
|
|
|
a.mkdir()
|
|
|
|
b = tmp_path / "b"
|
|
|
|
b.mkdir()
|
|
|
|
rootpath, inifile, _ = determine_setup(None, [str(a), str(b)])
|
|
|
|
assert rootpath == tmp_path
|
2020-02-24 00:12:55 +08:00
|
|
|
assert inifile is None
|
|
|
|
|
2020-08-03 22:46:35 +08:00
|
|
|
def test_with_arg_outside_cwd_with_inifile(self, tmp_path: Path) -> None:
|
|
|
|
a = tmp_path / "a"
|
|
|
|
a.mkdir()
|
|
|
|
b = tmp_path / "b"
|
|
|
|
b.mkdir()
|
|
|
|
inipath = a / "pytest.ini"
|
|
|
|
inipath.touch()
|
|
|
|
rootpath, parsed_inipath, _ = determine_setup(None, [str(a), str(b)])
|
|
|
|
assert rootpath == a
|
|
|
|
assert inipath == parsed_inipath
|
2020-02-24 00:12:55 +08:00
|
|
|
|
|
|
|
@pytest.mark.parametrize("dirs", ([], ["does-not-exist"], ["a/does-not-exist"]))
|
2020-08-03 22:46:35 +08:00
|
|
|
def test_with_non_dir_arg(
|
|
|
|
self, dirs: Sequence[str], tmp_path: Path, monkeypatch: MonkeyPatch
|
|
|
|
) -> None:
|
|
|
|
monkeypatch.chdir(tmp_path)
|
|
|
|
rootpath, inipath, _ = determine_setup(None, dirs)
|
|
|
|
assert rootpath == tmp_path
|
|
|
|
assert inipath is None
|
|
|
|
|
|
|
|
def test_with_existing_file_in_subdir(
|
|
|
|
self, tmp_path: Path, monkeypatch: MonkeyPatch
|
|
|
|
) -> None:
|
|
|
|
a = tmp_path / "a"
|
|
|
|
a.mkdir()
|
|
|
|
(a / "exists").touch()
|
|
|
|
monkeypatch.chdir(tmp_path)
|
|
|
|
rootpath, inipath, _ = determine_setup(None, ["a/exist"])
|
|
|
|
assert rootpath == tmp_path
|
|
|
|
assert inipath is None
|
2020-02-24 00:12:55 +08:00
|
|
|
|
2020-09-28 23:52:51 +08:00
|
|
|
def test_with_config_also_in_parent_directory(
|
|
|
|
self, tmp_path: Path, monkeypatch: MonkeyPatch
|
|
|
|
) -> None:
|
|
|
|
"""Regression test for #7807."""
|
|
|
|
(tmp_path / "setup.cfg").write_text("[tool:pytest]\n", "utf-8")
|
|
|
|
(tmp_path / "myproject").mkdir()
|
|
|
|
(tmp_path / "myproject" / "setup.cfg").write_text("[tool:pytest]\n", "utf-8")
|
|
|
|
(tmp_path / "myproject" / "tests").mkdir()
|
|
|
|
monkeypatch.chdir(tmp_path / "myproject")
|
|
|
|
|
|
|
|
rootpath, inipath, _ = determine_setup(None, ["tests/"])
|
|
|
|
|
|
|
|
assert rootpath == tmp_path / "myproject"
|
|
|
|
assert inipath == tmp_path / "myproject" / "setup.cfg"
|
|
|
|
|
2016-08-18 09:39:23 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestOverrideIniArgs:
|
2016-06-25 22:12:42 +08:00
|
|
|
@pytest.mark.parametrize("name", "setup.cfg tox.ini pytest.ini".split())
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_override_ini_names(self, pytester: Pytester, name: str) -> None:
|
2018-09-02 08:58:48 +08:00
|
|
|
section = "[pytest]" if name != "setup.cfg" else "[tool:pytest]"
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.path.joinpath(name).write_text(
|
2018-05-23 22:48:46 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""
|
2018-09-02 08:58:48 +08:00
|
|
|
{section}
|
|
|
|
custom = 1.0""".format(
|
|
|
|
section=section
|
|
|
|
)
|
2023-06-20 19:55:39 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makeconftest(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2016-06-25 22:12:42 +08:00
|
|
|
def pytest_addoption(parser):
|
2018-05-23 22:48:46 +08:00
|
|
|
parser.addini("custom", "")"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2016-06-25 22:12:42 +08:00
|
|
|
def test_pass(pytestconfig):
|
|
|
|
ini_val = pytestconfig.getini("custom")
|
2018-05-23 22:48:46 +08:00
|
|
|
print('\\ncustom_option:%s\\n' % ini_val)"""
|
|
|
|
)
|
2016-06-25 22:12:42 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest("--override-ini", "custom=2.0", "-s")
|
2016-06-25 22:12:42 +08:00
|
|
|
assert result.ret == 0
|
2016-07-24 00:09:42 +08:00
|
|
|
result.stdout.fnmatch_lines(["custom_option:2.0"])
|
2016-06-25 22:12:42 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest(
|
2018-05-23 22:48:46 +08:00
|
|
|
"--override-ini", "custom=2.0", "--override-ini=custom=3.0", "-s"
|
|
|
|
)
|
2016-06-25 22:12:42 +08:00
|
|
|
assert result.ret == 0
|
2016-07-24 00:09:42 +08:00
|
|
|
result.stdout.fnmatch_lines(["custom_option:3.0"])
|
2016-06-25 22:12:42 +08:00
|
|
|
|
2021-10-16 16:21:02 +08:00
|
|
|
def test_override_ini_paths(self, pytester: Pytester) -> None:
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makeconftest(
|
2021-10-16 16:21:02 +08:00
|
|
|
"""
|
2016-07-24 00:09:42 +08:00
|
|
|
def pytest_addoption(parser):
|
2021-10-16 16:21:02 +08:00
|
|
|
parser.addini("paths", "my new ini value", type="paths")"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makeini(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2016-07-24 00:09:42 +08:00
|
|
|
[pytest]
|
2018-05-23 22:48:46 +08:00
|
|
|
paths=blah.py"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makepyfile(
|
2021-10-16 16:21:02 +08:00
|
|
|
r"""
|
2021-04-26 22:46:26 +08:00
|
|
|
def test_overriden(pytestconfig):
|
2016-07-24 00:09:42 +08:00
|
|
|
config_paths = pytestconfig.getini("paths")
|
|
|
|
print(config_paths)
|
|
|
|
for cpf in config_paths:
|
2021-10-16 16:21:02 +08:00
|
|
|
print('\nuser_path:%s' % cpf.name)
|
2021-04-26 22:46:26 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest(
|
2018-05-23 22:48:46 +08:00
|
|
|
"--override-ini", "paths=foo/bar1.py foo/bar2.py", "-s"
|
|
|
|
)
|
|
|
|
result.stdout.fnmatch_lines(["user_path:bar1.py", "user_path:bar2.py"])
|
2016-06-25 22:12:42 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_override_multiple_and_default(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2016-07-24 00:09:42 +08:00
|
|
|
def pytest_addoption(parser):
|
|
|
|
addini = parser.addini
|
|
|
|
addini("custom_option_1", "", default="o1")
|
|
|
|
addini("custom_option_2", "", default="o2")
|
|
|
|
addini("custom_option_3", "", default=False, type="bool")
|
2018-05-23 22:48:46 +08:00
|
|
|
addini("custom_option_4", "", default=True, type="bool")"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makeini(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2016-07-24 00:09:42 +08:00
|
|
|
[pytest]
|
|
|
|
custom_option_1=custom_option_1
|
2018-01-24 07:13:20 +08:00
|
|
|
custom_option_2=custom_option_2
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2016-07-24 00:09:42 +08:00
|
|
|
def test_multiple_options(pytestconfig):
|
|
|
|
prefix = "custom_option"
|
|
|
|
for x in range(1, 5):
|
|
|
|
ini_value=pytestconfig.getini("%s_%d" % (prefix, x))
|
2018-01-24 07:13:20 +08:00
|
|
|
print('\\nini%d:%s' % (x, ini_value))
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest(
|
2018-05-23 22:48:46 +08:00
|
|
|
"--override-ini",
|
|
|
|
"custom_option_1=fulldir=/tmp/user1",
|
|
|
|
"-o",
|
|
|
|
"custom_option_2=url=/tmp/user2?a=b&d=e",
|
|
|
|
"-o",
|
|
|
|
"custom_option_3=True",
|
|
|
|
"-o",
|
|
|
|
"custom_option_4=no",
|
|
|
|
"-s",
|
|
|
|
)
|
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"ini1:fulldir=/tmp/user1",
|
|
|
|
"ini2:url=/tmp/user2?a=b&d=e",
|
|
|
|
"ini3:True",
|
|
|
|
"ini4:False",
|
|
|
|
]
|
|
|
|
)
|
2016-08-07 04:58:17 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_override_ini_usage_error_bad_style(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeini(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2016-12-01 20:20:42 +08:00
|
|
|
[pytest]
|
|
|
|
xdist_strict=False
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest("--override-ini", "xdist_strict", "True")
|
2020-02-29 02:41:56 +08:00
|
|
|
result.stderr.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"ERROR: -o/--override-ini expects option=value style (got: 'xdist_strict').",
|
|
|
|
]
|
|
|
|
)
|
2016-12-01 20:20:42 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.parametrize("with_ini", [True, False])
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_override_ini_handled_asap(
|
|
|
|
self, pytester: Pytester, with_ini: bool
|
|
|
|
) -> None:
|
2017-02-09 08:39:34 +08:00
|
|
|
"""-o should be handled as soon as possible and always override what's in ini files (#2238)"""
|
|
|
|
if with_ini:
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makeini(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2017-02-09 08:39:34 +08:00
|
|
|
[pytest]
|
|
|
|
python_files=test_*.py
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
unittest_ini_handle="""
|
2017-02-09 08:39:34 +08:00
|
|
|
def test():
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest("--override-ini", "python_files=unittest_*.py")
|
2017-02-09 08:39:34 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed in*"])
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_addopts_before_initini(
|
|
|
|
self, monkeypatch: MonkeyPatch, _config_for_test, _sys_snapshot
|
|
|
|
) -> None:
|
2018-05-23 22:48:46 +08:00
|
|
|
cache_dir = ".custom_cache"
|
|
|
|
monkeypatch.setenv("PYTEST_ADDOPTS", "-o cache_dir=%s" % cache_dir)
|
2019-03-28 05:34:37 +08:00
|
|
|
config = _config_for_test
|
2017-10-11 01:03:15 +08:00
|
|
|
config._preparse([], addopts=True)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert config._override_ini == ["cache_dir=%s" % cache_dir]
|
2018-01-21 02:23:08 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_addopts_from_env_not_concatenated(
|
|
|
|
self, monkeypatch: MonkeyPatch, _config_for_test
|
|
|
|
) -> None:
|
2018-12-09 21:44:45 +08:00
|
|
|
"""PYTEST_ADDOPTS should not take values from normal args (#4265)."""
|
|
|
|
monkeypatch.setenv("PYTEST_ADDOPTS", "-o")
|
2019-03-28 05:34:37 +08:00
|
|
|
config = _config_for_test
|
2019-01-18 05:06:45 +08:00
|
|
|
with pytest.raises(UsageError) as excinfo:
|
2018-12-09 21:44:45 +08:00
|
|
|
config._preparse(["cache_dir=ignored"], addopts=True)
|
2019-01-18 05:06:45 +08:00
|
|
|
assert (
|
|
|
|
"error: argument -o/--override-ini: expected one argument (via PYTEST_ADDOPTS)"
|
|
|
|
in excinfo.value.args[0]
|
|
|
|
)
|
2018-12-09 21:44:45 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_addopts_from_ini_not_concatenated(self, pytester: Pytester) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""`addopts` from ini should not take values from normal args (#4265)."""
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makeini(
|
2018-12-09 21:44:45 +08:00
|
|
|
"""
|
|
|
|
[pytest]
|
|
|
|
addopts=-o
|
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest("cache_dir=ignored")
|
2018-12-09 21:44:45 +08:00
|
|
|
result.stderr.fnmatch_lines(
|
|
|
|
[
|
2019-01-18 05:06:45 +08:00
|
|
|
"%s: error: argument -o/--override-ini: expected one argument (via addopts config)"
|
2020-12-09 04:20:02 +08:00
|
|
|
% (pytester._request.config._parser.optparser.prog,)
|
2018-12-09 21:44:45 +08:00
|
|
|
]
|
|
|
|
)
|
2020-02-11 05:43:30 +08:00
|
|
|
assert result.ret == _pytest.config.ExitCode.USAGE_ERROR
|
2018-12-09 21:44:45 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_override_ini_does_not_contain_paths(
|
|
|
|
self, _config_for_test, _sys_snapshot
|
|
|
|
) -> None:
|
2018-01-24 07:13:20 +08:00
|
|
|
"""Check that -o no longer swallows all options after it (#3103)"""
|
2019-03-28 05:34:37 +08:00
|
|
|
config = _config_for_test
|
2018-05-23 22:48:46 +08:00
|
|
|
config._preparse(["-o", "cache_dir=/cache", "/some/test/path"])
|
|
|
|
assert config._override_ini == ["cache_dir=/cache"]
|
2018-01-24 07:13:20 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_multiple_override_ini_options(self, pytester: Pytester) -> None:
|
2018-01-23 03:58:04 +08:00
|
|
|
"""Ensure a file path following a '-o' option does not generate an error (#3103)"""
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
**{
|
|
|
|
"conftest.py": """
|
2018-01-24 07:13:20 +08:00
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addini('foo', default=None, help='some option')
|
|
|
|
parser.addini('bar', default=None, help='some option')
|
|
|
|
""",
|
2018-05-23 22:48:46 +08:00
|
|
|
"test_foo.py": """
|
2018-01-24 07:13:20 +08:00
|
|
|
def test(pytestconfig):
|
|
|
|
assert pytestconfig.getini('foo') == '1'
|
|
|
|
assert pytestconfig.getini('bar') == '0'
|
|
|
|
""",
|
2018-05-23 22:48:46 +08:00
|
|
|
"test_bar.py": """
|
2018-01-24 07:13:20 +08:00
|
|
|
def test():
|
|
|
|
assert False
|
|
|
|
""",
|
2018-05-23 22:48:46 +08:00
|
|
|
}
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest("-o", "foo=1", "-o", "bar=0", "test_foo.py")
|
2018-05-23 22:48:46 +08:00
|
|
|
assert "ERROR:" not in result.stderr.str()
|
|
|
|
result.stdout.fnmatch_lines(["collected 1 item", "*= 1 passed in *="])
|
2019-01-18 05:06:45 +08:00
|
|
|
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_help_via_addopts(pytester: Pytester) -> None:
|
|
|
|
pytester.makeini(
|
2019-01-18 05:06:45 +08:00
|
|
|
"""
|
|
|
|
[pytest]
|
|
|
|
addopts = --unknown-option-should-allow-for-help --help
|
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest()
|
2019-01-18 05:06:45 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"usage: *",
|
|
|
|
"positional arguments:",
|
|
|
|
# Displays full/default help.
|
|
|
|
"to see available markers type: pytest --markers",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_help_and_version_after_argument_error(pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2019-01-18 05:06:45 +08:00
|
|
|
"""
|
|
|
|
def validate(arg):
|
|
|
|
raise argparse.ArgumentTypeError("argerror")
|
|
|
|
|
|
|
|
def pytest_addoption(parser):
|
|
|
|
group = parser.getgroup('cov')
|
|
|
|
group.addoption(
|
|
|
|
"--invalid-option-should-allow-for-help",
|
|
|
|
type=validate,
|
|
|
|
)
|
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makeini(
|
2019-01-18 05:06:45 +08:00
|
|
|
"""
|
|
|
|
[pytest]
|
|
|
|
addopts = --invalid-option-should-allow-for-help
|
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest("--help")
|
2019-01-18 05:06:45 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"usage: *",
|
|
|
|
"positional arguments:",
|
|
|
|
"NOTE: displaying only minimal help due to UsageError.",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
result.stderr.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"ERROR: usage: *",
|
|
|
|
"%s: error: argument --invalid-option-should-allow-for-help: expected one argument"
|
2020-12-09 04:20:02 +08:00
|
|
|
% (pytester._request.config._parser.optparser.prog,),
|
2019-01-18 05:06:45 +08:00
|
|
|
]
|
|
|
|
)
|
|
|
|
# Does not display full/default help.
|
|
|
|
assert "to see available markers type: pytest --markers" not in result.stdout.lines
|
2019-06-07 18:58:51 +08:00
|
|
|
assert result.ret == ExitCode.USAGE_ERROR
|
2019-01-18 05:06:45 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest("--version")
|
2021-01-17 19:43:36 +08:00
|
|
|
result.stdout.fnmatch_lines([f"pytest {pytest.__version__}"])
|
2019-06-07 18:58:51 +08:00
|
|
|
assert result.ret == ExitCode.USAGE_ERROR
|
2019-03-20 09:25:35 +08:00
|
|
|
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_help_formatter_uses_py_get_terminal_width(monkeypatch: MonkeyPatch) -> None:
|
2019-04-05 21:16:35 +08:00
|
|
|
from _pytest.config.argparsing import DropShorterLongHelpFormatter
|
|
|
|
|
|
|
|
monkeypatch.setenv("COLUMNS", "90")
|
|
|
|
formatter = DropShorterLongHelpFormatter("prog")
|
|
|
|
assert formatter._width == 90
|
|
|
|
|
2020-04-30 19:20:48 +08:00
|
|
|
monkeypatch.setattr("_pytest._io.get_terminal_width", lambda: 160)
|
2019-04-05 21:16:35 +08:00
|
|
|
formatter = DropShorterLongHelpFormatter("prog")
|
|
|
|
assert formatter._width == 160
|
|
|
|
|
|
|
|
formatter = DropShorterLongHelpFormatter("prog", width=42)
|
|
|
|
assert formatter._width == 42
|
|
|
|
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_config_does_not_load_blocked_plugin_from_args(pytester: Pytester) -> None:
|
2019-03-20 09:25:35 +08:00
|
|
|
"""This tests that pytest's config setup handles "-p no:X"."""
|
2020-12-09 04:20:02 +08:00
|
|
|
p = pytester.makepyfile("def test(capfd): pass")
|
|
|
|
result = pytester.runpytest(str(p), "-pno:capture")
|
2019-03-20 09:25:35 +08:00
|
|
|
result.stdout.fnmatch_lines(["E fixture 'capfd' not found"])
|
2019-06-07 18:58:51 +08:00
|
|
|
assert result.ret == ExitCode.TESTS_FAILED
|
2019-03-20 09:25:35 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest(str(p), "-pno:capture", "-s")
|
2019-03-20 09:25:35 +08:00
|
|
|
result.stderr.fnmatch_lines(["*: error: unrecognized arguments: -s"])
|
2019-06-07 18:58:51 +08:00
|
|
|
assert result.ret == ExitCode.USAGE_ERROR
|
2019-03-21 11:50:51 +08:00
|
|
|
|
2023-01-21 19:22:44 +08:00
|
|
|
result = pytester.runpytest(str(p), "-p no:capture", "-s")
|
|
|
|
result.stderr.fnmatch_lines(["*: error: unrecognized arguments: -s"])
|
|
|
|
assert result.ret == ExitCode.USAGE_ERROR
|
|
|
|
|
2019-03-21 11:50:51 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_invocation_args(pytester: Pytester) -> None:
|
2019-07-06 06:38:16 +08:00
|
|
|
"""Ensure that Config.invocation_* arguments are correctly defined"""
|
|
|
|
|
|
|
|
class DummyPlugin:
|
|
|
|
pass
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
p = pytester.makepyfile("def test(): pass")
|
2019-07-06 06:38:16 +08:00
|
|
|
plugin = DummyPlugin()
|
2020-12-09 04:20:02 +08:00
|
|
|
rec = pytester.inline_run(p, "-v", plugins=[plugin])
|
2019-07-06 06:38:16 +08:00
|
|
|
calls = rec.getcalls("pytest_runtest_protocol")
|
|
|
|
assert len(calls) == 1
|
|
|
|
call = calls[0]
|
|
|
|
config = call.item.config
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
assert config.invocation_params.args == (str(p), "-v")
|
|
|
|
assert config.invocation_params.dir == pytester.path
|
2019-07-06 06:38:16 +08:00
|
|
|
|
2019-07-06 23:22:19 +08:00
|
|
|
plugins = config.invocation_params.plugins
|
2019-07-06 06:38:16 +08:00
|
|
|
assert len(plugins) == 2
|
|
|
|
assert plugins[0] is plugin
|
2020-12-09 04:20:02 +08:00
|
|
|
assert type(plugins[1]).__name__ == "Collect" # installed by pytester.inline_run()
|
2019-07-06 06:38:16 +08:00
|
|
|
|
2019-10-20 02:54:54 +08:00
|
|
|
# args cannot be None
|
|
|
|
with pytest.raises(TypeError):
|
2020-07-10 14:44:14 +08:00
|
|
|
Config.InvocationParams(args=None, plugins=None, dir=Path()) # type: ignore[arg-type]
|
2019-10-20 02:54:54 +08:00
|
|
|
|
2019-07-06 06:38:16 +08:00
|
|
|
|
2019-03-21 11:50:51 +08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"plugin",
|
|
|
|
[
|
|
|
|
x
|
|
|
|
for x in _pytest.config.default_plugins
|
2019-03-28 02:10:33 +08:00
|
|
|
if x not in _pytest.config.essential_plugins
|
2019-03-21 11:50:51 +08:00
|
|
|
],
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_config_blocked_default_plugins(pytester: Pytester, plugin: str) -> None:
|
|
|
|
p = pytester.makepyfile("def test(): pass")
|
|
|
|
result = pytester.runpytest(str(p), "-pno:%s" % plugin)
|
2019-05-16 22:30:40 +08:00
|
|
|
|
|
|
|
if plugin == "python":
|
2019-06-07 18:58:51 +08:00
|
|
|
assert result.ret == ExitCode.USAGE_ERROR
|
2019-05-16 22:30:40 +08:00
|
|
|
result.stderr.fnmatch_lines(
|
|
|
|
[
|
2022-03-29 18:34:15 +08:00
|
|
|
"ERROR: found no collectors for */test_config_blocked_default_plugins.py",
|
2019-05-16 22:30:40 +08:00
|
|
|
]
|
|
|
|
)
|
|
|
|
return
|
|
|
|
|
2019-06-07 18:58:51 +08:00
|
|
|
assert result.ret == ExitCode.OK
|
2019-03-28 02:10:33 +08:00
|
|
|
if plugin != "terminal":
|
|
|
|
result.stdout.fnmatch_lines(["* 1 passed in *"])
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
p = pytester.makepyfile("def test(): assert 0")
|
|
|
|
result = pytester.runpytest(str(p), "-pno:%s" % plugin)
|
2019-06-07 18:58:51 +08:00
|
|
|
assert result.ret == ExitCode.TESTS_FAILED
|
2019-04-27 09:39:00 +08:00
|
|
|
if plugin != "terminal":
|
2019-03-28 02:10:33 +08:00
|
|
|
result.stdout.fnmatch_lines(["* 1 failed in *"])
|
2019-04-27 09:39:00 +08:00
|
|
|
else:
|
2019-10-23 06:00:15 +08:00
|
|
|
assert result.stdout.lines == []
|
2019-06-30 23:15:29 +08:00
|
|
|
|
|
|
|
|
2019-06-30 23:18:14 +08:00
|
|
|
class TestSetupCfg:
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_pytest_setup_cfg_unsupported(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makefile(
|
2019-06-30 23:18:14 +08:00
|
|
|
".cfg",
|
|
|
|
setup="""
|
|
|
|
[pytest]
|
|
|
|
addopts = --verbose
|
|
|
|
""",
|
|
|
|
)
|
|
|
|
with pytest.raises(pytest.fail.Exception):
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.runpytest()
|
2019-06-30 23:18:14 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_pytest_custom_cfg_unsupported(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makefile(
|
2019-06-30 23:18:14 +08:00
|
|
|
".cfg",
|
|
|
|
custom="""
|
|
|
|
[pytest]
|
|
|
|
addopts = --verbose
|
|
|
|
""",
|
|
|
|
)
|
|
|
|
with pytest.raises(pytest.fail.Exception):
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.runpytest("-c", "custom.cfg")
|
2019-06-30 23:18:14 +08:00
|
|
|
|
2023-05-26 18:56:18 +08:00
|
|
|
with pytest.raises(pytest.fail.Exception):
|
|
|
|
pytester.runpytest("--config-file", "custom.cfg")
|
|
|
|
|
2019-06-30 23:18:14 +08:00
|
|
|
|
2019-06-30 23:15:29 +08:00
|
|
|
class TestPytestPluginsVariable:
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_pytest_plugins_in_non_top_level_conftest_unsupported(
|
|
|
|
self, pytester: Pytester
|
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2019-06-30 23:15:29 +08:00
|
|
|
**{
|
|
|
|
"subdirectory/conftest.py": """
|
|
|
|
pytest_plugins=['capture']
|
|
|
|
"""
|
|
|
|
}
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makepyfile(
|
2019-06-30 23:15:29 +08:00
|
|
|
"""
|
|
|
|
def test_func():
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
res = pytester.runpytest()
|
2019-06-30 23:15:29 +08:00
|
|
|
assert res.ret == 2
|
|
|
|
msg = "Defining 'pytest_plugins' in a non-top-level conftest is no longer supported"
|
2020-10-03 04:16:22 +08:00
|
|
|
res.stdout.fnmatch_lines([f"*{msg}*", f"*subdirectory{os.sep}conftest.py*"])
|
2019-06-30 23:15:29 +08:00
|
|
|
|
|
|
|
@pytest.mark.parametrize("use_pyargs", [True, False])
|
|
|
|
def test_pytest_plugins_in_non_top_level_conftest_unsupported_pyargs(
|
2020-12-09 04:20:02 +08:00
|
|
|
self, pytester: Pytester, use_pyargs: bool
|
|
|
|
) -> None:
|
2019-06-30 23:15:29 +08:00
|
|
|
"""When using --pyargs, do not emit the warning about non-top-level conftest warnings (#4039, #4044)"""
|
|
|
|
|
|
|
|
files = {
|
|
|
|
"src/pkg/__init__.py": "",
|
|
|
|
"src/pkg/conftest.py": "",
|
|
|
|
"src/pkg/test_root.py": "def test(): pass",
|
|
|
|
"src/pkg/sub/__init__.py": "",
|
|
|
|
"src/pkg/sub/conftest.py": "pytest_plugins=['capture']",
|
|
|
|
"src/pkg/sub/test_bar.py": "def test(): pass",
|
|
|
|
}
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makepyfile(**files)
|
|
|
|
pytester.syspathinsert(pytester.path.joinpath("src"))
|
2019-06-30 23:15:29 +08:00
|
|
|
|
|
|
|
args = ("--pyargs", "pkg") if use_pyargs else ()
|
2020-12-09 04:20:02 +08:00
|
|
|
res = pytester.runpytest(*args)
|
2019-06-30 23:15:29 +08:00
|
|
|
assert res.ret == (0 if use_pyargs else 2)
|
|
|
|
msg = (
|
|
|
|
msg
|
|
|
|
) = "Defining 'pytest_plugins' in a non-top-level conftest is no longer supported"
|
|
|
|
if use_pyargs:
|
|
|
|
assert msg not in res.stdout.str()
|
|
|
|
else:
|
2020-10-03 04:16:22 +08:00
|
|
|
res.stdout.fnmatch_lines([f"*{msg}*"])
|
2019-06-30 23:15:29 +08:00
|
|
|
|
|
|
|
def test_pytest_plugins_in_non_top_level_conftest_unsupported_no_top_level_conftest(
|
2020-12-09 04:20:02 +08:00
|
|
|
self, pytester: Pytester
|
|
|
|
) -> None:
|
|
|
|
subdirectory = pytester.path.joinpath("subdirectory")
|
2019-06-30 23:15:29 +08:00
|
|
|
subdirectory.mkdir()
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makeconftest(
|
2019-06-30 23:15:29 +08:00
|
|
|
"""
|
|
|
|
pytest_plugins=['capture']
|
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.path.joinpath("conftest.py").rename(
|
|
|
|
subdirectory.joinpath("conftest.py")
|
|
|
|
)
|
2019-06-30 23:15:29 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makepyfile(
|
2019-06-30 23:15:29 +08:00
|
|
|
"""
|
|
|
|
def test_func():
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
res = pytester.runpytest_subprocess()
|
2019-06-30 23:15:29 +08:00
|
|
|
assert res.ret == 2
|
|
|
|
msg = "Defining 'pytest_plugins' in a non-top-level conftest is no longer supported"
|
2020-10-03 04:16:22 +08:00
|
|
|
res.stdout.fnmatch_lines([f"*{msg}*", f"*subdirectory{os.sep}conftest.py*"])
|
2019-06-30 23:15:29 +08:00
|
|
|
|
|
|
|
def test_pytest_plugins_in_non_top_level_conftest_unsupported_no_false_positives(
|
2020-12-09 04:20:02 +08:00
|
|
|
self, pytester: Pytester
|
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2020-02-23 06:31:08 +08:00
|
|
|
"def test_func(): pass",
|
|
|
|
**{
|
|
|
|
"subdirectory/conftest": "pass",
|
|
|
|
"conftest": """
|
|
|
|
import warnings
|
|
|
|
warnings.filterwarnings('always', category=DeprecationWarning)
|
|
|
|
pytest_plugins=['capture']
|
|
|
|
""",
|
2020-05-06 03:00:55 +08:00
|
|
|
},
|
2019-06-30 23:15:29 +08:00
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
res = pytester.runpytest_subprocess()
|
2019-06-30 23:15:29 +08:00
|
|
|
assert res.ret == 0
|
|
|
|
msg = "Defining 'pytest_plugins' in a non-top-level conftest is no longer supported"
|
|
|
|
assert msg not in res.stdout.str()
|
2020-05-17 03:05:12 +08:00
|
|
|
|
|
|
|
|
2020-12-11 19:40:37 +08:00
|
|
|
def test_conftest_import_error_repr(tmp_path: Path) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""`ConftestImportFailure` should use a short error message and readable
|
|
|
|
path to the failed conftest.py file."""
|
2020-12-11 19:40:37 +08:00
|
|
|
path = tmp_path.joinpath("foo/conftest.py")
|
2020-05-17 03:05:12 +08:00
|
|
|
with pytest.raises(
|
|
|
|
ConftestImportFailure,
|
2020-10-03 04:16:22 +08:00
|
|
|
match=re.escape(f"RuntimeError: some error (from {path})"),
|
2020-05-17 03:05:12 +08:00
|
|
|
):
|
|
|
|
try:
|
|
|
|
raise RuntimeError("some error")
|
2020-06-21 05:34:41 +08:00
|
|
|
except Exception as exc:
|
|
|
|
assert exc.__traceback__ is not None
|
|
|
|
exc_info = (type(exc), exc, exc.__traceback__)
|
|
|
|
raise ConftestImportFailure(path, exc_info) from exc
|
2020-09-04 22:57:15 +08:00
|
|
|
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_strtobool() -> None:
|
2020-09-04 22:57:15 +08:00
|
|
|
assert _strtobool("YES")
|
|
|
|
assert not _strtobool("NO")
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
_strtobool("unknown")
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"arg, escape, expected",
|
|
|
|
[
|
|
|
|
("ignore", False, ("ignore", "", Warning, "", 0)),
|
|
|
|
(
|
|
|
|
"ignore::DeprecationWarning",
|
|
|
|
False,
|
|
|
|
("ignore", "", DeprecationWarning, "", 0),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"ignore:some msg:DeprecationWarning",
|
|
|
|
False,
|
|
|
|
("ignore", "some msg", DeprecationWarning, "", 0),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"ignore::DeprecationWarning:mod",
|
|
|
|
False,
|
|
|
|
("ignore", "", DeprecationWarning, "mod", 0),
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"ignore::DeprecationWarning:mod:42",
|
|
|
|
False,
|
|
|
|
("ignore", "", DeprecationWarning, "mod", 42),
|
|
|
|
),
|
|
|
|
("error:some\\msg:::", True, ("error", "some\\\\msg", Warning, "", 0)),
|
|
|
|
("error:::mod\\foo:", True, ("error", "", Warning, "mod\\\\foo\\Z", 0)),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_parse_warning_filter(
|
2020-10-03 04:02:22 +08:00
|
|
|
arg: str, escape: bool, expected: Tuple[str, str, Type[Warning], str, int]
|
2020-09-04 22:57:15 +08:00
|
|
|
) -> None:
|
|
|
|
assert parse_warning_filter(arg, escape=escape) == expected
|
|
|
|
|
|
|
|
|
2021-10-21 23:05:39 +08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"arg",
|
|
|
|
[
|
|
|
|
# Too much parts.
|
|
|
|
":" * 5,
|
|
|
|
# Invalid action.
|
|
|
|
"FOO::",
|
|
|
|
# ImportError when importing the warning class.
|
|
|
|
"::test_parse_warning_filter_failure.NonExistentClass::",
|
|
|
|
# Class is not a Warning subclass.
|
|
|
|
"::list::",
|
|
|
|
# Negative line number.
|
|
|
|
"::::-1",
|
|
|
|
# Not a line number.
|
|
|
|
"::::not-a-number",
|
|
|
|
],
|
|
|
|
)
|
2020-09-04 22:57:15 +08:00
|
|
|
def test_parse_warning_filter_failure(arg: str) -> None:
|
2021-10-21 23:05:39 +08:00
|
|
|
with pytest.raises(pytest.UsageError):
|
2020-09-04 22:57:15 +08:00
|
|
|
parse_warning_filter(arg, escape=True)
|
2021-07-31 04:37:38 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TestDebugOptions:
|
|
|
|
def test_without_debug_does_not_write_log(self, pytester: Pytester) -> None:
|
|
|
|
result = pytester.runpytest()
|
|
|
|
result.stderr.no_fnmatch_line(
|
|
|
|
"*writing pytest debug information to*pytestdebug.log"
|
|
|
|
)
|
|
|
|
result.stderr.no_fnmatch_line(
|
|
|
|
"*wrote pytest debug information to*pytestdebug.log"
|
|
|
|
)
|
|
|
|
assert not [f.name for f in pytester.path.glob("**/*.log")]
|
|
|
|
|
|
|
|
def test_with_only_debug_writes_pytestdebug_log(self, pytester: Pytester) -> None:
|
|
|
|
result = pytester.runpytest("--debug")
|
|
|
|
result.stderr.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*writing pytest debug information to*pytestdebug.log",
|
|
|
|
"*wrote pytest debug information to*pytestdebug.log",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
assert "pytestdebug.log" in [f.name for f in pytester.path.glob("**/*.log")]
|
|
|
|
|
|
|
|
def test_multiple_custom_debug_logs(self, pytester: Pytester) -> None:
|
|
|
|
result = pytester.runpytest("--debug", "bar.log")
|
|
|
|
result.stderr.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*writing pytest debug information to*bar.log",
|
|
|
|
"*wrote pytest debug information to*bar.log",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
result = pytester.runpytest("--debug", "foo.log")
|
|
|
|
result.stderr.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*writing pytest debug information to*foo.log",
|
|
|
|
"*wrote pytest debug information to*foo.log",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
assert {"bar.log", "foo.log"} == {
|
|
|
|
f.name for f in pytester.path.glob("**/*.log")
|
|
|
|
}
|
|
|
|
|
|
|
|
def test_debug_help(self, pytester: Pytester) -> None:
|
|
|
|
result = pytester.runpytest("-h")
|
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
2022-06-01 03:32:51 +08:00
|
|
|
"*Store internal tracing debug information in this log*",
|
|
|
|
"*file. This file is opened with 'w' and truncated as a*",
|
|
|
|
"*Default: pytestdebug.log.",
|
2021-07-31 04:37:38 +08:00
|
|
|
]
|
|
|
|
)
|
2023-11-19 22:56:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TestVerbosity:
|
|
|
|
SOME_OUTPUT_TYPE = Config.VERBOSITY_ASSERTIONS
|
|
|
|
SOME_OUTPUT_VERBOSITY_LEVEL = 5
|
|
|
|
|
|
|
|
class VerbosityIni:
|
|
|
|
def pytest_addoption(self, parser: Parser) -> None:
|
|
|
|
Config._add_verbosity_ini(
|
|
|
|
parser, TestVerbosity.SOME_OUTPUT_TYPE, help="some help text"
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_level_matches_verbose_when_not_specified(
|
|
|
|
self, pytester: Pytester, tmp_path: Path
|
|
|
|
) -> None:
|
|
|
|
tmp_path.joinpath("pytest.ini").write_text(
|
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
[pytest]
|
|
|
|
addopts = --verbose
|
|
|
|
"""
|
|
|
|
),
|
|
|
|
encoding="utf-8",
|
|
|
|
)
|
|
|
|
pytester.plugins = [TestVerbosity.VerbosityIni()]
|
|
|
|
|
|
|
|
config = pytester.parseconfig(tmp_path)
|
|
|
|
|
|
|
|
assert (
|
|
|
|
config.get_verbosity(TestVerbosity.SOME_OUTPUT_TYPE)
|
|
|
|
== config.option.verbose
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_level_matches_verbose_when_not_known_type(
|
|
|
|
self, pytester: Pytester, tmp_path: Path
|
|
|
|
) -> None:
|
|
|
|
tmp_path.joinpath("pytest.ini").write_text(
|
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
[pytest]
|
|
|
|
addopts = --verbose
|
|
|
|
"""
|
|
|
|
),
|
|
|
|
encoding="utf-8",
|
|
|
|
)
|
|
|
|
pytester.plugins = [TestVerbosity.VerbosityIni()]
|
|
|
|
|
|
|
|
config = pytester.parseconfig(tmp_path)
|
|
|
|
|
|
|
|
assert config.get_verbosity("some fake verbosity type") == config.option.verbose
|
|
|
|
|
|
|
|
def test_level_matches_specified_override(
|
|
|
|
self, pytester: Pytester, tmp_path: Path
|
|
|
|
) -> None:
|
|
|
|
setting_name = f"verbosity_{TestVerbosity.SOME_OUTPUT_TYPE}"
|
|
|
|
tmp_path.joinpath("pytest.ini").write_text(
|
|
|
|
textwrap.dedent(
|
|
|
|
f"""\
|
|
|
|
[pytest]
|
|
|
|
addopts = --verbose
|
|
|
|
{setting_name} = {TestVerbosity.SOME_OUTPUT_VERBOSITY_LEVEL}
|
|
|
|
"""
|
|
|
|
),
|
|
|
|
encoding="utf-8",
|
|
|
|
)
|
|
|
|
pytester.plugins = [TestVerbosity.VerbosityIni()]
|
|
|
|
|
|
|
|
config = pytester.parseconfig(tmp_path)
|
|
|
|
|
|
|
|
assert (
|
|
|
|
config.get_verbosity(TestVerbosity.SOME_OUTPUT_TYPE)
|
|
|
|
== TestVerbosity.SOME_OUTPUT_VERBOSITY_LEVEL
|
|
|
|
)
|