3 from pathlib
import Path
4 from pytest_output_to_files
import _DEFAULT_LINE_LIMIT
7 def test_help_message(testdir
):
8 # type: (pytest.Testdir) -> None
9 result
= testdir
.runpytest(
12 # fnmatch_lines does an assertion internally
13 result
.stdout
.fnmatch_lines([
15 '*--shorten-output-dir=DIR*',
16 '*shorten test outputs by storing them in files in DIR and*',
17 '*returning just the first/last few lines. disable by*',
18 '*using --shorten-output-dir=""*',
19 '*--shorten-output-lines=LINES*',
20 '*change the number of lines shown by the*',
21 '*--shorten-output-dir option*',
25 def do_stdout_stderr_check(testdir
, additional_args
, stdout_lines
,
26 stderr_lines
, enabled
, line_limit
):
27 # type: (pytest.Testdir, list[str], int, int, bool, int) -> pytest.RunResult
28 testdir
.makepyfile(test_print
=f
"""
32 for i in range({stdout_lines}):
33 print(f'in stdout {{i}}')
34 for i in range({stderr_lines}):
35 print(f'in stderr {{i}}', file=sys.stderr)
39 full_stdout
= ''.join(f
'in stdout {i}\n' for i
in range(stdout_lines
))
40 full_stderr
= ''.join(f
'in stderr {i}\n' for i
in range(stderr_lines
))
42 result
= testdir
.runpytest('-v', *additional_args
)
44 test_out_path
= Path(testdir
.tmpdir
)
45 test_out_path
/= "test-out"
46 test_print_path
= test_out_path
/ "test_print_py"
47 test_print_path
/= "test_print"
48 call_stdout_path
= test_print_path
/ "call-stdout.txt"
49 call_stderr_path
= test_print_path
/ "call-stderr.txt"
51 lines
= ['*--- Captured stdout call ---*']
53 if enabled
and stdout_lines
>= line_limit
:
54 trimmed_msg
= ("Output Trimmed, Full output in: "
55 "test-out/test_print_py/test_print/call-stdout.txt")
56 lines
.append(trimmed_msg
)
58 for i
in range((line_limit
+ 1) // 2):
59 lines
.append(f
'in stdout {i}')
61 lines
.append(trimmed_msg
)
63 for i
in range(stdout_lines
- line_limit
// 2, stdout_lines
):
64 lines
.append(f
'in stdout {i}')
66 lines
.append(trimmed_msg
)
68 for i
in range(stdout_lines
):
69 lines
.append(f
'in stdout {i}')
70 lines
.append('*--- Captured stderr call ---*')
71 if enabled
and stderr_lines
>= line_limit
:
72 trimmed_msg
= ("Output Trimmed, Full output in: "
73 "test-out/test_print_py/test_print/call-stderr.txt")
74 lines
.append(trimmed_msg
)
76 for i
in range((line_limit
+ 1) // 2):
77 lines
.append(f
'in stderr {i}')
79 lines
.append(trimmed_msg
)
81 for i
in range(stderr_lines
- line_limit
// 2, stderr_lines
):
82 lines
.append(f
'in stderr {i}')
84 lines
.append(trimmed_msg
)
86 for i
in range(stderr_lines
):
87 lines
.append(f
'in stderr {i}')
88 lines
.append("*====*")
90 result
.stdout
.fnmatch_lines(lines
, consecutive
=True)
92 result
.stdout
.fnmatch_lines([
93 'FAILED test_print.py::test_print *',
97 for empty_file
in ("setup-stdout.txt", "setup-stderr.txt",
98 "teardown-stdout.txt", "teardown-stderr.txt"):
99 assert (test_print_path
/ empty_file
).read_text("utf-8") == ""
100 assert call_stdout_path
.read_text("utf-8") == full_stdout
101 assert call_stderr_path
.read_text("utf-8") == full_stderr
102 call_stdout_path
.unlink() # remove big files
103 call_stderr_path
.unlink() # remove big files
105 assert not test_out_path
.exists()
106 assert result
.ret
!= 0
111 def test_ini_setting(testdir
):
112 # type: (pytest.Testdir) -> None
115 shorten-output-dir = test-out
118 do_stdout_stderr_check(testdir
, [], 1, 1, True,
119 line_limit
=_DEFAULT_LINE_LIMIT
)
122 def test_nothing(testdir
):
123 # type: (pytest.Testdir) -> None
124 do_stdout_stderr_check(testdir
, [], 1, 1, False,
125 line_limit
=_DEFAULT_LINE_LIMIT
)
128 def test_arg(testdir
):
129 # type: (pytest.Testdir) -> None
130 do_stdout_stderr_check(
131 testdir
, ["--shorten-output-dir=test-out"], 1, 1, True,
132 line_limit
=_DEFAULT_LINE_LIMIT
)
135 def test_arg_override_ini(testdir
):
136 # type: (pytest.Testdir) -> None
139 shorten-output-dir = test-out
142 do_stdout_stderr_check(
143 testdir
, ["--shorten-output-dir="], 1, 1, False,
144 line_limit
=_DEFAULT_LINE_LIMIT
)
147 def test_disable_capture(testdir
):
148 # type: (pytest.Testdir) -> None
151 shorten-output-dir = test-out
154 testdir
.makepyfile(test_print
=f
"""
159 print(f'in stderr', file=sys.stderr)
163 result
= testdir
.runpytest('-v', '-s')
165 test_out_path
= Path(testdir
.tmpdir
)
166 test_out_path
/= "test-out"
168 assert not test_out_path
.exists()
170 result
.stdout
.fnmatch_lines(['test_print.py::test_print*in stdout'])
171 result
.stderr
.fnmatch_lines(['in stderr'])
173 assert result
.ret
!= 0
176 def test_20k_disabled(testdir
):
177 # type: (pytest.Testdir) -> None
178 do_stdout_stderr_check(testdir
, [], 20000, 20000, False,
179 line_limit
=_DEFAULT_LINE_LIMIT
)
182 def test_20k(testdir
):
183 # type: (pytest.Testdir) -> None
184 do_stdout_stderr_check(
185 testdir
, ["--shorten-output-dir=test-out"], 20000, 20000, True,
186 line_limit
=_DEFAULT_LINE_LIMIT
)
189 def test_21k(testdir
):
190 # type: (pytest.Testdir) -> None
191 do_stdout_stderr_check(
192 testdir
, ["--shorten-output-dir=test-out"], 21000, 21000, True,
193 line_limit
=_DEFAULT_LINE_LIMIT
)
196 def test_22k(testdir
):
197 # type: (pytest.Testdir) -> None
198 do_stdout_stderr_check(
199 testdir
, ["--shorten-output-dir=test-out"], 22000, 22000, True,
200 line_limit
=_DEFAULT_LINE_LIMIT
)
203 def test_half(testdir
):
204 # type: (pytest.Testdir) -> None
205 lines
= _DEFAULT_LINE_LIMIT
// 2
206 do_stdout_stderr_check(
207 testdir
, ["--shorten-output-dir=test-out"], lines
, lines
, True,
208 line_limit
=_DEFAULT_LINE_LIMIT
)
211 def test_75_percent(testdir
):
212 # type: (pytest.Testdir) -> None
213 lines
= _DEFAULT_LINE_LIMIT
* 3 // 4
214 do_stdout_stderr_check(
215 testdir
, ["--shorten-output-dir=test-out"], lines
, lines
, True,
216 line_limit
=_DEFAULT_LINE_LIMIT
)
219 def test_limit_minus_two(testdir
):
220 # type: (pytest.Testdir) -> None
221 lines
= _DEFAULT_LINE_LIMIT
- 2
222 do_stdout_stderr_check(
223 testdir
, ["--shorten-output-dir=test-out"], lines
, lines
, True,
224 line_limit
=_DEFAULT_LINE_LIMIT
)
227 def test_limit_minus_one(testdir
):
228 # type: (pytest.Testdir) -> None
229 lines
= _DEFAULT_LINE_LIMIT
- 1
230 do_stdout_stderr_check(
231 testdir
, ["--shorten-output-dir=test-out"], lines
, lines
, True,
232 line_limit
=_DEFAULT_LINE_LIMIT
)
235 def test_limit(testdir
):
236 # type: (pytest.Testdir) -> None
237 lines
= _DEFAULT_LINE_LIMIT
238 do_stdout_stderr_check(
239 testdir
, ["--shorten-output-dir=test-out"], lines
, lines
, True,
240 line_limit
=_DEFAULT_LINE_LIMIT
)
243 def test_limit_plus_one(testdir
):
244 # type: (pytest.Testdir) -> None
245 lines
= _DEFAULT_LINE_LIMIT
+ 1
246 do_stdout_stderr_check(
247 testdir
, ["--shorten-output-dir=test-out"], lines
, lines
, True,
248 line_limit
=_DEFAULT_LINE_LIMIT
)
251 def test_limit_plus_two(testdir
):
252 # type: (pytest.Testdir) -> None
253 lines
= _DEFAULT_LINE_LIMIT
+ 2
254 do_stdout_stderr_check(
255 testdir
, ["--shorten-output-dir=test-out"], lines
, lines
, True,
256 line_limit
=_DEFAULT_LINE_LIMIT
)
259 def test_1M(testdir
):
260 # type: (pytest.Testdir) -> None
262 do_stdout_stderr_check(
263 testdir
, ["--shorten-output-dir=test-out"], lines
, lines
, True,
264 line_limit
=_DEFAULT_LINE_LIMIT
)
267 def test_small_limit_minus_one(testdir
):
268 # type: (pytest.Testdir) -> None
270 lines
= line_limit
- 1
271 do_stdout_stderr_check(testdir
,
272 ["--shorten-output-dir=test-out",
273 f
"--shorten-output-lines={line_limit}"],
274 lines
, lines
, True, line_limit
=line_limit
)
277 def test_small_limit(testdir
):
278 # type: (pytest.Testdir) -> None
281 do_stdout_stderr_check(testdir
,
282 ["--shorten-output-dir=test-out",
283 f
"--shorten-output-lines={line_limit}"],
284 lines
, lines
, True, line_limit
=line_limit
)
287 def test_small_limit_plus_one(testdir
):
288 # type: (pytest.Testdir) -> None
290 lines
= line_limit
+ 1
291 do_stdout_stderr_check(testdir
,
292 ["--shorten-output-dir=test-out",
293 f
"--shorten-output-lines={line_limit}"],
294 lines
, lines
, True, line_limit
=line_limit
)
297 def test_large_limit_minus_one(testdir
):
298 # type: (pytest.Testdir) -> None
300 lines
= line_limit
- 1
301 do_stdout_stderr_check(testdir
,
302 ["--shorten-output-dir=test-out",
303 f
"--shorten-output-lines={line_limit}"],
304 lines
, lines
, True, line_limit
=line_limit
)
307 def test_large_limit(testdir
):
308 # type: (pytest.Testdir) -> None
311 do_stdout_stderr_check(testdir
,
312 ["--shorten-output-dir=test-out",
313 f
"--shorten-output-lines={line_limit}"],
314 lines
, lines
, True, line_limit
=line_limit
)
317 def test_large_limit_plus_one(testdir
):
318 # type: (pytest.Testdir) -> None
320 lines
= line_limit
+ 1
321 do_stdout_stderr_check(testdir
,
322 ["--shorten-output-dir=test-out",
323 f
"--shorten-output-lines={line_limit}"],
324 lines
, lines
, True, line_limit
=line_limit
)
327 if __name__
== "__main__":
328 sys
.exit(pytest
.main())