Debian Codegraph @

testing/test_helpconfig.py

raw
1 # mypy: allow-untyped-defs
2 from __future__ import annotations
3
4 from _pytest.config import ExitCode
5 from _pytest.pytester import Pytester
6 import pytest
7
8
9 def test_version_verbose(pytester: Pytester, pytestconfig, monkeypatch) -> None:
10 monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD")
11 monkeypatch.delenv("PYTEST_PLUGINS", raising=False)
12 result = pytester.runpytest("--version", "--version")
13 assert result.ret == ExitCode.OK
14 result.stdout.fnmatch_lines([f"*pytest*{pytest.__version__}*imported from*"])
15 if pytestconfig.pluginmanager.list_plugin_distinfo():
16 result.stdout.fnmatch_lines(["*registered third-party plugins:", "*at*"])
17
18
19 @pytest.mark.parametrize("flag", ["--version", "-V"])
20 def test_version_less_verbose(pytester: Pytester, flag: str) -> None:
21 """Single ``--version`` or ``-V`` should display only the pytest version, without loading plugins (#13574)."""
22 pytester.makeconftest("print('This should not be printed')")
23 result = pytester.runpytest_subprocess(flag)
24 assert result.ret == ExitCode.OK
25 assert result.stdout.str().strip() == f"pytest {pytest.__version__}"
26
27
28 def test_versions() -> None:
29 """Regression check for the public version attributes in pytest."""
30 assert isinstance(pytest.__version__, str)
31 assert isinstance(pytest.version_tuple, tuple)
32
33
34 def test_help(pytester: Pytester) -> None:
35 result = pytester.runpytest("--help")
36 assert result.ret == ExitCode.OK
37 result.stdout.fnmatch_lines(
38 """
39 -m MARKEXPR Only run tests matching given mark expression. For
40 example: -m 'mark1 and not mark2'.
41 Reporting:
42 --durations=N *
43 -V, --version Display pytest version and information about plugins.
44 When given twice, also display information about
45 plugins.
46 *setup.cfg*
47 *minversion*
48 *to see*markers*pytest --markers*
49 *to see*fixtures*pytest --fixtures*
50 """
51 )
52
53
54 def test_none_help_param_raises_exception(pytester: Pytester) -> None:
55 """Test that a None help param raises a TypeError."""
56 pytester.makeconftest(
57 """
58 def pytest_addoption(parser):
59 parser.addini("test_ini", None, default=True, type="bool")
60 """
61 )
62 result = pytester.runpytest("--help")
63 result.stderr.fnmatch_lines(
64 ["*TypeError: help argument cannot be None for test_ini*"]
65 )
66
67
68 def test_empty_help_param(pytester: Pytester) -> None:
69 """Test that an empty help param is displayed correctly."""
70 pytester.makeconftest(
71 """
72 def pytest_addoption(parser):
73 parser.addini("test_ini", "", default=True, type="bool")
74 """
75 )
76 result = pytester.runpytest("--help")
77 assert result.ret == ExitCode.OK
78 lines = [
79 " required_plugins (args):",
80 " Plugins that must be present for pytest to run*",
81 " test_ini (bool):*",
82 "Environment variables:",
83 ]
84 result.stdout.fnmatch_lines(lines, consecutive=True)
85
86
87 def test_parse_known_args_doesnt_quit_on_help(pytester: Pytester) -> None:
88 """`parse_known_args` shouldn't exit on `--help`, unlike `parse`."""
89 config = pytester.parseconfig()
90 # Doesn't raise or exit!
91 config._parser.parse_known_args(["--help"])
92 config._parser.parse_known_and_unknown_args(["--help"])
93
94
95 def test_hookvalidation_unknown(pytester: Pytester) -> None:
96 pytester.makeconftest(
97 """
98 def pytest_hello(xyz):
99 pass
100 """
101 )
102 result = pytester.runpytest()
103 assert result.ret != ExitCode.OK
104 result.stdout.fnmatch_lines(["*unknown hook*pytest_hello*"])
105
106
107 def test_hookvalidation_optional(pytester: Pytester) -> None:
108 pytester.makeconftest(
109 """
110 import pytest
111 @pytest.hookimpl(optionalhook=True)
112 def pytest_hello(xyz):
113 pass
114 """
115 )
116 result = pytester.runpytest()
117 assert result.ret == ExitCode.NO_TESTS_COLLECTED
118
119
120 def test_traceconfig(pytester: Pytester) -> None:
121 result = pytester.runpytest("--traceconfig")
122 result.stdout.fnmatch_lines(["*using*pytest*", "*active plugins*"])
123
124
125 def test_debug(pytester: Pytester) -> None:
126 result = pytester.runpytest_subprocess("--debug")
127 assert result.ret == ExitCode.NO_TESTS_COLLECTED
128 p = pytester.path.joinpath("pytestdebug.log")
129 assert "pytest_sessionstart" in p.read_text("utf-8")
130
131
132 def test_PYTEST_DEBUG(pytester: Pytester, monkeypatch) -> None:
133 monkeypatch.setenv("PYTEST_DEBUG", "1")
134 result = pytester.runpytest_subprocess()
135 assert result.ret == ExitCode.NO_TESTS_COLLECTED
136 result.stderr.fnmatch_lines(
137 ["*pytest_plugin_registered*", "*manager*PluginManager*"]
138 )