Skip to content

Commit d5c62d0

Browse files
authored
Making --debug more configurable for the pytest user (#8955)
Close #8954
1 parent 6d6bc97 commit d5c62d0

File tree

4 files changed

+68
-6
lines changed

4 files changed

+68
-6
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,6 @@ coverage.xml
5353

5454
# generated by pip
5555
pip-wheel-metadata/
56+
57+
# pytest debug logs generated via --debug
58+
pytestdebug.log

changelog/8954.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
``--debug`` flag now accepts a :class:`str` file to route debug logs into, remains defaulted to `pytestdebug.log`.

src/_pytest/helpconfig.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,14 @@ def pytest_addoption(parser: Parser) -> None:
8080
)
8181
group.addoption(
8282
"--debug",
83-
action="store_true",
83+
action="store",
84+
nargs="?",
85+
const="pytestdebug.log",
8486
dest="debug",
85-
default=False,
86-
help="store internal tracing debug information in 'pytestdebug.log'.",
87+
metavar="DEBUG_FILE_NAME",
88+
help="store internal tracing debug information in this log file.\n"
89+
"This file is opened with 'w' and truncated as a result, care advised.\n"
90+
"Defaults to 'pytestdebug.log'.",
8791
)
8892
group._addoption(
8993
"-o",
@@ -98,8 +102,10 @@ def pytest_addoption(parser: Parser) -> None:
98102
def pytest_cmdline_parse():
99103
outcome = yield
100104
config: Config = outcome.get_result()
105+
101106
if config.option.debug:
102-
path = os.path.abspath("pytestdebug.log")
107+
# --debug | --debug <file.log> was provided.
108+
path = config.option.debug
103109
debugfile = open(path, "w")
104110
debugfile.write(
105111
"versions pytest-%s, py-%s, "
@@ -114,11 +120,11 @@ def pytest_cmdline_parse():
114120
)
115121
config.trace.root.setwriter(debugfile.write)
116122
undo_tracing = config.pluginmanager.enable_tracing()
117-
sys.stderr.write("writing pytestdebug information to %s\n" % path)
123+
sys.stderr.write("writing pytest debug information to %s\n" % path)
118124

119125
def unset_tracing() -> None:
120126
debugfile.close()
121-
sys.stderr.write("wrote pytestdebug information to %s\n" % debugfile.name)
127+
sys.stderr.write("wrote pytest debug information to %s\n" % debugfile.name)
122128
config.trace.root.setwriter(None)
123129
undo_tracing()
124130

testing/test_config.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2042,3 +2042,55 @@ def test_parse_warning_filter_failure(arg: str) -> None:
20422042

20432043
with pytest.raises(warnings._OptionError):
20442044
parse_warning_filter(arg, escape=True)
2045+
2046+
2047+
class TestDebugOptions:
2048+
def test_without_debug_does_not_write_log(self, pytester: Pytester) -> None:
2049+
result = pytester.runpytest()
2050+
result.stderr.no_fnmatch_line(
2051+
"*writing pytest debug information to*pytestdebug.log"
2052+
)
2053+
result.stderr.no_fnmatch_line(
2054+
"*wrote pytest debug information to*pytestdebug.log"
2055+
)
2056+
assert not [f.name for f in pytester.path.glob("**/*.log")]
2057+
2058+
def test_with_only_debug_writes_pytestdebug_log(self, pytester: Pytester) -> None:
2059+
result = pytester.runpytest("--debug")
2060+
result.stderr.fnmatch_lines(
2061+
[
2062+
"*writing pytest debug information to*pytestdebug.log",
2063+
"*wrote pytest debug information to*pytestdebug.log",
2064+
]
2065+
)
2066+
assert "pytestdebug.log" in [f.name for f in pytester.path.glob("**/*.log")]
2067+
2068+
def test_multiple_custom_debug_logs(self, pytester: Pytester) -> None:
2069+
result = pytester.runpytest("--debug", "bar.log")
2070+
result.stderr.fnmatch_lines(
2071+
[
2072+
"*writing pytest debug information to*bar.log",
2073+
"*wrote pytest debug information to*bar.log",
2074+
]
2075+
)
2076+
result = pytester.runpytest("--debug", "foo.log")
2077+
result.stderr.fnmatch_lines(
2078+
[
2079+
"*writing pytest debug information to*foo.log",
2080+
"*wrote pytest debug information to*foo.log",
2081+
]
2082+
)
2083+
2084+
assert {"bar.log", "foo.log"} == {
2085+
f.name for f in pytester.path.glob("**/*.log")
2086+
}
2087+
2088+
def test_debug_help(self, pytester: Pytester) -> None:
2089+
result = pytester.runpytest("-h")
2090+
result.stdout.fnmatch_lines(
2091+
[
2092+
"*store internal tracing debug information in this log*",
2093+
"*This file is opened with 'w' and truncated as a result*",
2094+
"*Defaults to 'pytestdebug.log'.",
2095+
]
2096+
)

0 commit comments

Comments
 (0)