2018-10-25 15:01:29 +08:00
|
|
|
import contextlib
|
2019-01-22 11:46:57 +08:00
|
|
|
import io
|
2014-01-25 04:22:19 +08:00
|
|
|
import os
|
2019-01-22 11:46:57 +08:00
|
|
|
import subprocess
|
2014-01-25 04:22:19 +08:00
|
|
|
import sys
|
2018-08-24 00:06:17 +08:00
|
|
|
import textwrap
|
2017-03-15 03:45:56 +08:00
|
|
|
from io import UnsupportedOperation
|
2020-01-26 01:11:38 +08:00
|
|
|
from typing import BinaryIO
|
2020-05-01 19:40:17 +08:00
|
|
|
from typing import cast
|
2020-01-26 02:21:19 +08:00
|
|
|
from typing import Generator
|
2020-05-01 19:40:17 +08:00
|
|
|
from typing import TextIO
|
2015-11-27 22:43:01 +08:00
|
|
|
|
2018-10-25 15:01:29 +08:00
|
|
|
import pytest
|
2014-01-25 04:22:19 +08:00
|
|
|
from _pytest import capture
|
2020-02-23 06:39:20 +08:00
|
|
|
from _pytest.capture import _get_multicapture
|
2020-12-09 04:20:02 +08:00
|
|
|
from _pytest.capture import CaptureFixture
|
2010-11-13 18:10:45 +08:00
|
|
|
from _pytest.capture import CaptureManager
|
2020-08-10 02:20:07 +08:00
|
|
|
from _pytest.capture import CaptureResult
|
2020-02-23 06:39:20 +08:00
|
|
|
from _pytest.capture import MultiCapture
|
2020-02-11 05:43:30 +08:00
|
|
|
from _pytest.config import ExitCode
|
2020-12-09 04:20:02 +08:00
|
|
|
from _pytest.monkeypatch import MonkeyPatch
|
|
|
|
from _pytest.pytester import Pytester
|
2017-03-17 09:27:28 +08:00
|
|
|
|
2018-10-25 15:01:29 +08:00
|
|
|
# note: py.io capture tests where copied from
|
|
|
|
# pylib 1.4.20.dev2 (rev 13d9af95547e)
|
|
|
|
|
2009-07-31 20:21:02 +08:00
|
|
|
|
2020-08-10 01:15:19 +08:00
|
|
|
def StdCaptureFD(
|
|
|
|
out: bool = True, err: bool = True, in_: bool = True
|
|
|
|
) -> MultiCapture[str]:
|
2020-04-13 01:47:28 +08:00
|
|
|
return capture.MultiCapture(
|
|
|
|
in_=capture.FDCapture(0) if in_ else None,
|
|
|
|
out=capture.FDCapture(1) if out else None,
|
|
|
|
err=capture.FDCapture(2) if err else None,
|
|
|
|
)
|
2014-03-28 14:03:37 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-08-10 01:15:19 +08:00
|
|
|
def StdCapture(
|
|
|
|
out: bool = True, err: bool = True, in_: bool = True
|
|
|
|
) -> MultiCapture[str]:
|
2020-04-13 01:47:28 +08:00
|
|
|
return capture.MultiCapture(
|
|
|
|
in_=capture.SysCapture(0) if in_ else None,
|
|
|
|
out=capture.SysCapture(1) if out else None,
|
|
|
|
err=capture.SysCapture(2) if err else None,
|
|
|
|
)
|
2014-03-28 14:03:37 +08:00
|
|
|
|
|
|
|
|
2020-08-10 01:15:19 +08:00
|
|
|
def TeeStdCapture(
|
|
|
|
out: bool = True, err: bool = True, in_: bool = True
|
|
|
|
) -> MultiCapture[str]:
|
2020-04-13 01:47:28 +08:00
|
|
|
return capture.MultiCapture(
|
2020-04-16 14:49:17 +08:00
|
|
|
in_=capture.SysCapture(0, tee=True) if in_ else None,
|
|
|
|
out=capture.SysCapture(1, tee=True) if out else None,
|
|
|
|
err=capture.SysCapture(2, tee=True) if err else None,
|
2020-04-13 01:47:28 +08:00
|
|
|
)
|
2019-12-04 06:15:13 +08:00
|
|
|
|
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestCaptureManager:
|
2020-03-12 20:56:57 +08:00
|
|
|
@pytest.mark.parametrize("method", ["no", "sys", "fd"])
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_capturing_basic_api(self, method) -> None:
|
2014-03-28 14:03:37 +08:00
|
|
|
capouter = StdCaptureFD()
|
2009-07-31 20:21:02 +08:00
|
|
|
old = sys.stdout, sys.stderr, sys.stdin
|
|
|
|
try:
|
2014-04-01 20:32:12 +08:00
|
|
|
capman = CaptureManager(method)
|
2017-09-27 06:54:26 +08:00
|
|
|
capman.start_global_capturing()
|
2018-08-19 21:26:57 +08:00
|
|
|
capman.suspend_global_capture()
|
2018-08-20 23:48:14 +08:00
|
|
|
outerr = capman.read_global_capture()
|
2014-04-01 20:32:12 +08:00
|
|
|
assert outerr == ("", "")
|
2018-08-19 21:26:57 +08:00
|
|
|
capman.suspend_global_capture()
|
2018-08-20 23:48:14 +08:00
|
|
|
outerr = capman.read_global_capture()
|
2010-05-18 01:00:39 +08:00
|
|
|
assert outerr == ("", "")
|
2017-07-21 06:14:54 +08:00
|
|
|
print("hello")
|
2018-08-19 21:26:57 +08:00
|
|
|
capman.suspend_global_capture()
|
2018-08-20 23:48:14 +08:00
|
|
|
out, err = capman.read_global_capture()
|
2009-07-31 20:21:02 +08:00
|
|
|
if method == "no":
|
|
|
|
assert old == (sys.stdout, sys.stderr, sys.stdin)
|
|
|
|
else:
|
2014-04-01 20:32:12 +08:00
|
|
|
assert not out
|
2017-09-27 06:54:26 +08:00
|
|
|
capman.resume_global_capture()
|
2017-07-21 06:14:54 +08:00
|
|
|
print("hello")
|
2018-08-19 21:26:57 +08:00
|
|
|
capman.suspend_global_capture()
|
2018-08-20 23:48:14 +08:00
|
|
|
out, err = capman.read_global_capture()
|
2014-04-01 20:32:12 +08:00
|
|
|
if method != "no":
|
|
|
|
assert out == "hello\n"
|
2017-09-27 06:54:26 +08:00
|
|
|
capman.stop_global_capturing()
|
2009-07-31 20:21:02 +08:00
|
|
|
finally:
|
2014-03-28 14:11:25 +08:00
|
|
|
capouter.stop_capturing()
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2014-04-01 20:32:12 +08:00
|
|
|
def test_init_capturing(self):
|
2014-03-28 14:03:37 +08:00
|
|
|
capouter = StdCaptureFD()
|
2009-07-31 20:21:02 +08:00
|
|
|
try:
|
2014-04-01 20:32:12 +08:00
|
|
|
capman = CaptureManager("fd")
|
2017-09-27 06:54:26 +08:00
|
|
|
capman.start_global_capturing()
|
2018-11-23 02:05:10 +08:00
|
|
|
pytest.raises(AssertionError, capman.start_global_capturing)
|
2017-09-27 06:54:26 +08:00
|
|
|
capman.stop_global_capturing()
|
2009-07-31 20:21:02 +08:00
|
|
|
finally:
|
2014-03-28 14:11:25 +08:00
|
|
|
capouter.stop_capturing()
|
2009-07-31 20:21:02 +08:00
|
|
|
|
2014-01-25 04:22:19 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.parametrize("method", ["fd", "sys"])
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_capturing_unicode(pytester: Pytester, method: str) -> None:
|
2019-05-28 07:31:52 +08:00
|
|
|
obj = "'b\u00f6y'"
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makepyfile(
|
2019-06-03 06:40:34 +08:00
|
|
|
"""\
|
2009-08-19 22:42:29 +08:00
|
|
|
# taken from issue 227 from nosetests
|
2009-08-06 20:34:19 +08:00
|
|
|
def test_unicode():
|
|
|
|
import sys
|
2018-11-22 16:15:14 +08:00
|
|
|
print(sys.stdout)
|
|
|
|
print(%s)
|
2019-06-03 06:40:34 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
% obj
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest("--capture=%s" % method)
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
2009-08-06 20:34:19 +08:00
|
|
|
|
2014-01-25 04:22:19 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.parametrize("method", ["fd", "sys"])
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_capturing_bytes_in_utf8_encoding(pytester: Pytester, method: str) -> None:
|
|
|
|
pytester.makepyfile(
|
2019-06-05 08:48:06 +08:00
|
|
|
"""\
|
2009-08-19 22:42:29 +08:00
|
|
|
def test_unicode():
|
2018-11-22 16:15:14 +08:00
|
|
|
print('b\\u00f6y')
|
2019-06-05 08:48:06 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest("--capture=%s" % method)
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
2009-08-19 22:42:29 +08:00
|
|
|
|
2014-01-25 04:22:19 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_collect_capturing(pytester: Pytester) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2019-03-24 06:14:39 +08:00
|
|
|
import sys
|
|
|
|
|
2018-11-22 16:15:14 +08:00
|
|
|
print("collect %s failure" % 13)
|
2019-03-24 06:14:39 +08:00
|
|
|
sys.stderr.write("collect %s_stderr failure" % 13)
|
2009-07-31 20:21:02 +08:00
|
|
|
import xyz42123
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest(p)
|
2019-03-24 06:14:39 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*Captured stdout*",
|
|
|
|
"collect 13 failure",
|
|
|
|
"*Captured stderr*",
|
|
|
|
"collect 13_stderr failure",
|
|
|
|
]
|
|
|
|
)
|
2009-07-31 20:21:02 +08:00
|
|
|
|
2014-01-25 04:22:19 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestPerTestCapturing:
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_capture_and_fixtures(self, pytester: Pytester) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2009-07-31 20:21:02 +08:00
|
|
|
def setup_module(mod):
|
2018-11-22 16:15:14 +08:00
|
|
|
print("setup module")
|
2009-07-31 20:21:02 +08:00
|
|
|
def setup_function(function):
|
2018-11-22 16:15:14 +08:00
|
|
|
print("setup " + function.__name__)
|
2009-07-31 20:21:02 +08:00
|
|
|
def test_func1():
|
2018-11-22 16:15:14 +08:00
|
|
|
print("in func1")
|
2009-07-31 20:21:02 +08:00
|
|
|
assert 0
|
|
|
|
def test_func2():
|
2018-11-22 16:15:14 +08:00
|
|
|
print("in func2")
|
2009-07-31 20:21:02 +08:00
|
|
|
assert 0
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest(p)
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"setup module*",
|
|
|
|
"setup test_func1*",
|
|
|
|
"in func1*",
|
|
|
|
"setup test_func2*",
|
|
|
|
"in func2*",
|
|
|
|
]
|
|
|
|
)
|
2009-07-31 20:21:02 +08:00
|
|
|
|
2014-01-29 20:56:24 +08:00
|
|
|
@pytest.mark.xfail(reason="unimplemented feature")
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_capture_scope_cache(self, pytester: Pytester) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2009-08-03 18:16:12 +08:00
|
|
|
import sys
|
|
|
|
def setup_module(func):
|
2018-11-22 16:15:14 +08:00
|
|
|
print("module-setup")
|
2009-08-03 18:16:12 +08:00
|
|
|
def setup_function(func):
|
2018-11-22 16:15:14 +08:00
|
|
|
print("function-setup")
|
2009-08-03 18:16:12 +08:00
|
|
|
def test_func():
|
2018-11-22 16:15:14 +08:00
|
|
|
print("in function")
|
2009-08-03 18:16:12 +08:00
|
|
|
assert 0
|
|
|
|
def teardown_function(func):
|
2018-11-22 16:15:14 +08:00
|
|
|
print("in teardown")
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest(p)
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*test_func():*",
|
|
|
|
"*Captured stdout during setup*",
|
|
|
|
"module-setup*",
|
|
|
|
"function-setup*",
|
|
|
|
"*Captured stdout*",
|
|
|
|
"in teardown*",
|
|
|
|
]
|
|
|
|
)
|
2009-08-03 18:16:12 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_no_carry_over(self, pytester: Pytester) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2009-07-31 20:21:02 +08:00
|
|
|
def test_func1():
|
2018-11-22 16:15:14 +08:00
|
|
|
print("in func1")
|
2009-07-31 20:21:02 +08:00
|
|
|
def test_func2():
|
2018-11-22 16:15:14 +08:00
|
|
|
print("in func2")
|
2009-07-31 20:21:02 +08:00
|
|
|
assert 0
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest(p)
|
2009-07-31 20:21:02 +08:00
|
|
|
s = result.stdout.str()
|
2010-07-27 03:15:15 +08:00
|
|
|
assert "in func1" not in s
|
|
|
|
assert "in func2" in s
|
2009-07-31 20:21:02 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_teardown_capturing(self, pytester: Pytester) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2009-07-31 20:21:02 +08:00
|
|
|
def setup_function(function):
|
2018-11-22 16:15:14 +08:00
|
|
|
print("setup func1")
|
2009-07-31 20:21:02 +08:00
|
|
|
def teardown_function(function):
|
2018-11-22 16:15:14 +08:00
|
|
|
print("teardown func1")
|
2009-07-31 20:21:02 +08:00
|
|
|
assert 0
|
2010-07-27 03:15:15 +08:00
|
|
|
def test_func1():
|
2018-11-22 16:15:14 +08:00
|
|
|
print("in func1")
|
2009-07-31 20:21:02 +08:00
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest(p)
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*teardown_function*",
|
|
|
|
"*Captured stdout*",
|
|
|
|
"setup func1*",
|
|
|
|
"in func1*",
|
|
|
|
"teardown func1*",
|
|
|
|
# "*1 fixture failure*"
|
|
|
|
]
|
|
|
|
)
|
2009-07-31 20:21:02 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_teardown_capturing_final(self, pytester: Pytester) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2009-07-31 20:21:02 +08:00
|
|
|
def teardown_module(mod):
|
2018-11-22 16:15:14 +08:00
|
|
|
print("teardown module")
|
2009-07-31 20:21:02 +08:00
|
|
|
assert 0
|
|
|
|
def test_func():
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest(p)
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*def teardown_module(mod):*",
|
|
|
|
"*Captured stdout*",
|
|
|
|
"*teardown module*",
|
|
|
|
"*1 error*",
|
|
|
|
]
|
|
|
|
)
|
2009-07-31 20:21:02 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_capturing_outerr(self, pytester: Pytester) -> None:
|
|
|
|
p1 = pytester.makepyfile(
|
2018-08-24 00:06:17 +08:00
|
|
|
"""\
|
2010-07-27 03:15:15 +08:00
|
|
|
import sys
|
2009-07-31 20:21:02 +08:00
|
|
|
def test_capturing():
|
2018-11-22 16:15:14 +08:00
|
|
|
print(42)
|
2009-08-29 21:51:49 +08:00
|
|
|
sys.stderr.write(str(23))
|
2009-07-31 20:21:02 +08:00
|
|
|
def test_capturing_error():
|
2018-11-22 16:15:14 +08:00
|
|
|
print(1)
|
2009-08-29 21:51:49 +08:00
|
|
|
sys.stderr.write(str(2))
|
2009-07-31 20:21:02 +08:00
|
|
|
raise ValueError
|
2018-08-24 00:06:17 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest(p1)
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*test_capturing_outerr.py .F*",
|
|
|
|
"====* FAILURES *====",
|
|
|
|
"____*____",
|
|
|
|
"*test_capturing_outerr.py:8: ValueError",
|
|
|
|
"*--- Captured stdout *call*",
|
|
|
|
"1",
|
|
|
|
"*--- Captured stderr *call*",
|
|
|
|
"2",
|
|
|
|
]
|
|
|
|
)
|
2009-07-31 20:21:02 +08:00
|
|
|
|
2014-01-25 04:22:19 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestLoggingInteraction:
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_logging_stream_ownership(self, pytester: Pytester) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2018-08-24 00:06:17 +08:00
|
|
|
"""\
|
2009-07-31 20:21:02 +08:00
|
|
|
def test_logging():
|
|
|
|
import logging
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
2017-02-20 01:16:32 +08:00
|
|
|
stream = capture.CaptureIO()
|
2009-07-31 20:21:02 +08:00
|
|
|
logging.basicConfig(stream=stream)
|
|
|
|
stream.close() # to free memory/release resources
|
2018-08-24 00:06:17 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest_subprocess(p)
|
2017-07-21 06:14:54 +08:00
|
|
|
assert result.stderr.str().find("atexit") == -1
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_logging_and_immediate_setupteardown(self, pytester: Pytester) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2018-08-24 00:06:17 +08:00
|
|
|
"""\
|
2009-07-31 20:21:02 +08:00
|
|
|
import logging
|
|
|
|
def setup_function(function):
|
2018-12-03 19:52:33 +08:00
|
|
|
logging.warning("hello1")
|
2009-07-31 20:21:02 +08:00
|
|
|
|
|
|
|
def test_logging():
|
2018-12-03 19:52:33 +08:00
|
|
|
logging.warning("hello2")
|
2009-07-31 20:21:02 +08:00
|
|
|
assert 0
|
|
|
|
|
|
|
|
def teardown_function(function):
|
2018-12-03 19:52:33 +08:00
|
|
|
logging.warning("hello3")
|
2009-07-31 20:21:02 +08:00
|
|
|
assert 0
|
2018-08-24 00:06:17 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
|
|
|
for optargs in (("--capture=sys",), ("--capture=fd",)):
|
2017-07-21 06:14:54 +08:00
|
|
|
print(optargs)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest_subprocess(p, *optargs)
|
2009-07-31 20:21:02 +08:00
|
|
|
s = result.stdout.str()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
["*WARN*hello3", "*WARN*hello1", "*WARN*hello2"] # errors show first!
|
|
|
|
)
|
2009-07-31 20:21:02 +08:00
|
|
|
# verify proper termination
|
|
|
|
assert "closed" not in s
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_logging_and_crossscope_fixtures(self, pytester: Pytester) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2018-08-24 00:06:17 +08:00
|
|
|
"""\
|
2009-07-31 20:21:02 +08:00
|
|
|
import logging
|
|
|
|
def setup_module(function):
|
2018-12-03 19:52:33 +08:00
|
|
|
logging.warning("hello1")
|
2009-07-31 20:21:02 +08:00
|
|
|
|
|
|
|
def test_logging():
|
2018-12-03 19:52:33 +08:00
|
|
|
logging.warning("hello2")
|
2009-07-31 20:21:02 +08:00
|
|
|
assert 0
|
|
|
|
|
|
|
|
def teardown_module(function):
|
2018-12-03 19:52:33 +08:00
|
|
|
logging.warning("hello3")
|
2009-07-31 20:21:02 +08:00
|
|
|
assert 0
|
2018-08-24 00:06:17 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
|
|
|
for optargs in (("--capture=sys",), ("--capture=fd",)):
|
2017-07-21 06:14:54 +08:00
|
|
|
print(optargs)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest_subprocess(p, *optargs)
|
2009-07-31 20:21:02 +08:00
|
|
|
s = result.stdout.str()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
["*WARN*hello3", "*WARN*hello1", "*WARN*hello2"] # errors come first
|
|
|
|
)
|
2009-07-31 20:21:02 +08:00
|
|
|
# verify proper termination
|
|
|
|
assert "closed" not in s
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_conftestlogging_is_shown(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2018-08-24 00:06:17 +08:00
|
|
|
"""\
|
2011-01-18 19:51:21 +08:00
|
|
|
import logging
|
|
|
|
logging.basicConfig()
|
2018-12-03 19:52:33 +08:00
|
|
|
logging.warning("hello435")
|
2018-08-24 00:06:17 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2011-01-18 19:51:21 +08:00
|
|
|
# make sure that logging is still captured in tests
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest_subprocess("-s", "-p", "no:capturelog")
|
2019-06-07 18:58:51 +08:00
|
|
|
assert result.ret == ExitCode.NO_TESTS_COLLECTED
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stderr.fnmatch_lines(["WARNING*hello435*"])
|
|
|
|
assert "operation on closed file" not in result.stderr.str()
|
2011-01-18 19:51:21 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_conftestlogging_and_test_logging(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2018-08-24 00:06:17 +08:00
|
|
|
"""\
|
2011-01-18 19:51:21 +08:00
|
|
|
import logging
|
|
|
|
logging.basicConfig()
|
2018-08-24 00:06:17 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2011-01-18 19:51:21 +08:00
|
|
|
# make sure that logging is still captured in tests
|
2020-12-09 04:20:02 +08:00
|
|
|
p = pytester.makepyfile(
|
2018-08-24 00:06:17 +08:00
|
|
|
"""\
|
2011-01-18 19:51:21 +08:00
|
|
|
def test_hello():
|
|
|
|
import logging
|
2018-12-03 19:52:33 +08:00
|
|
|
logging.warning("hello433")
|
2011-01-18 19:51:21 +08:00
|
|
|
assert 0
|
2018-08-24 00:06:17 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest_subprocess(p, "-p", "no:capturelog")
|
2011-01-18 19:51:21 +08:00
|
|
|
assert result.ret != 0
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["WARNING*hello433*"])
|
|
|
|
assert "something" not in result.stderr.str()
|
|
|
|
assert "operation on closed file" not in result.stderr.str()
|
2011-01-18 19:51:21 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_logging_after_cap_stopped(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2018-12-03 20:12:26 +08:00
|
|
|
"""\
|
|
|
|
import pytest
|
|
|
|
import logging
|
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def log_on_teardown():
|
|
|
|
yield
|
|
|
|
log.warning('Logging on teardown')
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
# make sure that logging is still captured in tests
|
2020-12-09 04:20:02 +08:00
|
|
|
p = pytester.makepyfile(
|
2018-12-03 20:12:26 +08:00
|
|
|
"""\
|
|
|
|
def test_hello(log_on_teardown):
|
|
|
|
import logging
|
|
|
|
logging.warning("hello433")
|
|
|
|
assert 1
|
|
|
|
raise KeyboardInterrupt()
|
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest_subprocess(p, "--log-cli-level", "info")
|
2018-12-03 20:12:26 +08:00
|
|
|
assert result.ret != 0
|
2018-12-03 20:58:22 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
["*WARNING*hello433*", "*WARNING*Logging on teardown*"]
|
|
|
|
)
|
|
|
|
assert (
|
|
|
|
"AttributeError: 'NoneType' object has no attribute 'resume_capturing'"
|
|
|
|
not in result.stderr.str()
|
|
|
|
)
|
2018-12-03 20:12:26 +08:00
|
|
|
|
2010-12-06 23:56:12 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestCaptureFixture:
|
2014-07-28 19:17:37 +08:00
|
|
|
@pytest.mark.parametrize("opt", [[], ["-s"]])
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_std_functional(self, pytester: Pytester, opt) -> None:
|
|
|
|
reprec = pytester.inline_runsource(
|
2018-08-24 00:06:17 +08:00
|
|
|
"""\
|
2009-07-31 20:21:02 +08:00
|
|
|
def test_hello(capsys):
|
2018-11-22 16:15:14 +08:00
|
|
|
print(42)
|
2009-07-31 20:21:02 +08:00
|
|
|
out, err = capsys.readouterr()
|
|
|
|
assert out.startswith("42")
|
2018-08-24 00:06:17 +08:00
|
|
|
""",
|
2020-03-07 08:40:48 +08:00
|
|
|
*opt,
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2009-07-31 20:21:02 +08:00
|
|
|
reprec.assertoutcome(passed=1)
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_capsyscapfd(self, pytester: Pytester) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2018-08-24 00:06:17 +08:00
|
|
|
"""\
|
2012-06-04 03:01:27 +08:00
|
|
|
def test_one(capsys, capfd):
|
|
|
|
pass
|
|
|
|
def test_two(capfd, capsys):
|
|
|
|
pass
|
2018-08-24 00:06:17 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest(p)
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*ERROR*setup*test_one*",
|
|
|
|
"E*capfd*capsys*same*time*",
|
|
|
|
"*ERROR*setup*test_two*",
|
|
|
|
"E*capsys*capfd*same*time*",
|
2019-10-27 23:02:37 +08:00
|
|
|
"*2 errors*",
|
2018-05-23 22:48:46 +08:00
|
|
|
]
|
|
|
|
)
|
2012-06-04 03:01:27 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_capturing_getfixturevalue(self, pytester: Pytester) -> None:
|
2016-07-12 07:44:46 +08:00
|
|
|
"""Test that asking for "capfd" and "capsys" using request.getfixturevalue
|
|
|
|
in the same test is an error.
|
|
|
|
"""
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makepyfile(
|
2018-08-24 00:06:17 +08:00
|
|
|
"""\
|
2016-07-12 07:44:46 +08:00
|
|
|
def test_one(capsys, request):
|
|
|
|
request.getfixturevalue("capfd")
|
|
|
|
def test_two(capfd, request):
|
|
|
|
request.getfixturevalue("capsys")
|
2018-08-24 00:06:17 +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
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*test_one*",
|
2020-02-08 02:23:37 +08:00
|
|
|
"E * cannot use capfd and capsys at the same time",
|
2018-05-23 22:48:46 +08:00
|
|
|
"*test_two*",
|
2020-02-08 02:23:37 +08:00
|
|
|
"E * cannot use capsys and capfd at the same time",
|
2018-05-23 22:48:46 +08:00
|
|
|
"*2 failed in*",
|
|
|
|
]
|
|
|
|
)
|
2016-07-12 07:44:46 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_capsyscapfdbinary(self, pytester: Pytester) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2018-08-24 00:06:17 +08:00
|
|
|
"""\
|
2017-11-15 06:08:23 +08:00
|
|
|
def test_one(capsys, capfdbinary):
|
|
|
|
pass
|
2018-08-24 00:06:17 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest(p)
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
["*ERROR*setup*test_one*", "E*capfdbinary*capsys*same*time*", "*1 error*"]
|
|
|
|
)
|
2017-11-15 06:08:23 +08:00
|
|
|
|
2012-06-04 03:01:27 +08:00
|
|
|
@pytest.mark.parametrize("method", ["sys", "fd"])
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_capture_is_represented_on_failure_issue128(
|
|
|
|
self, pytester: Pytester, method
|
|
|
|
) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2018-08-24 00:06:17 +08:00
|
|
|
"""\
|
|
|
|
def test_hello(cap{}):
|
2018-11-22 16:15:14 +08:00
|
|
|
print("xxx42xxx")
|
2012-06-04 03:01:27 +08:00
|
|
|
assert 0
|
2018-08-24 00:06:17 +08:00
|
|
|
""".format(
|
|
|
|
method
|
|
|
|
)
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest(p)
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["xxx42xxx"])
|
2012-06-04 03:01:27 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_stdfd_functional(self, pytester: Pytester) -> None:
|
|
|
|
reprec = pytester.inline_runsource(
|
2018-08-24 00:06:17 +08:00
|
|
|
"""\
|
2009-07-31 20:21:02 +08:00
|
|
|
def test_hello(capfd):
|
|
|
|
import os
|
2019-06-05 08:48:06 +08:00
|
|
|
os.write(1, b"42")
|
2009-07-31 20:21:02 +08:00
|
|
|
out, err = capfd.readouterr()
|
|
|
|
assert out.startswith("42")
|
|
|
|
capfd.close()
|
2018-08-24 00:06:17 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2009-07-31 20:21:02 +08:00
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
2020-07-20 23:54:20 +08:00
|
|
|
@pytest.mark.parametrize("nl", ("\n", "\r\n", "\r"))
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_cafd_preserves_newlines(self, capfd, nl) -> None:
|
2020-07-20 23:54:20 +08:00
|
|
|
print("test", end=nl)
|
|
|
|
out, err = capfd.readouterr()
|
|
|
|
assert out.endswith(nl)
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_capfdbinary(self, pytester: Pytester) -> None:
|
|
|
|
reprec = pytester.inline_runsource(
|
2018-08-24 00:06:17 +08:00
|
|
|
"""\
|
2017-11-15 06:08:23 +08:00
|
|
|
def test_hello(capfdbinary):
|
|
|
|
import os
|
|
|
|
# some likely un-decodable bytes
|
|
|
|
os.write(1, b'\\xfe\\x98\\x20')
|
|
|
|
out, err = capfdbinary.readouterr()
|
|
|
|
assert out == b'\\xfe\\x98\\x20'
|
|
|
|
assert err == b''
|
2018-08-24 00:06:17 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2017-11-15 06:08:23 +08:00
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_capsysbinary(self, pytester: Pytester) -> None:
|
|
|
|
p1 = pytester.makepyfile(
|
2020-03-08 09:15:15 +08:00
|
|
|
r"""
|
2017-11-18 01:02:46 +08:00
|
|
|
def test_hello(capsysbinary):
|
|
|
|
import sys
|
2020-03-08 09:15:15 +08:00
|
|
|
|
|
|
|
sys.stdout.buffer.write(b'hello')
|
|
|
|
|
|
|
|
# Some likely un-decodable bytes.
|
|
|
|
sys.stdout.buffer.write(b'\xfe\x98\x20')
|
|
|
|
|
|
|
|
sys.stdout.buffer.flush()
|
|
|
|
|
|
|
|
# Ensure writing in text mode still works and is captured.
|
|
|
|
# https://github.com/pytest-dev/pytest/issues/6871
|
|
|
|
print("world", flush=True)
|
|
|
|
|
2017-11-18 01:02:46 +08:00
|
|
|
out, err = capsysbinary.readouterr()
|
2020-03-08 09:15:15 +08:00
|
|
|
assert out == b'hello\xfe\x98\x20world\n'
|
2017-11-18 01:02:46 +08:00
|
|
|
assert err == b''
|
2020-03-08 09:15:15 +08:00
|
|
|
|
|
|
|
print("stdout after")
|
|
|
|
print("stderr after", file=sys.stderr)
|
2018-08-24 00:06:17 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest(str(p1), "-rA")
|
2020-03-08 09:15:15 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*- Captured stdout call -*",
|
|
|
|
"stdout after",
|
|
|
|
"*- Captured stderr call -*",
|
|
|
|
"stderr after",
|
|
|
|
"*= 1 passed in *",
|
|
|
|
]
|
|
|
|
)
|
2017-11-18 01:02:46 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_partial_setup_failure(self, pytester: Pytester) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2018-08-24 00:06:17 +08:00
|
|
|
"""\
|
2010-05-18 15:54:04 +08:00
|
|
|
def test_hello(capsys, missingarg):
|
2009-07-31 20:21:02 +08:00
|
|
|
pass
|
2018-08-24 00:06:17 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest(p)
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*test_partial_setup_failure*", "*1 error*"])
|
2009-07-31 20:21:02 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_keyboardinterrupt_disables_capturing(self, pytester: Pytester) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2018-08-24 00:06:17 +08:00
|
|
|
"""\
|
2009-07-31 20:21:02 +08:00
|
|
|
def test_hello(capfd):
|
|
|
|
import os
|
2019-06-05 08:48:06 +08:00
|
|
|
os.write(1, b'42')
|
2009-07-31 20:21:02 +08:00
|
|
|
raise KeyboardInterrupt()
|
2018-08-24 00:06:17 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest_subprocess(p)
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*KeyboardInterrupt*"])
|
2009-07-31 20:21:02 +08:00
|
|
|
assert result.ret == 2
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_capture_and_logging(self, pytester: Pytester) -> None:
|
2019-04-27 22:25:37 +08:00
|
|
|
"""#14"""
|
2020-12-09 04:20:02 +08:00
|
|
|
p = pytester.makepyfile(
|
2018-08-24 00:06:17 +08:00
|
|
|
"""\
|
2013-03-25 15:53:08 +08:00
|
|
|
import logging
|
|
|
|
def test_log(capsys):
|
|
|
|
logging.error('x')
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest_subprocess(p)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert "closed" not in result.stderr.str()
|
2013-03-25 15:53:08 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.parametrize("fixture", ["capsys", "capfd"])
|
|
|
|
@pytest.mark.parametrize("no_capture", [True, False])
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_disabled_capture_fixture(
|
|
|
|
self, pytester: Pytester, fixture: str, no_capture: bool
|
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-08-24 00:06:17 +08:00
|
|
|
"""\
|
2016-06-09 07:21:17 +08:00
|
|
|
def test_disabled({fixture}):
|
|
|
|
print('captured before')
|
|
|
|
with {fixture}.disabled():
|
|
|
|
print('while capture is disabled')
|
|
|
|
print('captured after')
|
2017-09-30 04:24:31 +08:00
|
|
|
assert {fixture}.readouterr() == ('captured before\\ncaptured after\\n', '')
|
|
|
|
|
|
|
|
def test_normal():
|
|
|
|
print('test_normal executed')
|
2018-05-23 22:48:46 +08:00
|
|
|
""".format(
|
|
|
|
fixture=fixture
|
|
|
|
)
|
|
|
|
)
|
|
|
|
args = ("-s",) if no_capture else ()
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest_subprocess(*args)
|
2019-10-27 04:54:11 +08:00
|
|
|
result.stdout.fnmatch_lines(["*while capture is disabled*", "*= 2 passed in *"])
|
2019-10-06 01:18:51 +08:00
|
|
|
result.stdout.no_fnmatch_line("*captured before*")
|
|
|
|
result.stdout.no_fnmatch_line("*captured after*")
|
2017-09-30 04:24:31 +08:00
|
|
|
if no_capture:
|
2018-05-23 22:48:46 +08:00
|
|
|
assert "test_normal executed" in result.stdout.str()
|
2017-09-30 04:24:31 +08:00
|
|
|
else:
|
2019-10-06 01:18:51 +08:00
|
|
|
result.stdout.no_fnmatch_line("*test_normal executed*")
|
2016-06-09 07:21:17 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_disabled_capture_fixture_twice(self, pytester: Pytester) -> None:
|
2020-08-16 00:16:59 +08:00
|
|
|
"""Test that an inner disabled() exit doesn't undo an outer disabled().
|
|
|
|
|
|
|
|
Issue #7148.
|
|
|
|
"""
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makepyfile(
|
2020-08-16 00:16:59 +08:00
|
|
|
"""
|
|
|
|
def test_disabled(capfd):
|
|
|
|
print('captured before')
|
|
|
|
with capfd.disabled():
|
|
|
|
print('while capture is disabled 1')
|
|
|
|
with capfd.disabled():
|
|
|
|
print('while capture is disabled 2')
|
|
|
|
print('while capture is disabled 1 after')
|
|
|
|
print('captured after')
|
|
|
|
assert capfd.readouterr() == ('captured before\\ncaptured after\\n', '')
|
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest_subprocess()
|
2020-08-16 00:16:59 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*while capture is disabled 1",
|
|
|
|
"*while capture is disabled 2",
|
|
|
|
"*while capture is disabled 1 after",
|
|
|
|
],
|
|
|
|
consecutive=True,
|
|
|
|
)
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.parametrize("fixture", ["capsys", "capfd"])
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_fixture_use_by_other_fixtures(self, pytester: Pytester, fixture) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""Ensure that capsys and capfd can be used by other fixtures during
|
|
|
|
setup and teardown."""
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makepyfile(
|
2018-08-24 00:06:17 +08:00
|
|
|
"""\
|
2017-09-26 13:34:41 +08:00
|
|
|
import sys
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def captured_print({fixture}):
|
|
|
|
print('stdout contents begin')
|
|
|
|
print('stderr contents begin', file=sys.stderr)
|
|
|
|
out, err = {fixture}.readouterr()
|
|
|
|
|
|
|
|
yield out, err
|
|
|
|
|
|
|
|
print('stdout contents end')
|
|
|
|
print('stderr contents end', file=sys.stderr)
|
|
|
|
out, err = {fixture}.readouterr()
|
|
|
|
assert out == 'stdout contents end\\n'
|
|
|
|
assert err == 'stderr contents end\\n'
|
|
|
|
|
|
|
|
def test_captured_print(captured_print):
|
|
|
|
out, err = captured_print
|
|
|
|
assert out == 'stdout contents begin\\n'
|
|
|
|
assert err == 'stderr contents begin\\n'
|
2018-05-23 22:48:46 +08:00
|
|
|
""".format(
|
|
|
|
fixture=fixture
|
|
|
|
)
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest_subprocess()
|
2019-03-23 18:36:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
2019-10-06 01:18:51 +08:00
|
|
|
result.stdout.no_fnmatch_line("*stdout contents begin*")
|
|
|
|
result.stdout.no_fnmatch_line("*stderr contents begin*")
|
2017-09-26 13:34:41 +08:00
|
|
|
|
2018-08-19 01:32:10 +08:00
|
|
|
@pytest.mark.parametrize("cap", ["capsys", "capfd"])
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_fixture_use_by_other_fixtures_teardown(
|
|
|
|
self, pytester: Pytester, cap
|
|
|
|
) -> None:
|
2018-08-19 01:32:10 +08:00
|
|
|
"""Ensure we can access setup and teardown buffers from teardown when using capsys/capfd (##3033)"""
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makepyfile(
|
2018-08-24 00:06:17 +08:00
|
|
|
"""\
|
2018-08-19 01:32:10 +08:00
|
|
|
import sys
|
|
|
|
import pytest
|
|
|
|
import os
|
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
|
def fix({cap}):
|
|
|
|
print("setup out")
|
|
|
|
sys.stderr.write("setup err\\n")
|
|
|
|
yield
|
|
|
|
out, err = {cap}.readouterr()
|
|
|
|
assert out == 'setup out\\ncall out\\n'
|
|
|
|
assert err == 'setup err\\ncall err\\n'
|
|
|
|
|
|
|
|
def test_a(fix):
|
|
|
|
print("call out")
|
|
|
|
sys.stderr.write("call err\\n")
|
|
|
|
""".format(
|
|
|
|
cap=cap
|
|
|
|
)
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
reprec = pytester.inline_run()
|
2018-08-19 01:32:10 +08:00
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
2013-03-25 15:53:08 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_setup_failure_does_not_kill_capturing(pytester: Pytester) -> None:
|
|
|
|
sub1 = pytester.mkpydir("sub1")
|
|
|
|
sub1.joinpath("conftest.py").write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
def pytest_runtest_setup(item):
|
|
|
|
raise ValueError(42)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
sub1.joinpath("test_mod.py").write_text("def test_func1(): pass")
|
|
|
|
result = pytester.runpytest(pytester.path, "--traceconfig")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*ValueError(42)*", "*1 error*"])
|
2009-12-30 18:16:20 +08:00
|
|
|
|
2014-01-25 04:22:19 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_capture_conftest_runtest_setup(pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2010-11-22 06:17:59 +08:00
|
|
|
def pytest_runtest_setup():
|
2018-11-22 16:15:14 +08:00
|
|
|
print("hello19")
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makepyfile("def test_func(): pass")
|
|
|
|
result = pytester.runpytest()
|
2010-11-22 06:17:59 +08:00
|
|
|
assert result.ret == 0
|
2019-10-06 01:18:51 +08:00
|
|
|
result.stdout.no_fnmatch_line("*hello19*")
|
2013-09-30 19:14:16 +08:00
|
|
|
|
2014-01-25 04:22:19 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_capture_badoutput_issue412(pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2014-03-27 20:53:59 +08:00
|
|
|
import os
|
|
|
|
|
|
|
|
def test_func():
|
|
|
|
omg = bytearray([1,129,1])
|
|
|
|
os.write(1, omg)
|
|
|
|
assert 0
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest("--capture=fd")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
"""
|
2014-03-28 16:46:38 +08:00
|
|
|
*def test_func*
|
|
|
|
*assert 0*
|
|
|
|
*Captured*
|
|
|
|
*1 failed*
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2014-03-27 20:53:59 +08:00
|
|
|
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_capture_early_option_parsing(pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2013-09-30 19:14:16 +08:00
|
|
|
def pytest_runtest_setup():
|
2018-11-22 16:15:14 +08:00
|
|
|
print("hello19")
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makepyfile("def test_func(): pass")
|
|
|
|
result = pytester.runpytest("-vs")
|
2013-09-30 19:14:16 +08:00
|
|
|
assert result.ret == 0
|
2018-05-23 22:48:46 +08:00
|
|
|
assert "hello19" in result.stdout.str()
|
2014-01-23 01:07:54 +08:00
|
|
|
|
2014-01-25 04:22:19 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_capture_binary_output(pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
r"""
|
2014-01-23 01:07:54 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
def test_a():
|
|
|
|
import sys
|
|
|
|
import subprocess
|
|
|
|
subprocess.call([sys.executable, __file__])
|
|
|
|
|
|
|
|
def test_foo():
|
|
|
|
import os;os.write(1, b'\xc3')
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
test_foo()
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest("--assert=plain")
|
2015-04-28 17:54:53 +08:00
|
|
|
result.assert_outcomes(passed=2)
|
2014-01-23 05:18:33 +08:00
|
|
|
|
2014-01-25 04:22:19 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_error_during_readouterr(pytester: Pytester) -> None:
|
2017-02-15 23:00:18 +08:00
|
|
|
"""Make sure we suspend capturing if errors occur during readouterr"""
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
pytest_xyz="""
|
2015-07-26 07:15:51 +08:00
|
|
|
from _pytest.capture import FDCapture
|
2019-04-11 03:56:44 +08:00
|
|
|
|
2015-07-26 07:15:51 +08:00
|
|
|
def bad_snap(self):
|
|
|
|
raise Exception('boom')
|
2019-04-11 03:56:44 +08:00
|
|
|
|
2015-07-26 07:28:00 +08:00
|
|
|
assert FDCapture.snap
|
2015-07-26 07:15:51 +08:00
|
|
|
FDCapture.snap = bad_snap
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest_subprocess("-p", "pytest_xyz", "--version")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stderr.fnmatch_lines(
|
|
|
|
["*in bad_snap", " raise Exception('boom')", "Exception: boom"]
|
|
|
|
)
|
2015-07-26 07:15:51 +08:00
|
|
|
|
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestCaptureIO:
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_text(self) -> None:
|
2017-02-20 01:16:32 +08:00
|
|
|
f = capture.CaptureIO()
|
2014-01-25 04:22:19 +08:00
|
|
|
f.write("hello")
|
|
|
|
s = f.getvalue()
|
|
|
|
assert s == "hello"
|
|
|
|
f.close()
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_unicode_and_str_mixture(self) -> None:
|
2017-02-20 01:16:32 +08:00
|
|
|
f = capture.CaptureIO()
|
2019-05-28 07:31:52 +08:00
|
|
|
f.write("\u00f6")
|
|
|
|
pytest.raises(TypeError, f.write, b"hello")
|
2014-01-25 04:22:19 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_write_bytes_to_buffer(self) -> None:
|
2017-02-20 01:16:32 +08:00
|
|
|
"""In python3, stdout / stderr are text io wrappers (exposing a buffer
|
|
|
|
property of the underlying bytestream). See issue #1407
|
|
|
|
"""
|
|
|
|
f = capture.CaptureIO()
|
2018-05-23 22:48:46 +08:00
|
|
|
f.buffer.write(b"foo\r\n")
|
|
|
|
assert f.getvalue() == "foo\r\n"
|
2017-02-20 01:16:32 +08:00
|
|
|
|
2014-01-25 04:22:19 +08:00
|
|
|
|
2020-02-04 16:49:11 +08:00
|
|
|
class TestTeeCaptureIO(TestCaptureIO):
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_text(self) -> None:
|
2019-12-04 06:15:13 +08:00
|
|
|
sio = io.StringIO()
|
2020-02-04 16:49:11 +08:00
|
|
|
f = capture.TeeCaptureIO(sio)
|
2019-12-04 06:15:13 +08:00
|
|
|
f.write("hello")
|
|
|
|
s1 = f.getvalue()
|
|
|
|
assert s1 == "hello"
|
|
|
|
s2 = sio.getvalue()
|
|
|
|
assert s2 == s1
|
|
|
|
f.close()
|
|
|
|
sio.close()
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_unicode_and_str_mixture(self) -> None:
|
2019-12-04 06:15:13 +08:00
|
|
|
sio = io.StringIO()
|
2020-02-04 16:49:11 +08:00
|
|
|
f = capture.TeeCaptureIO(sio)
|
2019-12-04 06:15:13 +08:00
|
|
|
f.write("\u00f6")
|
|
|
|
pytest.raises(TypeError, f.write, b"hello")
|
|
|
|
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_dontreadfrominput() -> None:
|
2014-01-25 04:22:19 +08:00
|
|
|
from _pytest.capture import DontReadFromInput
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2014-01-25 04:22:19 +08:00
|
|
|
f = DontReadFromInput()
|
2019-10-28 02:30:41 +08:00
|
|
|
assert f.buffer is f
|
2014-01-25 04:22:19 +08:00
|
|
|
assert not f.isatty()
|
2020-03-27 23:40:23 +08:00
|
|
|
pytest.raises(OSError, f.read)
|
|
|
|
pytest.raises(OSError, f.readlines)
|
2018-03-16 09:25:50 +08:00
|
|
|
iter_f = iter(f)
|
2020-03-27 23:40:23 +08:00
|
|
|
pytest.raises(OSError, next, iter_f)
|
2017-03-15 03:45:56 +08:00
|
|
|
pytest.raises(UnsupportedOperation, f.fileno)
|
2022-07-31 21:44:02 +08:00
|
|
|
pytest.raises(UnsupportedOperation, f.flush)
|
|
|
|
assert not f.readable()
|
|
|
|
pytest.raises(UnsupportedOperation, f.seek, 0)
|
|
|
|
assert not f.seekable()
|
|
|
|
pytest.raises(UnsupportedOperation, f.tell)
|
|
|
|
pytest.raises(UnsupportedOperation, f.truncate, 0)
|
|
|
|
pytest.raises(UnsupportedOperation, f.write, b"")
|
|
|
|
pytest.raises(UnsupportedOperation, f.writelines, [])
|
|
|
|
assert not f.writable()
|
2014-01-25 04:22:19 +08:00
|
|
|
f.close() # just for completeness
|
2016-08-25 06:44:43 +08:00
|
|
|
|
|
|
|
|
2020-08-10 02:20:07 +08:00
|
|
|
def test_captureresult() -> None:
|
|
|
|
cr = CaptureResult("out", "err")
|
|
|
|
assert len(cr) == 2
|
|
|
|
assert cr.out == "out"
|
|
|
|
assert cr.err == "err"
|
|
|
|
out, err = cr
|
|
|
|
assert out == "out"
|
|
|
|
assert err == "err"
|
|
|
|
assert cr[0] == "out"
|
|
|
|
assert cr[1] == "err"
|
|
|
|
assert cr == cr
|
|
|
|
assert cr == CaptureResult("out", "err")
|
|
|
|
assert cr != CaptureResult("wrong", "err")
|
|
|
|
assert cr == ("out", "err")
|
|
|
|
assert cr != ("out", "wrong")
|
|
|
|
assert hash(cr) == hash(CaptureResult("out", "err"))
|
|
|
|
assert hash(cr) == hash(("out", "err"))
|
|
|
|
assert hash(cr) != hash(("out", "wrong"))
|
|
|
|
assert cr < ("z",)
|
|
|
|
assert cr < ("z", "b")
|
|
|
|
assert cr < ("z", "b", "c")
|
|
|
|
assert cr.count("err") == 1
|
|
|
|
assert cr.count("wrong") == 0
|
|
|
|
assert cr.index("err") == 1
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
assert cr.index("wrong") == 0
|
|
|
|
assert next(iter(cr)) == "out"
|
|
|
|
assert cr._replace(err="replaced") == ("out", "replaced")
|
|
|
|
|
|
|
|
|
2019-05-28 07:31:52 +08:00
|
|
|
@pytest.fixture
|
2020-12-09 04:20:02 +08:00
|
|
|
def tmpfile(pytester: Pytester) -> Generator[BinaryIO, None, None]:
|
|
|
|
f = pytester.makepyfile("").open("wb+")
|
2014-01-29 18:18:15 +08:00
|
|
|
yield f
|
|
|
|
if not f.closed:
|
|
|
|
f.close()
|
2014-01-25 04:22:19 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2014-01-29 18:18:15 +08:00
|
|
|
@contextlib.contextmanager
|
|
|
|
def lsof_check():
|
2014-01-25 04:22:19 +08:00
|
|
|
pid = os.getpid()
|
|
|
|
try:
|
2019-01-22 11:46:57 +08:00
|
|
|
out = subprocess.check_output(("lsof", "-p", str(pid))).decode()
|
2019-10-29 22:18:29 +08:00
|
|
|
except (OSError, subprocess.CalledProcessError, UnicodeDecodeError) as exc:
|
2015-07-25 06:21:57 +08:00
|
|
|
# about UnicodeDecodeError, see note on pytester
|
2020-10-03 04:16:22 +08:00
|
|
|
pytest.skip(f"could not run 'lsof' ({exc!r})")
|
2014-01-29 18:18:15 +08:00
|
|
|
yield
|
2019-01-22 11:46:57 +08:00
|
|
|
out2 = subprocess.check_output(("lsof", "-p", str(pid))).decode()
|
2014-01-25 04:22:19 +08:00
|
|
|
len1 = len([x for x in out.split("\n") if "REG" in x])
|
|
|
|
len2 = len([x for x in out2.split("\n") if "REG" in x])
|
|
|
|
assert len2 < len1 + 3, out2
|
|
|
|
|
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestFDCapture:
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_simple(self, tmpfile: BinaryIO) -> None:
|
2014-01-25 04:22:19 +08:00
|
|
|
fd = tmpfile.fileno()
|
2014-01-26 02:42:45 +08:00
|
|
|
cap = capture.FDCapture(fd)
|
2018-11-05 04:59:28 +08:00
|
|
|
data = b"hello"
|
2014-01-25 04:22:19 +08:00
|
|
|
os.write(fd, data)
|
2020-04-17 15:01:51 +08:00
|
|
|
pytest.raises(AssertionError, cap.snap)
|
2014-03-28 14:11:25 +08:00
|
|
|
cap.done()
|
2014-01-25 04:22:19 +08:00
|
|
|
cap = capture.FDCapture(fd)
|
2014-01-26 02:42:45 +08:00
|
|
|
cap.start()
|
2014-01-25 04:22:19 +08:00
|
|
|
os.write(fd, data)
|
2014-03-28 14:11:25 +08:00
|
|
|
s = cap.snap()
|
|
|
|
cap.done()
|
2014-01-25 04:22:19 +08:00
|
|
|
assert s == "hello"
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_simple_many(self, tmpfile: BinaryIO) -> None:
|
2014-01-25 04:22:19 +08:00
|
|
|
for i in range(10):
|
|
|
|
self.test_simple(tmpfile)
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_simple_many_check_open_files(self, pytester: Pytester) -> None:
|
2014-01-29 18:18:15 +08:00
|
|
|
with lsof_check():
|
2020-12-09 04:20:02 +08:00
|
|
|
with pytester.makepyfile("").open("wb+") as tmpfile:
|
2014-01-29 18:18:15 +08:00
|
|
|
self.test_simple_many(tmpfile)
|
2014-01-25 04:22:19 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_simple_fail_second_start(self, tmpfile: BinaryIO) -> None:
|
2014-01-25 04:22:19 +08:00
|
|
|
fd = tmpfile.fileno()
|
|
|
|
cap = capture.FDCapture(fd)
|
2014-03-28 14:11:25 +08:00
|
|
|
cap.done()
|
2020-04-17 15:01:51 +08:00
|
|
|
pytest.raises(AssertionError, cap.start)
|
2014-01-25 04:22:19 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_stderr(self) -> None:
|
2014-03-28 14:03:37 +08:00
|
|
|
cap = capture.FDCapture(2)
|
2014-01-26 02:42:45 +08:00
|
|
|
cap.start()
|
2017-03-17 09:27:28 +08:00
|
|
|
print("hello", file=sys.stderr)
|
2014-03-28 14:11:25 +08:00
|
|
|
s = cap.snap()
|
|
|
|
cap.done()
|
2014-01-25 04:22:19 +08:00
|
|
|
assert s == "hello\n"
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_stdin(self) -> None:
|
2014-04-01 20:19:55 +08:00
|
|
|
cap = capture.FDCapture(0)
|
2014-01-26 02:42:45 +08:00
|
|
|
cap.start()
|
2014-01-25 04:22:19 +08:00
|
|
|
x = os.read(0, 100).strip()
|
|
|
|
cap.done()
|
2018-11-05 04:59:28 +08:00
|
|
|
assert x == b""
|
2014-01-25 04:22:19 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_writeorg(self, tmpfile: BinaryIO) -> None:
|
2018-11-05 04:59:28 +08:00
|
|
|
data1, data2 = b"foo", b"bar"
|
2014-04-02 18:32:21 +08:00
|
|
|
cap = capture.FDCapture(tmpfile.fileno())
|
|
|
|
cap.start()
|
|
|
|
tmpfile.write(data1)
|
|
|
|
tmpfile.flush()
|
2020-03-08 09:15:15 +08:00
|
|
|
cap.writeorg(data2.decode("ascii"))
|
2014-03-28 14:11:25 +08:00
|
|
|
scap = cap.snap()
|
|
|
|
cap.done()
|
2018-11-05 04:59:28 +08:00
|
|
|
assert scap == data1.decode("ascii")
|
2018-05-23 22:48:46 +08:00
|
|
|
with open(tmpfile.name, "rb") as stmp_file:
|
2014-03-31 23:25:35 +08:00
|
|
|
stmp = stmp_file.read()
|
|
|
|
assert stmp == data2
|
2014-01-25 04:22:19 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_simple_resume_suspend(self) -> None:
|
2014-04-01 20:19:55 +08:00
|
|
|
with saved_fd(1):
|
|
|
|
cap = capture.FDCapture(1)
|
|
|
|
cap.start()
|
2018-11-05 04:59:28 +08:00
|
|
|
data = b"hello"
|
2014-04-01 20:19:55 +08:00
|
|
|
os.write(1, data)
|
|
|
|
sys.stdout.write("whatever")
|
|
|
|
s = cap.snap()
|
|
|
|
assert s == "hellowhatever"
|
|
|
|
cap.suspend()
|
2018-11-05 04:59:28 +08:00
|
|
|
os.write(1, b"world")
|
2014-04-01 20:19:55 +08:00
|
|
|
sys.stdout.write("qlwkej")
|
|
|
|
assert not cap.snap()
|
|
|
|
cap.resume()
|
2018-11-05 04:59:28 +08:00
|
|
|
os.write(1, b"but now")
|
2014-04-01 20:19:55 +08:00
|
|
|
sys.stdout.write(" yes\n")
|
|
|
|
s = cap.snap()
|
|
|
|
assert s == "but now yes\n"
|
|
|
|
cap.suspend()
|
|
|
|
cap.done()
|
2020-04-17 15:01:51 +08:00
|
|
|
pytest.raises(AssertionError, cap.suspend)
|
2014-04-01 20:19:55 +08:00
|
|
|
|
2020-02-20 07:51:57 +08:00
|
|
|
assert repr(cap) == (
|
Perform FD capturing even if the FD is invalid
The `FDCapture`/`FDCaptureBinary` classes, used by `capfd`/`capfdbinary`
fixtures and the `--capture=fd` option (set by default), redirect FDs
1/2 (stdout/stderr) to a temporary file. To do this, they need to save
the old file by duplicating the FD before redirecting it, to be restored
once finished.
Previously, if this duplicating (`os.dup()`) failed, most likely due to
that FD being invalid, the FD redirection would silently not be done. The
FD capturing also performs python-level redirection (monkeypatching
`sys.stdout`/`sys.stderr`) which would still be done, but direct writes
to the FDs would fail.
This is not great. If pytest is run with `--capture=fd`, or a test is
using `capfd`, it expects writes to the FD to work and be captured,
regardless of external circumstances.
So, instead of disabling FD capturing, keep the redirection to a
temporary file, just don't restore it after closing, because there is
nothing to restore to.
2020-03-16 05:32:59 +08:00
|
|
|
"<FDCapture 1 oldfd={} _state='done' tmpfile={!r}>".format(
|
|
|
|
cap.targetfd_save, cap.tmpfile
|
2020-02-20 07:51:57 +08:00
|
|
|
)
|
|
|
|
)
|
|
|
|
# Should not crash with missing "_old".
|
|
|
|
assert repr(cap.syscapture) == (
|
|
|
|
"<SysCapture stdout _old=<UNSET> _state='done' tmpfile={!r}>".format(
|
|
|
|
cap.syscapture.tmpfile
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_capfd_sys_stdout_mode(self, capfd) -> None:
|
2019-05-15 05:54:03 +08:00
|
|
|
assert "b" not in sys.stdout.mode
|
|
|
|
|
2014-09-22 20:56:07 +08:00
|
|
|
|
2014-04-01 20:19:55 +08:00
|
|
|
@contextlib.contextmanager
|
|
|
|
def saved_fd(fd):
|
|
|
|
new_fd = os.dup(fd)
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
os.dup2(new_fd, fd)
|
2014-09-22 20:56:07 +08:00
|
|
|
os.close(new_fd)
|
2014-01-25 04:22:19 +08:00
|
|
|
|
2014-06-16 17:27:32 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestStdCapture:
|
2014-03-28 14:55:07 +08:00
|
|
|
captureclass = staticmethod(StdCapture)
|
|
|
|
|
|
|
|
@contextlib.contextmanager
|
2014-01-25 04:22:19 +08:00
|
|
|
def getcapture(self, **kw):
|
2014-03-28 14:55:07 +08:00
|
|
|
cap = self.__class__.captureclass(**kw)
|
2014-03-14 19:49:36 +08:00
|
|
|
cap.start_capturing()
|
2014-03-28 14:55:07 +08:00
|
|
|
try:
|
|
|
|
yield cap
|
|
|
|
finally:
|
|
|
|
cap.stop_capturing()
|
2014-01-25 04:22:19 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_capturing_done_simple(self) -> None:
|
2014-03-28 14:55:07 +08:00
|
|
|
with self.getcapture() as cap:
|
|
|
|
sys.stdout.write("hello")
|
|
|
|
sys.stderr.write("world")
|
|
|
|
out, err = cap.readouterr()
|
2014-03-28 14:11:25 +08:00
|
|
|
assert out == "hello"
|
|
|
|
assert err == "world"
|
2014-01-25 04:22:19 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_capturing_reset_simple(self) -> None:
|
2014-03-28 14:55:07 +08:00
|
|
|
with self.getcapture() as cap:
|
|
|
|
print("hello world")
|
|
|
|
sys.stderr.write("hello error\n")
|
|
|
|
out, err = cap.readouterr()
|
2014-01-25 04:22:19 +08:00
|
|
|
assert out == "hello world\n"
|
|
|
|
assert err == "hello error\n"
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_capturing_readouterr(self) -> None:
|
2014-03-28 14:55:07 +08:00
|
|
|
with self.getcapture() as cap:
|
2017-07-21 06:14:54 +08:00
|
|
|
print("hello world")
|
2014-01-25 04:22:19 +08:00
|
|
|
sys.stderr.write("hello error\n")
|
|
|
|
out, err = cap.readouterr()
|
|
|
|
assert out == "hello world\n"
|
|
|
|
assert err == "hello error\n"
|
|
|
|
sys.stderr.write("error2")
|
2014-03-28 14:11:25 +08:00
|
|
|
out, err = cap.readouterr()
|
2014-01-25 04:22:19 +08:00
|
|
|
assert err == "error2"
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_capture_results_accessible_by_attribute(self) -> None:
|
2017-10-29 02:53:19 +08:00
|
|
|
with self.getcapture() as cap:
|
|
|
|
sys.stdout.write("hello")
|
|
|
|
sys.stderr.write("world")
|
|
|
|
capture_result = cap.readouterr()
|
|
|
|
assert capture_result.out == "hello"
|
|
|
|
assert capture_result.err == "world"
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_capturing_readouterr_unicode(self) -> None:
|
2014-03-28 14:55:07 +08:00
|
|
|
with self.getcapture() as cap:
|
2018-08-23 10:21:00 +08:00
|
|
|
print("hxąć")
|
2014-01-25 04:22:19 +08:00
|
|
|
out, err = cap.readouterr()
|
2019-06-03 06:32:00 +08:00
|
|
|
assert out == "hxąć\n"
|
2014-01-25 04:22:19 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_reset_twice_error(self) -> None:
|
2014-03-28 14:55:07 +08:00
|
|
|
with self.getcapture() as cap:
|
2017-07-21 06:14:54 +08:00
|
|
|
print("hello")
|
2014-03-28 14:55:07 +08:00
|
|
|
out, err = cap.readouterr()
|
2014-03-28 14:11:25 +08:00
|
|
|
pytest.raises(ValueError, cap.stop_capturing)
|
2014-01-25 04:22:19 +08:00
|
|
|
assert out == "hello\n"
|
|
|
|
assert not err
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_capturing_modify_sysouterr_in_between(self) -> None:
|
2014-01-25 04:22:19 +08:00
|
|
|
oldout = sys.stdout
|
|
|
|
olderr = sys.stderr
|
2014-03-28 14:55:07 +08:00
|
|
|
with self.getcapture() as cap:
|
|
|
|
sys.stdout.write("hello")
|
|
|
|
sys.stderr.write("world")
|
2017-02-20 01:16:32 +08:00
|
|
|
sys.stdout = capture.CaptureIO()
|
|
|
|
sys.stderr = capture.CaptureIO()
|
2017-07-21 06:14:54 +08:00
|
|
|
print("not seen")
|
2014-03-28 14:55:07 +08:00
|
|
|
sys.stderr.write("not seen\n")
|
|
|
|
out, err = cap.readouterr()
|
2014-01-25 04:22:19 +08:00
|
|
|
assert out == "hello"
|
|
|
|
assert err == "world"
|
|
|
|
assert sys.stdout == oldout
|
|
|
|
assert sys.stderr == olderr
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_capturing_error_recursive(self) -> None:
|
2014-03-28 14:55:07 +08:00
|
|
|
with self.getcapture() as cap1:
|
2017-07-21 06:14:54 +08:00
|
|
|
print("cap1")
|
2014-03-28 14:55:07 +08:00
|
|
|
with self.getcapture() as cap2:
|
2017-07-21 06:14:54 +08:00
|
|
|
print("cap2")
|
2014-03-28 14:55:07 +08:00
|
|
|
out2, err2 = cap2.readouterr()
|
|
|
|
out1, err1 = cap1.readouterr()
|
2014-01-25 04:22:19 +08:00
|
|
|
assert out1 == "cap1\n"
|
|
|
|
assert out2 == "cap2\n"
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_just_out_capture(self) -> None:
|
2014-03-28 14:55:07 +08:00
|
|
|
with self.getcapture(out=True, err=False) as cap:
|
|
|
|
sys.stdout.write("hello")
|
|
|
|
sys.stderr.write("world")
|
|
|
|
out, err = cap.readouterr()
|
2014-01-25 04:22:19 +08:00
|
|
|
assert out == "hello"
|
|
|
|
assert not err
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_just_err_capture(self) -> None:
|
2014-03-28 14:55:07 +08:00
|
|
|
with self.getcapture(out=False, err=True) as cap:
|
|
|
|
sys.stdout.write("hello")
|
|
|
|
sys.stderr.write("world")
|
|
|
|
out, err = cap.readouterr()
|
2014-01-25 04:22:19 +08:00
|
|
|
assert err == "world"
|
|
|
|
assert not out
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_stdin_restored(self) -> None:
|
2014-01-25 04:22:19 +08:00
|
|
|
old = sys.stdin
|
2014-04-03 00:48:08 +08:00
|
|
|
with self.getcapture(in_=True):
|
2014-03-28 14:55:07 +08:00
|
|
|
newstdin = sys.stdin
|
2014-01-25 04:22:19 +08:00
|
|
|
assert newstdin != sys.stdin
|
|
|
|
assert sys.stdin is old
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_stdin_nulled_by_default(self) -> None:
|
2017-07-21 06:14:54 +08:00
|
|
|
print("XXX this test may well hang instead of crashing")
|
|
|
|
print("XXX which indicates an error in the underlying capturing")
|
|
|
|
print("XXX mechanisms")
|
2014-04-03 00:48:08 +08:00
|
|
|
with self.getcapture():
|
2020-03-27 23:40:23 +08:00
|
|
|
pytest.raises(OSError, sys.stdin.read)
|
2014-01-25 04:22:19 +08:00
|
|
|
|
|
|
|
|
2019-12-04 06:15:13 +08:00
|
|
|
class TestTeeStdCapture(TestStdCapture):
|
|
|
|
captureclass = staticmethod(TeeStdCapture)
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_capturing_error_recursive(self) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
r"""For TeeStdCapture since we passthrough stderr/stdout, cap1
|
|
|
|
should get all output, while cap2 should only get "cap2\n"."""
|
2019-12-04 06:15:13 +08:00
|
|
|
|
|
|
|
with self.getcapture() as cap1:
|
|
|
|
print("cap1")
|
|
|
|
with self.getcapture() as cap2:
|
|
|
|
print("cap2")
|
|
|
|
out2, err2 = cap2.readouterr()
|
|
|
|
out1, err1 = cap1.readouterr()
|
|
|
|
assert out1 == "cap1\ncap2\n"
|
|
|
|
assert out2 == "cap2\n"
|
|
|
|
|
|
|
|
|
2014-01-25 04:22:19 +08:00
|
|
|
class TestStdCaptureFD(TestStdCapture):
|
2014-03-28 14:55:07 +08:00
|
|
|
captureclass = staticmethod(StdCaptureFD)
|
2014-01-25 04:22:19 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_simple_only_fd(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2019-06-05 08:48:06 +08:00
|
|
|
"""\
|
2014-03-28 14:11:25 +08:00
|
|
|
import os
|
|
|
|
def test_x():
|
2019-06-05 08:48:06 +08:00
|
|
|
os.write(1, b"hello\\n")
|
2014-03-28 14:11:25 +08:00
|
|
|
assert 0
|
2019-06-05 08:48:06 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest_subprocess()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
"""
|
2014-03-28 14:11:25 +08:00
|
|
|
*test_x*
|
|
|
|
*assert 0*
|
|
|
|
*Captured stdout*
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2014-01-25 04:22:19 +08:00
|
|
|
|
|
|
|
def test_intermingling(self):
|
2014-03-28 14:55:07 +08:00
|
|
|
with self.getcapture() as cap:
|
2018-11-05 04:59:28 +08:00
|
|
|
os.write(1, b"1")
|
2014-03-28 14:55:07 +08:00
|
|
|
sys.stdout.write(str(2))
|
|
|
|
sys.stdout.flush()
|
2018-11-05 04:59:28 +08:00
|
|
|
os.write(1, b"3")
|
|
|
|
os.write(2, b"a")
|
2014-03-28 14:55:07 +08:00
|
|
|
sys.stderr.write("b")
|
|
|
|
sys.stderr.flush()
|
2018-11-05 04:59:28 +08:00
|
|
|
os.write(2, b"c")
|
2014-03-28 14:55:07 +08:00
|
|
|
out, err = cap.readouterr()
|
2014-01-25 04:22:19 +08:00
|
|
|
assert out == "123"
|
|
|
|
assert err == "abc"
|
|
|
|
|
|
|
|
def test_many(self, capfd):
|
2014-01-29 18:18:15 +08:00
|
|
|
with lsof_check():
|
2014-01-25 04:22:19 +08:00
|
|
|
for i in range(10):
|
2014-03-28 14:03:37 +08:00
|
|
|
cap = StdCaptureFD()
|
2020-04-17 15:01:51 +08:00
|
|
|
cap.start_capturing()
|
2014-03-28 14:11:25 +08:00
|
|
|
cap.stop_capturing()
|
2014-01-25 04:22:19 +08:00
|
|
|
|
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestStdCaptureFDinvalidFD:
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_stdcapture_fd_invalid_fd(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2014-01-25 04:22:19 +08:00
|
|
|
import os
|
Perform FD capturing even if the FD is invalid
The `FDCapture`/`FDCaptureBinary` classes, used by `capfd`/`capfdbinary`
fixtures and the `--capture=fd` option (set by default), redirect FDs
1/2 (stdout/stderr) to a temporary file. To do this, they need to save
the old file by duplicating the FD before redirecting it, to be restored
once finished.
Previously, if this duplicating (`os.dup()`) failed, most likely due to
that FD being invalid, the FD redirection would silently not be done. The
FD capturing also performs python-level redirection (monkeypatching
`sys.stdout`/`sys.stderr`) which would still be done, but direct writes
to the FDs would fail.
This is not great. If pytest is run with `--capture=fd`, or a test is
using `capfd`, it expects writes to the FD to work and be captured,
regardless of external circumstances.
So, instead of disabling FD capturing, keep the redirection to a
temporary file, just don't restore it after closing, because there is
nothing to restore to.
2020-03-16 05:32:59 +08:00
|
|
|
from fnmatch import fnmatch
|
2014-03-28 14:03:37 +08:00
|
|
|
from _pytest import capture
|
2019-03-18 09:06:52 +08:00
|
|
|
|
2014-03-28 14:03:37 +08:00
|
|
|
def StdCaptureFD(out=True, err=True, in_=True):
|
2020-04-13 01:47:28 +08:00
|
|
|
return capture.MultiCapture(
|
|
|
|
in_=capture.FDCapture(0) if in_ else None,
|
|
|
|
out=capture.FDCapture(1) if out else None,
|
|
|
|
err=capture.FDCapture(2) if err else None,
|
|
|
|
)
|
2019-03-18 09:06:52 +08:00
|
|
|
|
2014-01-25 04:22:19 +08:00
|
|
|
def test_stdout():
|
|
|
|
os.close(1)
|
|
|
|
cap = StdCaptureFD(out=True, err=False, in_=False)
|
2020-04-17 15:01:51 +08:00
|
|
|
assert fnmatch(repr(cap.out), "<FDCapture 1 oldfd=* _state='initialized' tmpfile=*>")
|
Perform FD capturing even if the FD is invalid
The `FDCapture`/`FDCaptureBinary` classes, used by `capfd`/`capfdbinary`
fixtures and the `--capture=fd` option (set by default), redirect FDs
1/2 (stdout/stderr) to a temporary file. To do this, they need to save
the old file by duplicating the FD before redirecting it, to be restored
once finished.
Previously, if this duplicating (`os.dup()`) failed, most likely due to
that FD being invalid, the FD redirection would silently not be done. The
FD capturing also performs python-level redirection (monkeypatching
`sys.stdout`/`sys.stderr`) which would still be done, but direct writes
to the FDs would fail.
This is not great. If pytest is run with `--capture=fd`, or a test is
using `capfd`, it expects writes to the FD to work and be captured,
regardless of external circumstances.
So, instead of disabling FD capturing, keep the redirection to a
temporary file, just don't restore it after closing, because there is
nothing to restore to.
2020-03-16 05:32:59 +08:00
|
|
|
cap.start_capturing()
|
|
|
|
os.write(1, b"stdout")
|
|
|
|
assert cap.readouterr() == ("stdout", "")
|
2014-03-14 19:49:36 +08:00
|
|
|
cap.stop_capturing()
|
2019-03-18 09:06:52 +08:00
|
|
|
|
2014-01-25 04:22:19 +08:00
|
|
|
def test_stderr():
|
|
|
|
os.close(2)
|
|
|
|
cap = StdCaptureFD(out=False, err=True, in_=False)
|
2020-04-17 15:01:51 +08:00
|
|
|
assert fnmatch(repr(cap.err), "<FDCapture 2 oldfd=* _state='initialized' tmpfile=*>")
|
Perform FD capturing even if the FD is invalid
The `FDCapture`/`FDCaptureBinary` classes, used by `capfd`/`capfdbinary`
fixtures and the `--capture=fd` option (set by default), redirect FDs
1/2 (stdout/stderr) to a temporary file. To do this, they need to save
the old file by duplicating the FD before redirecting it, to be restored
once finished.
Previously, if this duplicating (`os.dup()`) failed, most likely due to
that FD being invalid, the FD redirection would silently not be done. The
FD capturing also performs python-level redirection (monkeypatching
`sys.stdout`/`sys.stderr`) which would still be done, but direct writes
to the FDs would fail.
This is not great. If pytest is run with `--capture=fd`, or a test is
using `capfd`, it expects writes to the FD to work and be captured,
regardless of external circumstances.
So, instead of disabling FD capturing, keep the redirection to a
temporary file, just don't restore it after closing, because there is
nothing to restore to.
2020-03-16 05:32:59 +08:00
|
|
|
cap.start_capturing()
|
|
|
|
os.write(2, b"stderr")
|
|
|
|
assert cap.readouterr() == ("", "stderr")
|
2014-03-14 19:49:36 +08:00
|
|
|
cap.stop_capturing()
|
2019-03-18 09:06:52 +08:00
|
|
|
|
2014-01-25 04:22:19 +08:00
|
|
|
def test_stdin():
|
|
|
|
os.close(0)
|
|
|
|
cap = StdCaptureFD(out=False, err=False, in_=True)
|
2020-04-17 15:01:51 +08:00
|
|
|
assert fnmatch(repr(cap.in_), "<FDCapture 0 oldfd=* _state='initialized' tmpfile=*>")
|
2014-03-14 19:49:36 +08:00
|
|
|
cap.stop_capturing()
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest_subprocess("--capture=fd")
|
2014-01-25 04:22:19 +08:00
|
|
|
assert result.ret == 0
|
2018-05-23 22:48:46 +08:00
|
|
|
assert result.parseoutcomes()["passed"] == 3
|
2014-01-25 04:22:19 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_fdcapture_invalid_fd_with_fd_reuse(self, pytester: Pytester) -> None:
|
Perform FD capturing even if the FD is invalid
The `FDCapture`/`FDCaptureBinary` classes, used by `capfd`/`capfdbinary`
fixtures and the `--capture=fd` option (set by default), redirect FDs
1/2 (stdout/stderr) to a temporary file. To do this, they need to save
the old file by duplicating the FD before redirecting it, to be restored
once finished.
Previously, if this duplicating (`os.dup()`) failed, most likely due to
that FD being invalid, the FD redirection would silently not be done. The
FD capturing also performs python-level redirection (monkeypatching
`sys.stdout`/`sys.stderr`) which would still be done, but direct writes
to the FDs would fail.
This is not great. If pytest is run with `--capture=fd`, or a test is
using `capfd`, it expects writes to the FD to work and be captured,
regardless of external circumstances.
So, instead of disabling FD capturing, keep the redirection to a
temporary file, just don't restore it after closing, because there is
nothing to restore to.
2020-03-16 05:32:59 +08:00
|
|
|
with saved_fd(1):
|
|
|
|
os.close(1)
|
|
|
|
cap = capture.FDCaptureBinary(1)
|
|
|
|
cap.start()
|
|
|
|
os.write(1, b"started")
|
|
|
|
cap.suspend()
|
|
|
|
os.write(1, b" suspended")
|
|
|
|
cap.resume()
|
|
|
|
os.write(1, b" resumed")
|
|
|
|
assert cap.snap() == b"started resumed"
|
|
|
|
cap.done()
|
|
|
|
with pytest.raises(OSError):
|
|
|
|
os.write(1, b"done")
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_fdcapture_invalid_fd_without_fd_reuse(self, pytester: Pytester) -> None:
|
Perform FD capturing even if the FD is invalid
The `FDCapture`/`FDCaptureBinary` classes, used by `capfd`/`capfdbinary`
fixtures and the `--capture=fd` option (set by default), redirect FDs
1/2 (stdout/stderr) to a temporary file. To do this, they need to save
the old file by duplicating the FD before redirecting it, to be restored
once finished.
Previously, if this duplicating (`os.dup()`) failed, most likely due to
that FD being invalid, the FD redirection would silently not be done. The
FD capturing also performs python-level redirection (monkeypatching
`sys.stdout`/`sys.stderr`) which would still be done, but direct writes
to the FDs would fail.
This is not great. If pytest is run with `--capture=fd`, or a test is
using `capfd`, it expects writes to the FD to work and be captured,
regardless of external circumstances.
So, instead of disabling FD capturing, keep the redirection to a
temporary file, just don't restore it after closing, because there is
nothing to restore to.
2020-03-16 05:32:59 +08:00
|
|
|
with saved_fd(1), saved_fd(2):
|
|
|
|
os.close(1)
|
|
|
|
os.close(2)
|
|
|
|
cap = capture.FDCaptureBinary(2)
|
|
|
|
cap.start()
|
|
|
|
os.write(2, b"started")
|
|
|
|
cap.suspend()
|
|
|
|
os.write(2, b" suspended")
|
|
|
|
cap.resume()
|
|
|
|
os.write(2, b" resumed")
|
|
|
|
assert cap.snap() == b"started resumed"
|
|
|
|
cap.done()
|
|
|
|
with pytest.raises(OSError):
|
|
|
|
os.write(2, b"done")
|
|
|
|
|
2014-01-25 04:22:19 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_capture_not_started_but_reset() -> None:
|
2014-03-28 14:03:37 +08:00
|
|
|
capsys = StdCapture()
|
2014-03-14 19:49:36 +08:00
|
|
|
capsys.stop_capturing()
|
2014-01-25 04:22:19 +08:00
|
|
|
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_using_capsys_fixture_works_with_sys_stdout_encoding(
|
|
|
|
capsys: CaptureFixture[str],
|
|
|
|
) -> None:
|
2018-05-23 22:48:46 +08:00
|
|
|
test_text = "test text"
|
2017-07-15 22:15:26 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
print(test_text.encode(sys.stdout.encoding, "replace"))
|
2017-07-15 22:15:26 +08:00
|
|
|
(out, err) = capsys.readouterr()
|
|
|
|
assert out
|
2018-05-23 22:48:46 +08:00
|
|
|
assert err == ""
|
2017-07-15 22:15:26 +08:00
|
|
|
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_capsys_results_accessible_by_attribute(capsys: CaptureFixture[str]) -> None:
|
2017-10-29 02:53:19 +08:00
|
|
|
sys.stdout.write("spam")
|
|
|
|
sys.stderr.write("eggs")
|
|
|
|
capture_result = capsys.readouterr()
|
|
|
|
assert capture_result.out == "spam"
|
|
|
|
assert capture_result.err == "eggs"
|
|
|
|
|
|
|
|
|
2020-05-27 18:04:56 +08:00
|
|
|
def test_fdcapture_tmpfile_remains_the_same() -> None:
|
|
|
|
cap = StdCaptureFD(out=False, err=True)
|
2014-01-25 04:22:19 +08:00
|
|
|
try:
|
2014-03-14 19:49:36 +08:00
|
|
|
cap.start_capturing()
|
2014-01-25 04:22:19 +08:00
|
|
|
capfile = cap.err.tmpfile
|
2014-03-14 19:49:36 +08:00
|
|
|
cap.readouterr()
|
2014-01-25 04:22:19 +08:00
|
|
|
finally:
|
2014-03-28 14:11:25 +08:00
|
|
|
cap.stop_capturing()
|
2014-01-25 04:22:19 +08:00
|
|
|
capfile2 = cap.err.tmpfile
|
|
|
|
assert capfile2 == capfile
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_close_and_capture_again(pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2014-04-01 20:19:52 +08:00
|
|
|
import os
|
|
|
|
def test_close():
|
|
|
|
os.close(1)
|
|
|
|
def test_capture_again():
|
2014-04-01 20:32:12 +08:00
|
|
|
os.write(1, b"hello\\n")
|
2014-04-01 20:19:52 +08:00
|
|
|
assert 0
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest_subprocess()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
"""
|
2014-04-01 20:19:52 +08:00
|
|
|
*test_capture_again*
|
|
|
|
*assert 0*
|
|
|
|
*stdout*
|
|
|
|
*hello*
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2014-04-01 20:19:52 +08:00
|
|
|
|
|
|
|
|
2020-04-16 14:49:17 +08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"method", ["SysCapture(2)", "SysCapture(2, tee=True)", "FDCapture(2)"]
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_capturing_and_logging_fundamentals(pytester: Pytester, method: str) -> None:
|
2014-01-25 04:22:19 +08:00
|
|
|
# here we check a fundamental feature
|
2020-12-09 04:20:02 +08:00
|
|
|
p = pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2021-10-16 07:50:05 +08:00
|
|
|
import sys, os, logging
|
2014-01-25 04:22:19 +08:00
|
|
|
from _pytest import capture
|
2020-04-13 01:47:28 +08:00
|
|
|
cap = capture.MultiCapture(
|
|
|
|
in_=None,
|
|
|
|
out=None,
|
2020-04-16 14:49:17 +08:00
|
|
|
err=capture.%s,
|
2020-04-13 01:47:28 +08:00
|
|
|
)
|
2014-03-14 19:49:36 +08:00
|
|
|
cap.start_capturing()
|
2014-01-25 04:22:19 +08:00
|
|
|
|
2018-12-03 19:52:33 +08:00
|
|
|
logging.warning("hello1")
|
2014-03-14 19:49:36 +08:00
|
|
|
outerr = cap.readouterr()
|
2018-11-22 16:15:14 +08:00
|
|
|
print("suspend, captured %%s" %%(outerr,))
|
2018-12-03 19:52:33 +08:00
|
|
|
logging.warning("hello2")
|
2014-01-25 04:22:19 +08:00
|
|
|
|
2014-03-14 19:49:36 +08:00
|
|
|
cap.pop_outerr_to_orig()
|
2018-12-03 19:52:33 +08:00
|
|
|
logging.warning("hello3")
|
2014-01-25 04:22:19 +08:00
|
|
|
|
2014-03-14 19:49:36 +08:00
|
|
|
outerr = cap.readouterr()
|
2018-11-22 16:15:14 +08:00
|
|
|
print("suspend2, captured %%s" %% (outerr,))
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
% (method,)
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpython(p)
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
"""
|
2014-03-14 19:49:36 +08:00
|
|
|
suspend, captured*hello1*
|
|
|
|
suspend2, captured*WARNING:root:hello3*
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
|
|
|
result.stderr.fnmatch_lines(
|
|
|
|
"""
|
2014-03-14 19:49:36 +08:00
|
|
|
WARNING:root:hello2
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2014-01-25 04:22:19 +08:00
|
|
|
assert "atexit" not in result.stderr.str()
|
2014-04-03 02:29:10 +08:00
|
|
|
|
2014-08-07 17:05:42 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_error_attribute_issue555(pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2014-08-07 17:05:42 +08:00
|
|
|
import sys
|
|
|
|
def test_capattr():
|
2020-03-08 00:38:22 +08:00
|
|
|
assert sys.stdout.errors == "replace"
|
|
|
|
assert sys.stderr.errors == "replace"
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
reprec = pytester.inline_run()
|
2014-08-07 17:05:42 +08:00
|
|
|
reprec.assertoutcome(passed=1)
|
2014-08-19 18:57:37 +08:00
|
|
|
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.skipif(
|
2020-12-30 17:56:09 +08:00
|
|
|
not sys.platform.startswith("win"),
|
|
|
|
reason="only on windows",
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2021-12-30 18:37:18 +08:00
|
|
|
def test_windowsconsoleio_workaround_non_standard_streams() -> None:
|
2017-08-08 07:55:28 +08:00
|
|
|
"""
|
2021-12-30 18:37:18 +08:00
|
|
|
Ensure _windowsconsoleio_workaround function works with objects that
|
2017-08-08 07:55:28 +08:00
|
|
|
do not implement the full ``io``-based stream protocol, for example execnet channels (#2666).
|
|
|
|
"""
|
2021-12-30 18:37:18 +08:00
|
|
|
from _pytest.capture import _windowsconsoleio_workaround
|
2017-08-08 07:55:28 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class DummyStream:
|
2017-08-08 07:55:28 +08:00
|
|
|
def write(self, s):
|
|
|
|
pass
|
|
|
|
|
2020-05-01 19:40:17 +08:00
|
|
|
stream = cast(TextIO, DummyStream())
|
2021-12-30 18:37:18 +08:00
|
|
|
_windowsconsoleio_workaround(stream)
|
2017-08-08 07:55:28 +08:00
|
|
|
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_dontreadfrominput_has_encoding(pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2014-09-22 19:19:27 +08:00
|
|
|
import sys
|
|
|
|
def test_capattr():
|
|
|
|
# should not raise AttributeError
|
|
|
|
assert sys.stdout.encoding
|
|
|
|
assert sys.stderr.encoding
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
reprec = pytester.inline_run()
|
2014-09-22 19:19:27 +08:00
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_crash_on_closing_tmpfile_py27(
|
|
|
|
pytester: Pytester, monkeypatch: MonkeyPatch
|
|
|
|
) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2018-03-14 04:41:26 +08:00
|
|
|
import threading
|
|
|
|
import sys
|
|
|
|
|
2019-03-03 21:06:55 +08:00
|
|
|
printing = threading.Event()
|
|
|
|
|
2018-03-14 04:41:26 +08:00
|
|
|
def spam():
|
|
|
|
f = sys.stderr
|
2019-03-03 21:06:55 +08:00
|
|
|
print('SPAMBEFORE', end='', file=f)
|
|
|
|
printing.set()
|
|
|
|
|
2018-03-14 04:41:26 +08:00
|
|
|
while True:
|
2019-03-03 21:06:55 +08:00
|
|
|
try:
|
|
|
|
f.flush()
|
|
|
|
except (OSError, ValueError):
|
|
|
|
break
|
2018-03-14 04:41:26 +08:00
|
|
|
|
2019-03-03 21:06:55 +08:00
|
|
|
def test_spam_in_thread():
|
2018-03-14 04:41:26 +08:00
|
|
|
t = threading.Thread(target=spam)
|
|
|
|
t.daemon = True
|
|
|
|
t.start()
|
|
|
|
|
2019-03-03 21:06:55 +08:00
|
|
|
printing.wait()
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-01-18 20:08:58 +08:00
|
|
|
# Do not consider plugins like hypothesis, which might output to stderr.
|
2020-12-09 04:20:02 +08:00
|
|
|
monkeypatch.setenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", "1")
|
|
|
|
result = pytester.runpytest_subprocess(str(p))
|
2018-03-14 04:41:26 +08:00
|
|
|
assert result.ret == 0
|
2019-03-03 21:06:55 +08:00
|
|
|
assert result.stderr.str() == ""
|
2020-03-27 23:40:23 +08:00
|
|
|
result.stdout.no_fnmatch_line("*OSError*")
|
2018-03-14 04:41:26 +08:00
|
|
|
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_global_capture_with_live_logging(pytester: Pytester) -> None:
|
2018-08-17 06:20:51 +08:00
|
|
|
# Issue 3819
|
2018-08-19 19:44:12 +08:00
|
|
|
# capture should work with live cli logging
|
|
|
|
|
|
|
|
# Teardown report seems to have the capture for the whole process (setup, capture, teardown)
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makeconftest(
|
2018-08-19 21:46:02 +08:00
|
|
|
"""
|
2018-08-19 19:44:12 +08:00
|
|
|
def pytest_runtest_logreport(report):
|
|
|
|
if "test_global" in report.nodeid:
|
|
|
|
if report.when == "teardown":
|
2018-08-19 20:29:57 +08:00
|
|
|
with open("caplog", "w") as f:
|
|
|
|
f.write(report.caplog)
|
|
|
|
with open("capstdout", "w") as f:
|
|
|
|
f.write(report.capstdout)
|
2018-08-20 18:23:59 +08:00
|
|
|
"""
|
2018-08-19 21:46:02 +08:00
|
|
|
)
|
2018-08-19 19:44:12 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makepyfile(
|
2018-08-17 06:20:51 +08:00
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
import sys
|
2018-08-19 20:29:57 +08:00
|
|
|
import pytest
|
2018-08-17 06:20:51 +08:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2018-08-19 21:46:02 +08:00
|
|
|
|
2018-08-19 19:44:12 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def fix1():
|
|
|
|
print("fix setup")
|
2018-08-19 20:29:57 +08:00
|
|
|
logging.info("fix setup")
|
2018-08-19 19:44:12 +08:00
|
|
|
yield
|
2018-08-19 20:29:57 +08:00
|
|
|
logging.info("fix teardown")
|
2018-08-19 19:44:12 +08:00
|
|
|
print("fix teardown")
|
2018-08-19 20:29:57 +08:00
|
|
|
|
|
|
|
def test_global(fix1):
|
2018-08-19 19:44:12 +08:00
|
|
|
print("begin test")
|
|
|
|
logging.info("something in test")
|
|
|
|
print("end test")
|
2018-08-17 06:20:51 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest_subprocess("--log-cli-level=INFO")
|
2018-08-17 06:20:51 +08:00
|
|
|
assert result.ret == 0
|
2018-08-19 20:29:57 +08:00
|
|
|
|
2020-04-25 02:51:07 +08:00
|
|
|
with open("caplog") as f:
|
2018-08-19 20:29:57 +08:00
|
|
|
caplog = f.read()
|
|
|
|
|
|
|
|
assert "fix setup" in caplog
|
|
|
|
assert "something in test" in caplog
|
|
|
|
assert "fix teardown" in caplog
|
|
|
|
|
2020-04-25 02:51:07 +08:00
|
|
|
with open("capstdout") as f:
|
2018-08-19 20:29:57 +08:00
|
|
|
capstdout = f.read()
|
|
|
|
|
|
|
|
assert "fix setup" in capstdout
|
|
|
|
assert "begin test" in capstdout
|
|
|
|
assert "end test" in capstdout
|
|
|
|
assert "fix teardown" in capstdout
|
2018-08-20 18:23:59 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("capture_fixture", ["capsys", "capfd"])
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_capture_with_live_logging(
|
|
|
|
pytester: Pytester, capture_fixture: CaptureFixture[str]
|
|
|
|
) -> None:
|
2018-08-20 18:23:59 +08:00
|
|
|
# Issue 3819
|
|
|
|
# capture should work with live cli logging
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
pytester.makepyfile(
|
2018-08-17 06:20:51 +08:00
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
import sys
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2018-08-20 18:23:59 +08:00
|
|
|
def test_capture({0}):
|
2018-08-17 06:20:51 +08:00
|
|
|
print("hello")
|
|
|
|
sys.stderr.write("world\\n")
|
2018-08-20 18:23:59 +08:00
|
|
|
captured = {0}.readouterr()
|
2018-08-17 06:20:51 +08:00
|
|
|
assert captured.out == "hello\\n"
|
|
|
|
assert captured.err == "world\\n"
|
|
|
|
|
|
|
|
logging.info("something")
|
|
|
|
print("next")
|
2018-08-17 19:00:27 +08:00
|
|
|
logging.info("something")
|
|
|
|
|
2018-08-20 18:23:59 +08:00
|
|
|
captured = {0}.readouterr()
|
2018-08-17 06:20:51 +08:00
|
|
|
assert captured.out == "next\\n"
|
2018-08-20 18:23:59 +08:00
|
|
|
""".format(
|
|
|
|
capture_fixture
|
|
|
|
)
|
2018-08-17 06:20:51 +08:00
|
|
|
)
|
2018-08-20 18:23:59 +08:00
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest_subprocess("--log-cli-level=INFO")
|
2018-08-17 06:20:51 +08:00
|
|
|
assert result.ret == 0
|
2019-03-01 17:08:39 +08:00
|
|
|
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_typeerror_encodedfile_write(pytester: Pytester) -> None:
|
2019-03-01 17:08:39 +08:00
|
|
|
"""It should behave the same with and without output capturing (#4861)."""
|
2020-12-09 04:20:02 +08:00
|
|
|
p = pytester.makepyfile(
|
2019-03-01 17:08:39 +08:00
|
|
|
"""
|
|
|
|
def test_fails():
|
|
|
|
import sys
|
|
|
|
sys.stdout.write(b"foo")
|
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result_without_capture = pytester.runpytest("-s", str(p))
|
|
|
|
result_with_capture = pytester.runpytest(str(p))
|
2019-03-01 17:08:39 +08:00
|
|
|
|
|
|
|
assert result_with_capture.ret == result_without_capture.ret
|
2020-03-08 00:38:22 +08:00
|
|
|
out = result_with_capture.stdout.str()
|
|
|
|
assert ("TypeError: write() argument must be str, not bytes" in out) or (
|
|
|
|
"TypeError: unicode argument expected, got 'bytes'" in out
|
2019-05-28 07:31:52 +08:00
|
|
|
)
|
2020-01-24 20:50:56 +08:00
|
|
|
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_stderr_write_returns_len(capsys: CaptureFixture[str]) -> None:
|
2020-01-24 20:50:56 +08:00
|
|
|
"""Write on Encoded files, namely captured stderr, should return number of characters written."""
|
|
|
|
assert sys.stderr.write("Foo") == 3
|
2020-01-25 17:58:26 +08:00
|
|
|
|
|
|
|
|
2020-01-26 01:11:38 +08:00
|
|
|
def test_encodedfile_writelines(tmpfile: BinaryIO) -> None:
|
2020-03-08 00:38:22 +08:00
|
|
|
ef = capture.EncodedFile(tmpfile, encoding="utf-8")
|
|
|
|
with pytest.raises(TypeError):
|
2020-10-17 21:54:54 +08:00
|
|
|
ef.writelines([b"line1", b"line2"]) # type: ignore[list-item]
|
2020-07-10 14:44:14 +08:00
|
|
|
assert ef.writelines(["line3", "line4"]) is None # type: ignore[func-returns-value]
|
2020-03-08 00:38:22 +08:00
|
|
|
ef.flush()
|
2020-01-25 17:58:26 +08:00
|
|
|
tmpfile.seek(0)
|
2020-03-08 00:38:22 +08:00
|
|
|
assert tmpfile.read() == b"line3line4"
|
2020-01-25 17:58:26 +08:00
|
|
|
tmpfile.close()
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
ef.read()
|
2020-02-23 06:39:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test__get_multicapture() -> None:
|
2020-04-09 00:11:31 +08:00
|
|
|
assert isinstance(_get_multicapture("no"), MultiCapture)
|
2020-02-23 06:39:20 +08:00
|
|
|
pytest.raises(ValueError, _get_multicapture, "unknown").match(
|
|
|
|
r"^unknown capturing method: 'unknown'"
|
|
|
|
)
|
2019-11-27 18:50:22 +08:00
|
|
|
|
|
|
|
|
2020-12-09 04:20:02 +08:00
|
|
|
def test_logging_while_collecting(pytester: Pytester) -> None:
|
2019-11-27 18:50:22 +08:00
|
|
|
"""Issue #6240: Calls to logging.xxx() during collection causes all logging calls to be duplicated to stderr"""
|
2020-12-09 04:20:02 +08:00
|
|
|
p = pytester.makepyfile(
|
2019-11-27 18:50:22 +08:00
|
|
|
"""\
|
|
|
|
import logging
|
|
|
|
|
|
|
|
logging.warning("during collection")
|
|
|
|
|
|
|
|
def test_logging():
|
|
|
|
logging.warning("during call")
|
|
|
|
assert False
|
|
|
|
"""
|
|
|
|
)
|
2020-12-09 04:20:02 +08:00
|
|
|
result = pytester.runpytest_subprocess(p)
|
2019-11-27 18:50:22 +08:00
|
|
|
assert result.ret == ExitCode.TESTS_FAILED
|
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
[
|
|
|
|
"*test_*.py F*",
|
|
|
|
"====* FAILURES *====",
|
|
|
|
"____*____",
|
|
|
|
"*--- Captured log call*",
|
|
|
|
"WARNING * during call",
|
|
|
|
"*1 failed*",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
result.stdout.no_fnmatch_line("*Captured stderr call*")
|
|
|
|
result.stdout.no_fnmatch_line("*during collection*")
|