2014-01-23 05:15:40 +08:00
|
|
|
import pytest, py, os, sys
|
2010-11-13 18:10:45 +08:00
|
|
|
from _pytest.capture import CaptureManager
|
2009-07-31 20:21:02 +08:00
|
|
|
|
2010-11-18 05:12:16 +08:00
|
|
|
needsosdup = pytest.mark.xfail("not hasattr(os, 'dup')")
|
2009-10-28 04:34:11 +08:00
|
|
|
|
2009-07-31 20:21:02 +08:00
|
|
|
class TestCaptureManager:
|
2009-09-05 22:54:52 +08:00
|
|
|
def test_getmethod_default_no_fd(self, testdir, monkeypatch):
|
|
|
|
config = testdir.parseconfig(testdir.tmpdir)
|
|
|
|
assert config.getvalue("capture") is None
|
|
|
|
capman = CaptureManager()
|
2009-10-28 04:34:11 +08:00
|
|
|
monkeypatch.delattr(os, 'dup', raising=False)
|
2009-09-05 22:54:52 +08:00
|
|
|
try:
|
2010-07-27 03:15:15 +08:00
|
|
|
assert capman._getmethod(config, None) == "sys"
|
2009-09-05 22:54:52 +08:00
|
|
|
finally:
|
2009-09-06 05:21:58 +08:00
|
|
|
monkeypatch.undo()
|
2009-09-05 22:54:52 +08:00
|
|
|
|
2012-10-04 17:51:14 +08:00
|
|
|
@pytest.mark.parametrize("mode", "no fd sys".split())
|
|
|
|
def test_configure_per_fspath(self, testdir, mode):
|
2009-07-31 20:21:02 +08:00
|
|
|
config = testdir.parseconfig(testdir.tmpdir)
|
|
|
|
capman = CaptureManager()
|
2009-10-28 04:34:11 +08:00
|
|
|
hasfd = hasattr(os, 'dup')
|
|
|
|
if hasfd:
|
2010-07-27 03:15:15 +08:00
|
|
|
assert capman._getmethod(config, None) == "fd"
|
2009-10-28 04:34:11 +08:00
|
|
|
else:
|
2010-07-27 03:15:15 +08:00
|
|
|
assert capman._getmethod(config, None) == "sys"
|
2009-07-31 20:21:02 +08:00
|
|
|
|
2012-10-04 17:51:14 +08:00
|
|
|
if not hasfd and mode == 'fd':
|
|
|
|
return
|
|
|
|
sub = testdir.tmpdir.mkdir("dir" + mode)
|
|
|
|
sub.ensure("__init__.py")
|
|
|
|
sub.join("conftest.py").write('option_capture = %r' % mode)
|
|
|
|
assert capman._getmethod(config, sub.join("test_hello.py")) == mode
|
2009-07-31 20:21:02 +08:00
|
|
|
|
2009-10-28 04:34:11 +08:00
|
|
|
@needsosdup
|
2013-11-21 21:40:14 +08:00
|
|
|
@pytest.mark.parametrize("method", ['no', 'fd', 'sys'])
|
2009-07-31 20:21:02 +08:00
|
|
|
def test_capturing_basic_api(self, method):
|
2014-01-23 05:15:40 +08:00
|
|
|
capouter = py.io.StdCaptureFD()
|
2009-07-31 20:21:02 +08:00
|
|
|
old = sys.stdout, sys.stderr, sys.stdin
|
|
|
|
try:
|
|
|
|
capman = CaptureManager()
|
2010-05-18 01:00:39 +08:00
|
|
|
# call suspend without resume or start
|
|
|
|
outerr = capman.suspendcapture()
|
|
|
|
outerr = capman.suspendcapture()
|
|
|
|
assert outerr == ("", "")
|
2009-07-31 20:21:02 +08:00
|
|
|
capman.resumecapture(method)
|
2009-08-29 21:51:49 +08:00
|
|
|
print ("hello")
|
2009-07-31 20:21:02 +08:00
|
|
|
out, err = capman.suspendcapture()
|
|
|
|
if method == "no":
|
|
|
|
assert old == (sys.stdout, sys.stderr, sys.stdin)
|
|
|
|
else:
|
|
|
|
assert out == "hello\n"
|
|
|
|
capman.resumecapture(method)
|
|
|
|
out, err = capman.suspendcapture()
|
2010-07-27 03:15:15 +08:00
|
|
|
assert not out and not err
|
2011-11-08 02:08:41 +08:00
|
|
|
capman.reset_capturings()
|
2009-07-31 20:21:02 +08:00
|
|
|
finally:
|
|
|
|
capouter.reset()
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2009-10-28 04:34:11 +08:00
|
|
|
@needsosdup
|
2009-07-31 20:21:02 +08:00
|
|
|
def test_juggle_capturings(self, testdir):
|
2014-01-23 05:15:40 +08:00
|
|
|
capouter = py.io.StdCaptureFD()
|
2009-07-31 20:21:02 +08:00
|
|
|
try:
|
2011-11-08 02:08:41 +08:00
|
|
|
#config = testdir.parseconfig(testdir.tmpdir)
|
2009-07-31 20:21:02 +08:00
|
|
|
capman = CaptureManager()
|
2011-11-08 02:08:41 +08:00
|
|
|
try:
|
|
|
|
capman.resumecapture("fd")
|
|
|
|
pytest.raises(ValueError, 'capman.resumecapture("fd")')
|
|
|
|
pytest.raises(ValueError, 'capman.resumecapture("sys")')
|
|
|
|
os.write(1, "hello\n".encode('ascii'))
|
|
|
|
out, err = capman.suspendcapture()
|
|
|
|
assert out == "hello\n"
|
|
|
|
capman.resumecapture("sys")
|
|
|
|
os.write(1, "hello\n".encode('ascii'))
|
|
|
|
py.builtin.print_("world", file=sys.stderr)
|
|
|
|
out, err = capman.suspendcapture()
|
|
|
|
assert not out
|
|
|
|
assert err == "world\n"
|
|
|
|
finally:
|
|
|
|
capman.reset_capturings()
|
2009-07-31 20:21:02 +08:00
|
|
|
finally:
|
|
|
|
capouter.reset()
|
|
|
|
|
2010-11-26 03:06:42 +08:00
|
|
|
@pytest.mark.xfail("hasattr(sys, 'pypy_version_info')")
|
2013-11-21 21:40:14 +08:00
|
|
|
@pytest.mark.parametrize("method", ['fd', 'sys'])
|
2009-08-06 20:34:19 +08:00
|
|
|
def test_capturing_unicode(testdir, method):
|
2014-01-23 05:15:40 +08:00
|
|
|
if sys.version_info >= (3,0):
|
2009-09-01 22:10:21 +08:00
|
|
|
obj = "'b\u00f6y'"
|
|
|
|
else:
|
|
|
|
obj = "u'\u00f6y'"
|
2009-08-06 20:34:19 +08:00
|
|
|
testdir.makepyfile("""
|
2010-07-04 23:06:50 +08:00
|
|
|
# coding=utf8
|
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
|
2009-08-29 21:51:49 +08:00
|
|
|
print (sys.stdout)
|
2009-09-01 22:10:21 +08:00
|
|
|
print (%s)
|
|
|
|
""" % obj)
|
2009-08-06 20:34:19 +08:00
|
|
|
result = testdir.runpytest("--capture=%s" % method)
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*1 passed*"
|
|
|
|
])
|
|
|
|
|
2013-11-21 21:40:14 +08:00
|
|
|
@pytest.mark.parametrize("method", ['fd', 'sys'])
|
2009-08-19 22:42:29 +08:00
|
|
|
def test_capturing_bytes_in_utf8_encoding(testdir, method):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
def test_unicode():
|
2009-08-29 21:51:49 +08:00
|
|
|
print ('b\\u00f6y')
|
2009-08-19 22:42:29 +08:00
|
|
|
""")
|
|
|
|
result = testdir.runpytest("--capture=%s" % method)
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*1 passed*"
|
|
|
|
])
|
|
|
|
|
2009-07-31 20:21:02 +08:00
|
|
|
def test_collect_capturing(testdir):
|
|
|
|
p = testdir.makepyfile("""
|
2009-08-29 21:51:49 +08:00
|
|
|
print ("collect %s failure" % 13)
|
2009-07-31 20:21:02 +08:00
|
|
|
import xyz42123
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest(p)
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*Captured stdout*",
|
2010-07-27 03:15:15 +08:00
|
|
|
"*collect 13 failure*",
|
2009-07-31 20:21:02 +08:00
|
|
|
])
|
|
|
|
|
|
|
|
class TestPerTestCapturing:
|
|
|
|
def test_capture_and_fixtures(self, testdir):
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
def setup_module(mod):
|
2009-08-29 21:51:49 +08:00
|
|
|
print ("setup module")
|
2009-07-31 20:21:02 +08:00
|
|
|
def setup_function(function):
|
2009-08-29 21:51:49 +08:00
|
|
|
print ("setup " + function.__name__)
|
2009-07-31 20:21:02 +08:00
|
|
|
def test_func1():
|
2009-08-29 21:51:49 +08:00
|
|
|
print ("in func1")
|
2009-07-31 20:21:02 +08:00
|
|
|
assert 0
|
|
|
|
def test_func2():
|
2009-08-29 21:51:49 +08:00
|
|
|
print ("in func2")
|
2009-07-31 20:21:02 +08:00
|
|
|
assert 0
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest(p)
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"setup module*",
|
|
|
|
"setup test_func1*",
|
|
|
|
"in func1*",
|
|
|
|
"setup test_func2*",
|
2009-11-13 04:15:59 +08:00
|
|
|
"in func2*",
|
|
|
|
])
|
2009-07-31 20:21:02 +08:00
|
|
|
|
2010-11-18 05:12:16 +08:00
|
|
|
@pytest.mark.xfail
|
2009-08-03 18:16:12 +08:00
|
|
|
def test_capture_scope_cache(self, testdir):
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
import sys
|
|
|
|
def setup_module(func):
|
2009-08-29 21:51:49 +08:00
|
|
|
print ("module-setup")
|
2009-08-03 18:16:12 +08:00
|
|
|
def setup_function(func):
|
2009-08-29 21:51:49 +08:00
|
|
|
print ("function-setup")
|
2009-08-03 18:16:12 +08:00
|
|
|
def test_func():
|
2009-08-29 21:51:49 +08:00
|
|
|
print ("in function")
|
2009-08-03 18:16:12 +08:00
|
|
|
assert 0
|
|
|
|
def teardown_function(func):
|
2009-08-29 21:51:49 +08:00
|
|
|
print ("in teardown")
|
2009-08-03 18:16:12 +08:00
|
|
|
""")
|
|
|
|
result = testdir.runpytest(p)
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*test_func():*",
|
2010-07-27 03:15:15 +08:00
|
|
|
"*Captured stdout during setup*",
|
|
|
|
"module-setup*",
|
|
|
|
"function-setup*",
|
2009-08-03 18:16:12 +08:00
|
|
|
"*Captured stdout*",
|
|
|
|
"in teardown*",
|
|
|
|
])
|
|
|
|
|
2014-01-23 05:15:40 +08:00
|
|
|
|
2009-07-31 20:21:02 +08:00
|
|
|
def test_no_carry_over(self, testdir):
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
def test_func1():
|
2009-08-29 21:51:49 +08:00
|
|
|
print ("in func1")
|
2009-07-31 20:21:02 +08:00
|
|
|
def test_func2():
|
2009-08-29 21:51:49 +08:00
|
|
|
print ("in func2")
|
2009-07-31 20:21:02 +08:00
|
|
|
assert 0
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest(p)
|
|
|
|
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
|
|
|
|
2014-01-23 05:15:40 +08:00
|
|
|
|
2009-07-31 20:21:02 +08:00
|
|
|
def test_teardown_capturing(self, testdir):
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
def setup_function(function):
|
2009-08-29 21:51:49 +08:00
|
|
|
print ("setup func1")
|
2009-07-31 20:21:02 +08:00
|
|
|
def teardown_function(function):
|
2009-08-29 21:51:49 +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():
|
2009-08-29 21:51:49 +08:00
|
|
|
print ("in func1")
|
2009-07-31 20:21:02 +08:00
|
|
|
pass
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest(p)
|
2010-04-29 06:12:38 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
2009-07-31 20:21:02 +08:00
|
|
|
'*teardown_function*',
|
|
|
|
'*Captured stdout*',
|
|
|
|
"setup func1*",
|
|
|
|
"in func1*",
|
|
|
|
"teardown func1*",
|
|
|
|
#"*1 fixture failure*"
|
|
|
|
])
|
|
|
|
|
2011-12-03 05:00:21 +08:00
|
|
|
def test_teardown_capturing_final(self, testdir):
|
2009-07-31 20:21:02 +08:00
|
|
|
p = testdir.makepyfile("""
|
|
|
|
def teardown_module(mod):
|
2009-08-29 21:51:49 +08:00
|
|
|
print ("teardown module")
|
2009-07-31 20:21:02 +08:00
|
|
|
assert 0
|
|
|
|
def test_func():
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest(p)
|
2010-04-29 06:12:38 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
2010-07-27 03:15:15 +08:00
|
|
|
"*def teardown_module(mod):*",
|
|
|
|
"*Captured stdout*",
|
|
|
|
"*teardown module*",
|
|
|
|
"*1 error*",
|
2009-07-31 20:21:02 +08:00
|
|
|
])
|
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
def test_capturing_outerr(self, testdir):
|
2009-07-31 20:21:02 +08:00
|
|
|
p1 = testdir.makepyfile("""
|
2010-07-27 03:15:15 +08:00
|
|
|
import sys
|
2009-07-31 20:21:02 +08:00
|
|
|
def test_capturing():
|
2009-08-29 21:51:49 +08:00
|
|
|
print (42)
|
|
|
|
sys.stderr.write(str(23))
|
2009-07-31 20:21:02 +08:00
|
|
|
def test_capturing_error():
|
2009-08-29 21:51:49 +08:00
|
|
|
print (1)
|
|
|
|
sys.stderr.write(str(2))
|
2009-07-31 20:21:02 +08:00
|
|
|
raise ValueError
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest(p1)
|
|
|
|
result.stdout.fnmatch_lines([
|
2010-07-27 03:15:15 +08:00
|
|
|
"*test_capturing_outerr.py .F",
|
2009-07-31 20:21:02 +08:00
|
|
|
"====* FAILURES *====",
|
2010-07-27 03:15:15 +08:00
|
|
|
"____*____",
|
2009-07-31 20:21:02 +08:00
|
|
|
"*test_capturing_outerr.py:8: ValueError",
|
|
|
|
"*--- Captured stdout ---*",
|
|
|
|
"1",
|
|
|
|
"*--- Captured stderr ---*",
|
|
|
|
"2",
|
|
|
|
])
|
|
|
|
|
|
|
|
class TestLoggingInteraction:
|
|
|
|
def test_logging_stream_ownership(self, testdir):
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
def test_logging():
|
|
|
|
import logging
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
2014-01-23 05:15:40 +08:00
|
|
|
stream = py.io.TextIO()
|
2009-07-31 20:21:02 +08:00
|
|
|
logging.basicConfig(stream=stream)
|
|
|
|
stream.close() # to free memory/release resources
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest(p)
|
|
|
|
result.stderr.str().find("atexit") == -1
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2009-07-31 20:21:02 +08:00
|
|
|
def test_logging_and_immediate_setupteardown(self, testdir):
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
import logging
|
|
|
|
def setup_function(function):
|
|
|
|
logging.warn("hello1")
|
|
|
|
|
|
|
|
def test_logging():
|
|
|
|
logging.warn("hello2")
|
|
|
|
assert 0
|
|
|
|
|
|
|
|
def teardown_function(function):
|
|
|
|
logging.warn("hello3")
|
|
|
|
assert 0
|
|
|
|
""")
|
|
|
|
for optargs in (('--capture=sys',), ('--capture=fd',)):
|
2009-08-29 21:51:49 +08:00
|
|
|
print (optargs)
|
2009-07-31 20:21:02 +08:00
|
|
|
result = testdir.runpytest(p, *optargs)
|
|
|
|
s = result.stdout.str()
|
|
|
|
result.stdout.fnmatch_lines([
|
2009-07-31 20:22:02 +08:00
|
|
|
"*WARN*hello3", # errors show first!
|
2010-07-27 03:15:15 +08:00
|
|
|
"*WARN*hello1",
|
|
|
|
"*WARN*hello2",
|
2009-07-31 20:21:02 +08:00
|
|
|
])
|
|
|
|
# verify proper termination
|
|
|
|
assert "closed" not in s
|
|
|
|
|
|
|
|
def test_logging_and_crossscope_fixtures(self, testdir):
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
import logging
|
|
|
|
def setup_module(function):
|
|
|
|
logging.warn("hello1")
|
|
|
|
|
|
|
|
def test_logging():
|
|
|
|
logging.warn("hello2")
|
|
|
|
assert 0
|
|
|
|
|
|
|
|
def teardown_module(function):
|
|
|
|
logging.warn("hello3")
|
|
|
|
assert 0
|
|
|
|
""")
|
2009-08-03 18:16:12 +08:00
|
|
|
for optargs in (('--capture=sys',), ('--capture=fd',)):
|
2009-08-29 21:51:49 +08:00
|
|
|
print (optargs)
|
2009-07-31 20:21:02 +08:00
|
|
|
result = testdir.runpytest(p, *optargs)
|
|
|
|
s = result.stdout.str()
|
|
|
|
result.stdout.fnmatch_lines([
|
2009-08-03 18:16:12 +08:00
|
|
|
"*WARN*hello3", # errors come first
|
2010-07-27 03:15:15 +08:00
|
|
|
"*WARN*hello1",
|
|
|
|
"*WARN*hello2",
|
2009-07-31 20:21:02 +08:00
|
|
|
])
|
|
|
|
# verify proper termination
|
|
|
|
assert "closed" not in s
|
|
|
|
|
2010-12-06 23:56:12 +08:00
|
|
|
def test_logging_initialized_in_test(self, testdir):
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
import sys
|
|
|
|
def test_something():
|
|
|
|
# pytest does not import logging
|
|
|
|
assert 'logging' not in sys.modules
|
|
|
|
import logging
|
|
|
|
logging.basicConfig()
|
|
|
|
logging.warn("hello432")
|
|
|
|
assert 0
|
|
|
|
""")
|
2014-01-23 05:15:40 +08:00
|
|
|
result = testdir.runpytest(p, "--traceconfig",
|
2010-12-06 23:56:12 +08:00
|
|
|
"-p", "no:capturelog")
|
|
|
|
assert result.ret != 0
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*hello432*",
|
|
|
|
])
|
|
|
|
assert 'operation on closed file' not in result.stderr.str()
|
|
|
|
|
2011-01-18 19:51:21 +08:00
|
|
|
def test_conftestlogging_is_shown(self, testdir):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
import logging
|
|
|
|
logging.basicConfig()
|
|
|
|
logging.warn("hello435")
|
|
|
|
""")
|
|
|
|
# make sure that logging is still captured in tests
|
|
|
|
result = testdir.runpytest("-s", "-p", "no:capturelog")
|
|
|
|
assert result.ret == 0
|
|
|
|
result.stderr.fnmatch_lines([
|
|
|
|
"WARNING*hello435*",
|
|
|
|
])
|
|
|
|
assert 'operation on closed file' not in result.stderr.str()
|
|
|
|
|
|
|
|
def test_conftestlogging_and_test_logging(self, testdir):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
import logging
|
|
|
|
logging.basicConfig()
|
|
|
|
""")
|
|
|
|
# make sure that logging is still captured in tests
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
def test_hello():
|
|
|
|
import logging
|
|
|
|
logging.warn("hello433")
|
|
|
|
assert 0
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest(p, "-p", "no:capturelog")
|
|
|
|
assert result.ret != 0
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"WARNING*hello433*",
|
|
|
|
])
|
|
|
|
assert 'something' not in result.stderr.str()
|
|
|
|
assert 'operation on closed file' not in result.stderr.str()
|
|
|
|
|
2010-12-06 23:56:12 +08:00
|
|
|
|
2012-10-05 20:24:44 +08:00
|
|
|
class TestCaptureFixture:
|
2010-07-27 03:15:15 +08:00
|
|
|
def test_std_functional(self, testdir):
|
2009-07-31 20:21:02 +08:00
|
|
|
reprec = testdir.inline_runsource("""
|
|
|
|
def test_hello(capsys):
|
2009-08-29 21:51:49 +08:00
|
|
|
print (42)
|
2009-07-31 20:21:02 +08:00
|
|
|
out, err = capsys.readouterr()
|
|
|
|
assert out.startswith("42")
|
|
|
|
""")
|
|
|
|
reprec.assertoutcome(passed=1)
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2012-06-04 03:01:27 +08:00
|
|
|
def test_capsyscapfd(self, testdir):
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
def test_one(capsys, capfd):
|
|
|
|
pass
|
|
|
|
def test_two(capfd, capsys):
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest(p)
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*ERROR*setup*test_one*",
|
|
|
|
"*capsys*capfd*same*time*",
|
|
|
|
"*ERROR*setup*test_two*",
|
|
|
|
"*capsys*capfd*same*time*",
|
|
|
|
"*2 error*"])
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("method", ["sys", "fd"])
|
|
|
|
def test_capture_is_represented_on_failure_issue128(self, testdir, method):
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
def test_hello(cap%s):
|
|
|
|
print ("xxx42xxx")
|
|
|
|
assert 0
|
|
|
|
""" % method)
|
|
|
|
result = testdir.runpytest(p)
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"xxx42xxx",
|
|
|
|
])
|
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
@needsosdup
|
|
|
|
def test_stdfd_functional(self, testdir):
|
2009-07-31 20:21:02 +08:00
|
|
|
reprec = testdir.inline_runsource("""
|
|
|
|
def test_hello(capfd):
|
|
|
|
import os
|
2009-08-29 21:51:49 +08:00
|
|
|
os.write(1, "42".encode('ascii'))
|
2009-07-31 20:21:02 +08:00
|
|
|
out, err = capfd.readouterr()
|
|
|
|
assert out.startswith("42")
|
|
|
|
capfd.close()
|
|
|
|
""")
|
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
def test_partial_setup_failure(self, testdir):
|
2009-07-31 20:21:02 +08:00
|
|
|
p = testdir.makepyfile("""
|
2010-05-18 15:54:04 +08:00
|
|
|
def test_hello(capsys, missingarg):
|
2009-07-31 20:21:02 +08:00
|
|
|
pass
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest(p)
|
2010-04-29 06:12:38 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
2009-07-31 20:21:02 +08:00
|
|
|
"*test_partial_setup_failure*",
|
2009-07-31 20:22:02 +08:00
|
|
|
"*1 error*",
|
2009-07-31 20:21:02 +08:00
|
|
|
])
|
|
|
|
|
2009-10-28 04:34:11 +08:00
|
|
|
@needsosdup
|
2010-07-27 03:15:15 +08:00
|
|
|
def test_keyboardinterrupt_disables_capturing(self, testdir):
|
2009-07-31 20:21:02 +08:00
|
|
|
p = testdir.makepyfile("""
|
|
|
|
def test_hello(capfd):
|
|
|
|
import os
|
2009-08-29 21:51:49 +08:00
|
|
|
os.write(1, str(42).encode('ascii'))
|
2009-07-31 20:21:02 +08:00
|
|
|
raise KeyboardInterrupt()
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest(p)
|
|
|
|
result.stdout.fnmatch_lines([
|
2010-05-25 22:52:09 +08:00
|
|
|
"*KeyboardInterrupt*"
|
2009-07-31 20:21:02 +08:00
|
|
|
])
|
|
|
|
assert result.ret == 2
|
|
|
|
|
2013-03-25 15:53:08 +08:00
|
|
|
@pytest.mark.issue14
|
|
|
|
def test_capture_and_logging(self, testdir):
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
import logging
|
|
|
|
def test_log(capsys):
|
|
|
|
logging.error('x')
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest(p)
|
|
|
|
assert 'closed' not in result.stderr.str()
|
|
|
|
|
|
|
|
|
2009-12-30 07:11:27 +08:00
|
|
|
def test_setup_failure_does_not_kill_capturing(testdir):
|
|
|
|
sub1 = testdir.mkpydir("sub1")
|
|
|
|
sub1.join("conftest.py").write(py.code.Source("""
|
|
|
|
def pytest_runtest_setup(item):
|
|
|
|
raise ValueError(42)
|
|
|
|
"""))
|
|
|
|
sub1.join("test_mod.py").write("def test_func1(): pass")
|
|
|
|
result = testdir.runpytest(testdir.tmpdir, '--traceconfig')
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*ValueError(42)*",
|
|
|
|
"*1 error*"
|
|
|
|
])
|
2009-12-30 18:16:20 +08:00
|
|
|
|
|
|
|
def test_fdfuncarg_skips_on_no_osdup(testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import os
|
2010-04-28 21:24:38 +08:00
|
|
|
if hasattr(os, 'dup'):
|
|
|
|
del os.dup
|
2009-12-30 18:16:20 +08:00
|
|
|
def test_hello(capfd):
|
|
|
|
pass
|
|
|
|
""")
|
2010-05-18 22:01:58 +08:00
|
|
|
result = testdir.runpytest("--capture=no")
|
2009-12-30 18:16:20 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*1 skipped*"
|
|
|
|
])
|
2010-11-22 06:17:59 +08:00
|
|
|
|
|
|
|
def test_capture_conftest_runtest_setup(testdir):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
def pytest_runtest_setup():
|
|
|
|
print ("hello19")
|
|
|
|
""")
|
|
|
|
testdir.makepyfile("def test_func(): pass")
|
|
|
|
result = testdir.runpytest()
|
|
|
|
assert result.ret == 0
|
|
|
|
assert 'hello19' not in result.stdout.str()
|
2013-09-30 19:14:16 +08:00
|
|
|
|
|
|
|
def test_capture_early_option_parsing(testdir):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
def pytest_runtest_setup():
|
|
|
|
print ("hello19")
|
|
|
|
""")
|
|
|
|
testdir.makepyfile("def test_func(): pass")
|
|
|
|
result = testdir.runpytest("-vs")
|
|
|
|
assert result.ret == 0
|
|
|
|
assert 'hello19' in result.stdout.str()
|
2014-01-23 01:07:54 +08:00
|
|
|
|
|
|
|
@pytest.mark.xfail(sys.version_info >= (3, 0), reason='encoding issues')
|
|
|
|
def test_capture_binary_output(testdir):
|
2014-01-23 05:15:40 +08:00
|
|
|
testdir.makepyfile("""
|
2014-01-23 01:07:54 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
def test_a():
|
|
|
|
import sys
|
|
|
|
import subprocess
|
|
|
|
subprocess.call([sys.executable, __file__])
|
|
|
|
|
|
|
|
@pytest.mark.skip
|
|
|
|
def test_foo():
|
|
|
|
import os;os.write(1, b'\xc3')
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
test_foo()
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest('--assert=plain')
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
'*2 passed*',
|
|
|
|
])
|
2014-01-23 05:15:40 +08:00
|
|
|
|