forked from pytest-dev/pytest
/
test_helpconfig.py
158 lines (131 loc) · 4.81 KB
/
test_helpconfig.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import pytest
from _pytest.config import ExitCode
from _pytest.pytester import Testdir
def test_version(testdir, pytestconfig):
testdir.monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD")
result = testdir.runpytest("--version")
assert result.ret == 0
# p = py.path.local(py.__file__).dirpath()
result.stderr.fnmatch_lines(
["*pytest*{}*imported from*".format(pytest.__version__)]
)
if pytestconfig.pluginmanager.list_plugin_distinfo():
result.stderr.fnmatch_lines(["*setuptools registered plugins:", "*at*"])
def test_help(testdir: Testdir) -> None:
result = testdir.runpytest("--help", "-p", "no:[defaults]")
assert result.ret == 0
trans_escape = str.maketrans({"[": "[[]", "]": "[]]"})
result.stdout.fnmatch_lines(
"""
usage: * [options] [file_or_dir] [file_or_dir] [...]
positional arguments:
file_or_dir
-m MARKEXPR only run tests matching given mark expression.
For example: -m 'mark1 and not mark2'.
reporting:
--durations=N *
collection:
--[no-]collect-only, --[no-]co
--[no-]conftest Don't load any conftest.py files.
test session debugging and configuration:
-V, --version display pytest version and information about plugins.
-h, --help show help message and configuration info
*setup.cfg*
*minversion*
*to see*markers*pytest --markers*
*to see*fixtures*pytest --fixtures*
""".translate(
trans_escape
)
)
result.stdout.no_fnmatch_line("logging:")
def test_help_for_empty_ini_params(testdir: "Testdir") -> None:
testdir.makeconftest(
"""
from argparse import SUPPRESS
def pytest_addoption(parser):
parser.addini("t_ini_none", None, default=True, type="bool")
parser.addini("t_ini_empty", "", default=True, type="bool")
parser.addoption("--t_opt_none", help=None)
parser.addoption("--t_opt_empty", help="")
parser.addoption("--t_opt_suppress", help=SUPPRESS)
"""
)
result = testdir.runpytest("--help")
result.stdout.fnmatch_lines(
[
"custom options:",
" --t_opt_none=T_OPT_NONE",
" --t_opt_empty=T_OPT_EMPTY",
"",
"[pytest] ini-options in the first pytest.ini|tox.ini|setup.cfg file found:",
" t_ini_empty (bool): ",
]
)
result_str = result.stdout.str()
assert "t_ini_none" not in result_str
assert "t_opt_suppress" not in result_str
@pytest.mark.parametrize("method", ("runpytest_inprocess", "runpytest_subprocess"))
def test_help_unconfigures_always(method: str, testdir: Testdir) -> None:
testdir.makeconftest(
"""
def pytest_addoption(parser):
parser._usage = "%(crash_help)s"
"""
)
testdir.makepyfile(
myplugin="""
def pytest_configure():
print("plugin pytest_configure")
def pytest_unconfigure():
print("plugin pytest_unconfigure")
"""
)
testdir.syspathinsert()
result = getattr(testdir, method)("--help", "-p", "no:[defaults]", "-p", "myplugin")
assert result.stdout.lines == [
"plugin pytest_configure",
"plugin pytest_unconfigure",
]
assert "KeyError: 'crash_help'" in result.stderr.lines
# XXX: should have the same exitcode?!
if method == "runpytest_inprocess":
assert result.ret == ExitCode.INTERNAL_ERROR
else:
assert result.ret == ExitCode.TESTS_FAILED
def test_hookvalidation_unknown(testdir):
testdir.makeconftest(
"""
def pytest_hello(xyz):
pass
"""
)
result = testdir.runpytest()
assert result.ret != 0
result.stdout.fnmatch_lines(["*unknown hook*pytest_hello*"])
def test_hookvalidation_optional(testdir):
testdir.makeconftest(
"""
import pytest
@pytest.hookimpl(optionalhook=True)
def pytest_hello(xyz):
pass
"""
)
result = testdir.runpytest()
assert result.ret == ExitCode.NO_TESTS_COLLECTED
def test_traceconfig(testdir):
result = testdir.runpytest("--traceconfig")
result.stdout.fnmatch_lines(["*using*pytest*py*", "*active plugins*"])
def test_debug(testdir):
result = testdir.runpytest_subprocess("--debug")
assert result.ret == ExitCode.NO_TESTS_COLLECTED
p = testdir.tmpdir.join("pytestdebug.log")
assert "pytest_sessionstart" in p.read()
def test_PYTEST_DEBUG(testdir, monkeypatch):
monkeypatch.setenv("PYTEST_DEBUG", "1")
result = testdir.runpytest_subprocess()
assert result.ret == ExitCode.NO_TESTS_COLLECTED
result.stderr.fnmatch_lines(
["*pytest_plugin_registered*", "*manager*PluginManager*"]
)