rewrite all testing uses of py.io to _pytest.capture
This commit is contained in:
parent
0ac94134f5
commit
3cc58c2f78
|
@ -4,10 +4,12 @@ import sys
|
||||||
import py
|
import py
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
from _pytest import capture
|
||||||
from _pytest.capture import CaptureManager
|
from _pytest.capture import CaptureManager
|
||||||
|
|
||||||
needsosdup = pytest.mark.xfail("not hasattr(os, 'dup')")
|
needsosdup = pytest.mark.xfail("not hasattr(os, 'dup')")
|
||||||
|
|
||||||
|
|
||||||
class TestCaptureManager:
|
class TestCaptureManager:
|
||||||
def test_getmethod_default_no_fd(self, testdir, monkeypatch):
|
def test_getmethod_default_no_fd(self, testdir, monkeypatch):
|
||||||
config = testdir.parseconfig(testdir.tmpdir)
|
config = testdir.parseconfig(testdir.tmpdir)
|
||||||
|
@ -39,7 +41,7 @@ class TestCaptureManager:
|
||||||
@needsosdup
|
@needsosdup
|
||||||
@pytest.mark.parametrize("method", ['no', 'fd', 'sys'])
|
@pytest.mark.parametrize("method", ['no', 'fd', 'sys'])
|
||||||
def test_capturing_basic_api(self, method):
|
def test_capturing_basic_api(self, method):
|
||||||
capouter = py.io.StdCaptureFD()
|
capouter = capture.StdCaptureFD()
|
||||||
old = sys.stdout, sys.stderr, sys.stdin
|
old = sys.stdout, sys.stderr, sys.stdin
|
||||||
try:
|
try:
|
||||||
capman = CaptureManager()
|
capman = CaptureManager()
|
||||||
|
@ -63,7 +65,7 @@ class TestCaptureManager:
|
||||||
|
|
||||||
@needsosdup
|
@needsosdup
|
||||||
def test_juggle_capturings(self, testdir):
|
def test_juggle_capturings(self, testdir):
|
||||||
capouter = py.io.StdCaptureFD()
|
capouter = capture.StdCaptureFD()
|
||||||
try:
|
try:
|
||||||
#config = testdir.parseconfig(testdir.tmpdir)
|
#config = testdir.parseconfig(testdir.tmpdir)
|
||||||
capman = CaptureManager()
|
capman = CaptureManager()
|
||||||
|
@ -85,10 +87,11 @@ class TestCaptureManager:
|
||||||
finally:
|
finally:
|
||||||
capouter.reset()
|
capouter.reset()
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.xfail("hasattr(sys, 'pypy_version_info')")
|
@pytest.mark.xfail("hasattr(sys, 'pypy_version_info')")
|
||||||
@pytest.mark.parametrize("method", ['fd', 'sys'])
|
@pytest.mark.parametrize("method", ['fd', 'sys'])
|
||||||
def test_capturing_unicode(testdir, method):
|
def test_capturing_unicode(testdir, method):
|
||||||
if sys.version_info >= (3,0):
|
if sys.version_info >= (3, 0):
|
||||||
obj = "'b\u00f6y'"
|
obj = "'b\u00f6y'"
|
||||||
else:
|
else:
|
||||||
obj = "u'\u00f6y'"
|
obj = "u'\u00f6y'"
|
||||||
|
@ -105,6 +108,7 @@ def test_capturing_unicode(testdir, method):
|
||||||
"*1 passed*"
|
"*1 passed*"
|
||||||
])
|
])
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("method", ['fd', 'sys'])
|
@pytest.mark.parametrize("method", ['fd', 'sys'])
|
||||||
def test_capturing_bytes_in_utf8_encoding(testdir, method):
|
def test_capturing_bytes_in_utf8_encoding(testdir, method):
|
||||||
testdir.makepyfile("""
|
testdir.makepyfile("""
|
||||||
|
@ -116,6 +120,7 @@ def test_capturing_bytes_in_utf8_encoding(testdir, method):
|
||||||
"*1 passed*"
|
"*1 passed*"
|
||||||
])
|
])
|
||||||
|
|
||||||
|
|
||||||
def test_collect_capturing(testdir):
|
def test_collect_capturing(testdir):
|
||||||
p = testdir.makepyfile("""
|
p = testdir.makepyfile("""
|
||||||
print ("collect %s failure" % 13)
|
print ("collect %s failure" % 13)
|
||||||
|
@ -127,6 +132,7 @@ def test_collect_capturing(testdir):
|
||||||
"*collect 13 failure*",
|
"*collect 13 failure*",
|
||||||
])
|
])
|
||||||
|
|
||||||
|
|
||||||
class TestPerTestCapturing:
|
class TestPerTestCapturing:
|
||||||
def test_capture_and_fixtures(self, testdir):
|
def test_capture_and_fixtures(self, testdir):
|
||||||
p = testdir.makepyfile("""
|
p = testdir.makepyfile("""
|
||||||
|
@ -174,7 +180,6 @@ class TestPerTestCapturing:
|
||||||
"in teardown*",
|
"in teardown*",
|
||||||
])
|
])
|
||||||
|
|
||||||
|
|
||||||
def test_no_carry_over(self, testdir):
|
def test_no_carry_over(self, testdir):
|
||||||
p = testdir.makepyfile("""
|
p = testdir.makepyfile("""
|
||||||
def test_func1():
|
def test_func1():
|
||||||
|
@ -188,7 +193,6 @@ class TestPerTestCapturing:
|
||||||
assert "in func1" not in s
|
assert "in func1" not in s
|
||||||
assert "in func2" in s
|
assert "in func2" in s
|
||||||
|
|
||||||
|
|
||||||
def test_teardown_capturing(self, testdir):
|
def test_teardown_capturing(self, testdir):
|
||||||
p = testdir.makepyfile("""
|
p = testdir.makepyfile("""
|
||||||
def setup_function(function):
|
def setup_function(function):
|
||||||
|
@ -249,13 +253,14 @@ class TestPerTestCapturing:
|
||||||
"2",
|
"2",
|
||||||
])
|
])
|
||||||
|
|
||||||
|
|
||||||
class TestLoggingInteraction:
|
class TestLoggingInteraction:
|
||||||
def test_logging_stream_ownership(self, testdir):
|
def test_logging_stream_ownership(self, testdir):
|
||||||
p = testdir.makepyfile("""
|
p = testdir.makepyfile("""
|
||||||
def test_logging():
|
def test_logging():
|
||||||
import logging
|
import logging
|
||||||
import pytest
|
import pytest
|
||||||
stream = py.io.TextIO()
|
stream = capture.TextIO()
|
||||||
logging.basicConfig(stream=stream)
|
logging.basicConfig(stream=stream)
|
||||||
stream.close() # to free memory/release resources
|
stream.close() # to free memory/release resources
|
||||||
""")
|
""")
|
||||||
|
@ -325,7 +330,8 @@ class TestLoggingInteraction:
|
||||||
logging.warn("hello432")
|
logging.warn("hello432")
|
||||||
assert 0
|
assert 0
|
||||||
""")
|
""")
|
||||||
result = testdir.runpytest(p, "--traceconfig",
|
result = testdir.runpytest(
|
||||||
|
p, "--traceconfig",
|
||||||
"-p", "no:capturelog")
|
"-p", "no:capturelog")
|
||||||
assert result.ret != 0
|
assert result.ret != 0
|
||||||
result.stdout.fnmatch_lines([
|
result.stdout.fnmatch_lines([
|
||||||
|
@ -466,6 +472,7 @@ def test_setup_failure_does_not_kill_capturing(testdir):
|
||||||
"*1 error*"
|
"*1 error*"
|
||||||
])
|
])
|
||||||
|
|
||||||
|
|
||||||
def test_fdfuncarg_skips_on_no_osdup(testdir):
|
def test_fdfuncarg_skips_on_no_osdup(testdir):
|
||||||
testdir.makepyfile("""
|
testdir.makepyfile("""
|
||||||
import os
|
import os
|
||||||
|
@ -479,6 +486,7 @@ def test_fdfuncarg_skips_on_no_osdup(testdir):
|
||||||
"*1 skipped*"
|
"*1 skipped*"
|
||||||
])
|
])
|
||||||
|
|
||||||
|
|
||||||
def test_capture_conftest_runtest_setup(testdir):
|
def test_capture_conftest_runtest_setup(testdir):
|
||||||
testdir.makeconftest("""
|
testdir.makeconftest("""
|
||||||
def pytest_runtest_setup():
|
def pytest_runtest_setup():
|
||||||
|
@ -489,6 +497,7 @@ def test_capture_conftest_runtest_setup(testdir):
|
||||||
assert result.ret == 0
|
assert result.ret == 0
|
||||||
assert 'hello19' not in result.stdout.str()
|
assert 'hello19' not in result.stdout.str()
|
||||||
|
|
||||||
|
|
||||||
def test_capture_early_option_parsing(testdir):
|
def test_capture_early_option_parsing(testdir):
|
||||||
testdir.makeconftest("""
|
testdir.makeconftest("""
|
||||||
def pytest_runtest_setup():
|
def pytest_runtest_setup():
|
||||||
|
@ -499,6 +508,7 @@ def test_capture_early_option_parsing(testdir):
|
||||||
assert result.ret == 0
|
assert result.ret == 0
|
||||||
assert 'hello19' in result.stdout.str()
|
assert 'hello19' in result.stdout.str()
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.xfail(sys.version_info >= (3, 0), reason='encoding issues')
|
@pytest.mark.xfail(sys.version_info >= (3, 0), reason='encoding issues')
|
||||||
def test_capture_binary_output(testdir):
|
def test_capture_binary_output(testdir):
|
||||||
testdir.makepyfile(r"""
|
testdir.makepyfile(r"""
|
||||||
|
@ -520,21 +530,20 @@ def test_capture_binary_output(testdir):
|
||||||
result.stdout.fnmatch_lines([
|
result.stdout.fnmatch_lines([
|
||||||
'*2 passed*',
|
'*2 passed*',
|
||||||
])
|
])
|
||||||
|
|
||||||
|
|
||||||
import os, sys
|
|
||||||
import py
|
|
||||||
|
|
||||||
needsdup = py.test.mark.skipif("not hasattr(os, 'dup')")
|
needsdup = pytest.mark.skipif("not hasattr(os, 'dup')")
|
||||||
|
|
||||||
from py.builtin import print_
|
from py.builtin import print_
|
||||||
|
|
||||||
if sys.version_info >= (3,0):
|
|
||||||
|
if sys.version_info >= (3, 0):
|
||||||
def tobytes(obj):
|
def tobytes(obj):
|
||||||
if isinstance(obj, str):
|
if isinstance(obj, str):
|
||||||
obj = obj.encode('UTF-8')
|
obj = obj.encode('UTF-8')
|
||||||
assert isinstance(obj, bytes)
|
assert isinstance(obj, bytes)
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
def totext(obj):
|
def totext(obj):
|
||||||
if isinstance(obj, bytes):
|
if isinstance(obj, bytes):
|
||||||
obj = str(obj, 'UTF-8')
|
obj = str(obj, 'UTF-8')
|
||||||
|
@ -546,51 +555,57 @@ else:
|
||||||
obj = obj.encode('UTF-8')
|
obj = obj.encode('UTF-8')
|
||||||
assert isinstance(obj, str)
|
assert isinstance(obj, str)
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
def totext(obj):
|
def totext(obj):
|
||||||
if isinstance(obj, str):
|
if isinstance(obj, str):
|
||||||
obj = unicode(obj, 'UTF-8')
|
obj = unicode(obj, 'UTF-8')
|
||||||
assert isinstance(obj, unicode)
|
assert isinstance(obj, unicode)
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
|
|
||||||
def oswritebytes(fd, obj):
|
def oswritebytes(fd, obj):
|
||||||
os.write(fd, tobytes(obj))
|
os.write(fd, tobytes(obj))
|
||||||
|
|
||||||
|
|
||||||
class TestTextIO:
|
class TestTextIO:
|
||||||
def test_text(self):
|
def test_text(self):
|
||||||
f = py.io.TextIO()
|
f = capture.TextIO()
|
||||||
f.write("hello")
|
f.write("hello")
|
||||||
s = f.getvalue()
|
s = f.getvalue()
|
||||||
assert s == "hello"
|
assert s == "hello"
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
def test_unicode_and_str_mixture(self):
|
def test_unicode_and_str_mixture(self):
|
||||||
f = py.io.TextIO()
|
f = capture.TextIO()
|
||||||
if sys.version_info >= (3,0):
|
if sys.version_info >= (3, 0):
|
||||||
f.write("\u00f6")
|
f.write("\u00f6")
|
||||||
py.test.raises(TypeError, "f.write(bytes('hello', 'UTF-8'))")
|
pytest.raises(TypeError, "f.write(bytes('hello', 'UTF-8'))")
|
||||||
else:
|
else:
|
||||||
f.write(unicode("\u00f6", 'UTF-8'))
|
f.write(unicode("\u00f6", 'UTF-8'))
|
||||||
f.write("hello") # bytes
|
f.write("hello") # bytes
|
||||||
s = f.getvalue()
|
s = f.getvalue()
|
||||||
f.close()
|
f.close()
|
||||||
assert isinstance(s, unicode)
|
assert isinstance(s, unicode)
|
||||||
|
|
||||||
|
|
||||||
def test_bytes_io():
|
def test_bytes_io():
|
||||||
f = py.io.BytesIO()
|
f = capture.BytesIO()
|
||||||
f.write(tobytes("hello"))
|
f.write(tobytes("hello"))
|
||||||
py.test.raises(TypeError, "f.write(totext('hello'))")
|
pytest.raises(TypeError, "f.write(totext('hello'))")
|
||||||
s = f.getvalue()
|
s = f.getvalue()
|
||||||
assert s == tobytes("hello")
|
assert s == tobytes("hello")
|
||||||
|
|
||||||
|
|
||||||
def test_dontreadfrominput():
|
def test_dontreadfrominput():
|
||||||
from py._io.capture import DontReadFromInput
|
from _pytest.capture import DontReadFromInput
|
||||||
f = DontReadFromInput()
|
f = DontReadFromInput()
|
||||||
assert not f.isatty()
|
assert not f.isatty()
|
||||||
py.test.raises(IOError, f.read)
|
pytest.raises(IOError, f.read)
|
||||||
py.test.raises(IOError, f.readlines)
|
pytest.raises(IOError, f.readlines)
|
||||||
py.test.raises(IOError, iter, f)
|
pytest.raises(IOError, iter, f)
|
||||||
py.test.raises(ValueError, f.fileno)
|
pytest.raises(ValueError, f.fileno)
|
||||||
f.close() # just for completeness
|
f.close() # just for completeness
|
||||||
|
|
||||||
|
|
||||||
def pytest_funcarg__tmpfile(request):
|
def pytest_funcarg__tmpfile(request):
|
||||||
testdir = request.getfuncargvalue("testdir")
|
testdir = request.getfuncargvalue("testdir")
|
||||||
|
@ -598,11 +613,12 @@ def pytest_funcarg__tmpfile(request):
|
||||||
request.addfinalizer(f.close)
|
request.addfinalizer(f.close)
|
||||||
return f
|
return f
|
||||||
|
|
||||||
|
|
||||||
@needsdup
|
@needsdup
|
||||||
def test_dupfile(tmpfile):
|
def test_dupfile(tmpfile):
|
||||||
flist = []
|
flist = []
|
||||||
for i in range(5):
|
for i in range(5):
|
||||||
nf = py.io.dupfile(tmpfile, encoding="utf-8")
|
nf = capture.dupfile(tmpfile, encoding="utf-8")
|
||||||
assert nf != tmpfile
|
assert nf != tmpfile
|
||||||
assert nf.fileno() != tmpfile.fileno()
|
assert nf.fileno() != tmpfile.fileno()
|
||||||
assert nf not in flist
|
assert nf not in flist
|
||||||
|
@ -616,6 +632,7 @@ def test_dupfile(tmpfile):
|
||||||
assert "01234" in repr(s)
|
assert "01234" in repr(s)
|
||||||
tmpfile.close()
|
tmpfile.close()
|
||||||
|
|
||||||
|
|
||||||
def test_dupfile_no_mode():
|
def test_dupfile_no_mode():
|
||||||
"""
|
"""
|
||||||
dupfile should trap an AttributeError and return f if no mode is supplied.
|
dupfile should trap an AttributeError and return f if no mode is supplied.
|
||||||
|
@ -625,34 +642,36 @@ def test_dupfile_no_mode():
|
||||||
def fileno(self):
|
def fileno(self):
|
||||||
return 1
|
return 1
|
||||||
tmpfile = SomeFileWrapper()
|
tmpfile = SomeFileWrapper()
|
||||||
assert py.io.dupfile(tmpfile) is tmpfile
|
assert capture.dupfile(tmpfile) is tmpfile
|
||||||
with py.test.raises(AttributeError):
|
with pytest.raises(AttributeError):
|
||||||
py.io.dupfile(tmpfile, raising=True)
|
capture.dupfile(tmpfile, raising=True)
|
||||||
|
|
||||||
|
|
||||||
def lsof_check(func):
|
def lsof_check(func):
|
||||||
pid = os.getpid()
|
pid = os.getpid()
|
||||||
try:
|
try:
|
||||||
out = py.process.cmdexec("lsof -p %d" % pid)
|
out = py.process.cmdexec("lsof -p %d" % pid)
|
||||||
except py.process.cmdexec.Error:
|
except py.process.cmdexec.Error:
|
||||||
py.test.skip("could not run 'lsof'")
|
pytest.skip("could not run 'lsof'")
|
||||||
func()
|
func()
|
||||||
out2 = py.process.cmdexec("lsof -p %d" % pid)
|
out2 = py.process.cmdexec("lsof -p %d" % pid)
|
||||||
len1 = len([x for x in out.split("\n") if "REG" in x])
|
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])
|
len2 = len([x for x in out2.split("\n") if "REG" in x])
|
||||||
assert len2 < len1 + 3, out2
|
assert len2 < len1 + 3, out2
|
||||||
|
|
||||||
|
|
||||||
class TestFDCapture:
|
class TestFDCapture:
|
||||||
pytestmark = needsdup
|
pytestmark = needsdup
|
||||||
|
|
||||||
def test_not_now(self, tmpfile):
|
def test_not_now(self, tmpfile):
|
||||||
fd = tmpfile.fileno()
|
fd = tmpfile.fileno()
|
||||||
cap = py.io.FDCapture(fd, now=False)
|
cap = capture.FDCapture(fd, now=False)
|
||||||
data = tobytes("hello")
|
data = tobytes("hello")
|
||||||
os.write(fd, data)
|
os.write(fd, data)
|
||||||
f = cap.done()
|
f = cap.done()
|
||||||
s = f.read()
|
s = f.read()
|
||||||
assert not s
|
assert not s
|
||||||
cap = py.io.FDCapture(fd, now=False)
|
cap = capture.FDCapture(fd, now=False)
|
||||||
cap.start()
|
cap.start()
|
||||||
os.write(fd, data)
|
os.write(fd, data)
|
||||||
f = cap.done()
|
f = cap.done()
|
||||||
|
@ -661,7 +680,7 @@ class TestFDCapture:
|
||||||
|
|
||||||
def test_simple(self, tmpfile):
|
def test_simple(self, tmpfile):
|
||||||
fd = tmpfile.fileno()
|
fd = tmpfile.fileno()
|
||||||
cap = py.io.FDCapture(fd)
|
cap = capture.FDCapture(fd)
|
||||||
data = tobytes("hello")
|
data = tobytes("hello")
|
||||||
os.write(fd, data)
|
os.write(fd, data)
|
||||||
f = cap.done()
|
f = cap.done()
|
||||||
|
@ -678,13 +697,13 @@ class TestFDCapture:
|
||||||
|
|
||||||
def test_simple_fail_second_start(self, tmpfile):
|
def test_simple_fail_second_start(self, tmpfile):
|
||||||
fd = tmpfile.fileno()
|
fd = tmpfile.fileno()
|
||||||
cap = py.io.FDCapture(fd)
|
cap = capture.FDCapture(fd)
|
||||||
f = cap.done()
|
f = cap.done()
|
||||||
py.test.raises(ValueError, cap.start)
|
pytest.raises(ValueError, cap.start)
|
||||||
f.close()
|
f.close()
|
||||||
|
|
||||||
def test_stderr(self):
|
def test_stderr(self):
|
||||||
cap = py.io.FDCapture(2, patchsys=True)
|
cap = capture.FDCapture(2, patchsys=True)
|
||||||
print_("hello", file=sys.stderr)
|
print_("hello", file=sys.stderr)
|
||||||
f = cap.done()
|
f = cap.done()
|
||||||
s = f.read()
|
s = f.read()
|
||||||
|
@ -693,17 +712,17 @@ class TestFDCapture:
|
||||||
def test_stdin(self, tmpfile):
|
def test_stdin(self, tmpfile):
|
||||||
tmpfile.write(tobytes("3"))
|
tmpfile.write(tobytes("3"))
|
||||||
tmpfile.seek(0)
|
tmpfile.seek(0)
|
||||||
cap = py.io.FDCapture(0, tmpfile=tmpfile)
|
cap = capture.FDCapture(0, tmpfile=tmpfile)
|
||||||
# check with os.read() directly instead of raw_input(), because
|
# check with os.read() directly instead of raw_input(), because
|
||||||
# sys.stdin itself may be redirected (as py.test now does by default)
|
# sys.stdin itself may be redirected (as pytest now does by default)
|
||||||
x = os.read(0, 100).strip()
|
x = os.read(0, 100).strip()
|
||||||
f = cap.done()
|
cap.done()
|
||||||
assert x == tobytes("3")
|
assert x == tobytes("3")
|
||||||
|
|
||||||
def test_writeorg(self, tmpfile):
|
def test_writeorg(self, tmpfile):
|
||||||
data1, data2 = tobytes("foo"), tobytes("bar")
|
data1, data2 = tobytes("foo"), tobytes("bar")
|
||||||
try:
|
try:
|
||||||
cap = py.io.FDCapture(tmpfile.fileno())
|
cap = capture.FDCapture(tmpfile.fileno())
|
||||||
tmpfile.write(data1)
|
tmpfile.write(data1)
|
||||||
cap.writeorg(data2)
|
cap.writeorg(data2)
|
||||||
finally:
|
finally:
|
||||||
|
@ -717,7 +736,7 @@ class TestFDCapture:
|
||||||
|
|
||||||
class TestStdCapture:
|
class TestStdCapture:
|
||||||
def getcapture(self, **kw):
|
def getcapture(self, **kw):
|
||||||
return py.io.StdCapture(**kw)
|
return capture.StdCapture(**kw)
|
||||||
|
|
||||||
def test_capturing_done_simple(self):
|
def test_capturing_done_simple(self):
|
||||||
cap = self.getcapture()
|
cap = self.getcapture()
|
||||||
|
@ -756,8 +775,8 @@ class TestStdCapture:
|
||||||
out, err = cap.readouterr()
|
out, err = cap.readouterr()
|
||||||
assert out == py.builtin._totext("hx\xc4\x85\xc4\x87\n", "utf8")
|
assert out == py.builtin._totext("hx\xc4\x85\xc4\x87\n", "utf8")
|
||||||
|
|
||||||
@py.test.mark.skipif('sys.version_info >= (3,)',
|
@pytest.mark.skipif('sys.version_info >= (3,)',
|
||||||
reason='text output different for bytes on python3')
|
reason='text output different for bytes on python3')
|
||||||
def test_capturing_readouterr_decode_error_handling(self):
|
def test_capturing_readouterr_decode_error_handling(self):
|
||||||
cap = self.getcapture()
|
cap = self.getcapture()
|
||||||
# triggered a internal error in pytest
|
# triggered a internal error in pytest
|
||||||
|
@ -778,7 +797,7 @@ class TestStdCapture:
|
||||||
cap = self.getcapture()
|
cap = self.getcapture()
|
||||||
print ("hello")
|
print ("hello")
|
||||||
out, err = cap.reset()
|
out, err = cap.reset()
|
||||||
py.test.raises(ValueError, cap.reset)
|
pytest.raises(ValueError, cap.reset)
|
||||||
assert out == "hello\n"
|
assert out == "hello\n"
|
||||||
assert not err
|
assert not err
|
||||||
|
|
||||||
|
@ -788,8 +807,8 @@ class TestStdCapture:
|
||||||
cap = self.getcapture()
|
cap = self.getcapture()
|
||||||
sys.stdout.write("hello")
|
sys.stdout.write("hello")
|
||||||
sys.stderr.write("world")
|
sys.stderr.write("world")
|
||||||
sys.stdout = py.io.TextIO()
|
sys.stdout = capture.TextIO()
|
||||||
sys.stderr = py.io.TextIO()
|
sys.stderr = capture.TextIO()
|
||||||
print ("not seen")
|
print ("not seen")
|
||||||
sys.stderr.write("not seen\n")
|
sys.stderr.write("not seen\n")
|
||||||
out, err = cap.reset()
|
out, err = cap.reset()
|
||||||
|
@ -837,7 +856,7 @@ class TestStdCapture:
|
||||||
print ("XXX which indicates an error in the underlying capturing")
|
print ("XXX which indicates an error in the underlying capturing")
|
||||||
print ("XXX mechanisms")
|
print ("XXX mechanisms")
|
||||||
cap = self.getcapture()
|
cap = self.getcapture()
|
||||||
py.test.raises(IOError, "sys.stdin.read()")
|
pytest.raises(IOError, "sys.stdin.read()")
|
||||||
out, err = cap.reset()
|
out, err = cap.reset()
|
||||||
|
|
||||||
def test_suspend_resume(self):
|
def test_suspend_resume(self):
|
||||||
|
@ -858,18 +877,20 @@ class TestStdCapture:
|
||||||
assert out == "after\n"
|
assert out == "after\n"
|
||||||
assert not err
|
assert not err
|
||||||
|
|
||||||
|
|
||||||
class TestStdCaptureNotNow(TestStdCapture):
|
class TestStdCaptureNotNow(TestStdCapture):
|
||||||
def getcapture(self, **kw):
|
def getcapture(self, **kw):
|
||||||
kw['now'] = False
|
kw['now'] = False
|
||||||
cap = py.io.StdCapture(**kw)
|
cap = capture.StdCapture(**kw)
|
||||||
cap.startall()
|
cap.startall()
|
||||||
return cap
|
return cap
|
||||||
|
|
||||||
|
|
||||||
class TestStdCaptureFD(TestStdCapture):
|
class TestStdCaptureFD(TestStdCapture):
|
||||||
pytestmark = needsdup
|
pytestmark = needsdup
|
||||||
|
|
||||||
def getcapture(self, **kw):
|
def getcapture(self, **kw):
|
||||||
return py.io.StdCaptureFD(**kw)
|
return capture.StdCaptureFD(**kw)
|
||||||
|
|
||||||
def test_intermingling(self):
|
def test_intermingling(self):
|
||||||
cap = self.getcapture()
|
cap = self.getcapture()
|
||||||
|
@ -888,10 +909,10 @@ class TestStdCaptureFD(TestStdCapture):
|
||||||
def test_callcapture(self):
|
def test_callcapture(self):
|
||||||
def func(x, y):
|
def func(x, y):
|
||||||
print (x)
|
print (x)
|
||||||
py.std.sys.stderr.write(str(y))
|
sys.stderr.write(str(y))
|
||||||
return 42
|
return 42
|
||||||
|
|
||||||
res, out, err = py.io.StdCaptureFD.call(func, 3, y=4)
|
res, out, err = capture.StdCaptureFD.call(func, 3, y=4)
|
||||||
assert res == 42
|
assert res == 42
|
||||||
assert out.startswith("3")
|
assert out.startswith("3")
|
||||||
assert err.startswith("4")
|
assert err.startswith("4")
|
||||||
|
@ -899,60 +920,67 @@ class TestStdCaptureFD(TestStdCapture):
|
||||||
def test_many(self, capfd):
|
def test_many(self, capfd):
|
||||||
def f():
|
def f():
|
||||||
for i in range(10):
|
for i in range(10):
|
||||||
cap = py.io.StdCaptureFD()
|
cap = capture.StdCaptureFD()
|
||||||
cap.reset()
|
cap.reset()
|
||||||
lsof_check(f)
|
lsof_check(f)
|
||||||
|
|
||||||
|
|
||||||
class TestStdCaptureFDNotNow(TestStdCaptureFD):
|
class TestStdCaptureFDNotNow(TestStdCaptureFD):
|
||||||
pytestmark = needsdup
|
pytestmark = needsdup
|
||||||
|
|
||||||
def getcapture(self, **kw):
|
def getcapture(self, **kw):
|
||||||
kw['now'] = False
|
kw['now'] = False
|
||||||
cap = py.io.StdCaptureFD(**kw)
|
cap = capture.StdCaptureFD(**kw)
|
||||||
cap.startall()
|
cap.startall()
|
||||||
return cap
|
return cap
|
||||||
|
|
||||||
|
|
||||||
@needsdup
|
@needsdup
|
||||||
def test_stdcapture_fd_tmpfile(tmpfile):
|
def test_stdcapture_fd_tmpfile(tmpfile):
|
||||||
capfd = py.io.StdCaptureFD(out=tmpfile)
|
capfd = capture.StdCaptureFD(out=tmpfile)
|
||||||
os.write(1, "hello".encode("ascii"))
|
os.write(1, "hello".encode("ascii"))
|
||||||
os.write(2, "world".encode("ascii"))
|
os.write(2, "world".encode("ascii"))
|
||||||
outf, errf = capfd.done()
|
outf, errf = capfd.done()
|
||||||
assert outf == tmpfile
|
assert outf == tmpfile
|
||||||
|
|
||||||
|
|
||||||
class TestStdCaptureFDinvalidFD:
|
class TestStdCaptureFDinvalidFD:
|
||||||
pytestmark = needsdup
|
pytestmark = needsdup
|
||||||
|
|
||||||
def test_stdcapture_fd_invalid_fd(self, testdir):
|
def test_stdcapture_fd_invalid_fd(self, testdir):
|
||||||
testdir.makepyfile("""
|
testdir.makepyfile("""
|
||||||
import py, os
|
import os
|
||||||
|
from _pytest.capture import StdCaptureFD
|
||||||
def test_stdout():
|
def test_stdout():
|
||||||
os.close(1)
|
os.close(1)
|
||||||
cap = py.io.StdCaptureFD(out=True, err=False, in_=False)
|
cap = StdCaptureFD(out=True, err=False, in_=False)
|
||||||
cap.done()
|
cap.done()
|
||||||
def test_stderr():
|
def test_stderr():
|
||||||
os.close(2)
|
os.close(2)
|
||||||
cap = py.io.StdCaptureFD(out=False, err=True, in_=False)
|
cap = StdCaptureFD(out=False, err=True, in_=False)
|
||||||
cap.done()
|
cap.done()
|
||||||
def test_stdin():
|
def test_stdin():
|
||||||
os.close(0)
|
os.close(0)
|
||||||
cap = py.io.StdCaptureFD(out=False, err=False, in_=True)
|
cap = StdCaptureFD(out=False, err=False, in_=True)
|
||||||
cap.done()
|
cap.done()
|
||||||
""")
|
""")
|
||||||
result = testdir.runpytest("--capture=fd")
|
result = testdir.runpytest("--capture=fd")
|
||||||
assert result.ret == 0
|
assert result.ret == 0
|
||||||
assert result.parseoutcomes()['passed'] == 3
|
assert result.parseoutcomes()['passed'] == 3
|
||||||
|
|
||||||
|
|
||||||
def test_capture_not_started_but_reset():
|
def test_capture_not_started_but_reset():
|
||||||
capsys = py.io.StdCapture(now=False)
|
capsys = capture.StdCapture(now=False)
|
||||||
capsys.done()
|
capsys.done()
|
||||||
capsys.done()
|
capsys.done()
|
||||||
capsys.reset()
|
capsys.reset()
|
||||||
|
|
||||||
|
|
||||||
@needsdup
|
@needsdup
|
||||||
def test_capture_no_sys():
|
def test_capture_no_sys():
|
||||||
capsys = py.io.StdCapture()
|
capsys = capture.StdCapture()
|
||||||
try:
|
try:
|
||||||
cap = py.io.StdCaptureFD(patchsys=False)
|
cap = capture.StdCaptureFD(patchsys=False)
|
||||||
sys.stdout.write("hello")
|
sys.stdout.write("hello")
|
||||||
sys.stderr.write("world")
|
sys.stderr.write("world")
|
||||||
oswritebytes(1, "1")
|
oswritebytes(1, "1")
|
||||||
|
@ -963,6 +991,7 @@ def test_capture_no_sys():
|
||||||
finally:
|
finally:
|
||||||
capsys.reset()
|
capsys.reset()
|
||||||
|
|
||||||
|
|
||||||
@needsdup
|
@needsdup
|
||||||
def test_callcapture_nofd():
|
def test_callcapture_nofd():
|
||||||
def func(x, y):
|
def func(x, y):
|
||||||
|
@ -972,21 +1001,22 @@ def test_callcapture_nofd():
|
||||||
sys.stderr.write(str(y))
|
sys.stderr.write(str(y))
|
||||||
return 42
|
return 42
|
||||||
|
|
||||||
capfd = py.io.StdCaptureFD(patchsys=False)
|
capfd = capture.StdCaptureFD(patchsys=False)
|
||||||
try:
|
try:
|
||||||
res, out, err = py.io.StdCapture.call(func, 3, y=4)
|
res, out, err = capture.StdCapture.call(func, 3, y=4)
|
||||||
finally:
|
finally:
|
||||||
capfd.reset()
|
capfd.reset()
|
||||||
assert res == 42
|
assert res == 42
|
||||||
assert out.startswith("3")
|
assert out.startswith("3")
|
||||||
assert err.startswith("4")
|
assert err.startswith("4")
|
||||||
|
|
||||||
|
|
||||||
@needsdup
|
@needsdup
|
||||||
@py.test.mark.parametrize('use', [True, False])
|
@pytest.mark.parametrize('use', [True, False])
|
||||||
def test_fdcapture_tmpfile_remains_the_same(tmpfile, use):
|
def test_fdcapture_tmpfile_remains_the_same(tmpfile, use):
|
||||||
if not use:
|
if not use:
|
||||||
tmpfile = True
|
tmpfile = True
|
||||||
cap = py.io.StdCaptureFD(out=False, err=tmpfile, now=False)
|
cap = capture.StdCaptureFD(out=False, err=tmpfile, now=False)
|
||||||
cap.startall()
|
cap.startall()
|
||||||
capfile = cap.err.tmpfile
|
capfile = cap.err.tmpfile
|
||||||
cap.suspend()
|
cap.suspend()
|
||||||
|
@ -994,15 +1024,17 @@ def test_fdcapture_tmpfile_remains_the_same(tmpfile, use):
|
||||||
capfile2 = cap.err.tmpfile
|
capfile2 = cap.err.tmpfile
|
||||||
assert capfile2 == capfile
|
assert capfile2 == capfile
|
||||||
|
|
||||||
@py.test.mark.parametrize('method', ['StdCapture', 'StdCaptureFD'])
|
|
||||||
|
@pytest.mark.parametrize('method', ['StdCapture', 'StdCaptureFD'])
|
||||||
def test_capturing_and_logging_fundamentals(testdir, method):
|
def test_capturing_and_logging_fundamentals(testdir, method):
|
||||||
if method == "StdCaptureFD" and not hasattr(os, 'dup'):
|
if method == "StdCaptureFD" and not hasattr(os, 'dup'):
|
||||||
py.test.skip("need os.dup")
|
pytest.skip("need os.dup")
|
||||||
# here we check a fundamental feature
|
# here we check a fundamental feature
|
||||||
p = testdir.makepyfile("""
|
p = testdir.makepyfile("""
|
||||||
import sys, os
|
import sys, os
|
||||||
import py, logging
|
import py, logging
|
||||||
cap = py.io.%s(out=False, in_=False)
|
from _pytest import capture
|
||||||
|
cap = capture.%s(out=False, in_=False)
|
||||||
|
|
||||||
logging.warn("hello1")
|
logging.warn("hello1")
|
||||||
outerr = cap.suspend()
|
outerr = cap.suspend()
|
||||||
|
|
Loading…
Reference in New Issue