refine and unify initial capturing - now works also if the logging module
is already used from an early-loaded conftest.py file (prior to option parsing)
This commit is contained in:
parent
b8f0d10f80
commit
d8d88ede65
|
@ -1,6 +1,9 @@
|
||||||
Changes between 2.0.0 and 2.0.1.devX
|
Changes between 2.0.0 and 2.0.1.devX
|
||||||
----------------------------------------------
|
----------------------------------------------
|
||||||
|
|
||||||
|
- refine and unify initial capturing so that it works nicely
|
||||||
|
even if the logging module is used on an early-loaded conftest.py
|
||||||
|
file or plugin.
|
||||||
- fix issue12 - show plugin versions with "--version" and
|
- fix issue12 - show plugin versions with "--version" and
|
||||||
"--traceconfig" and also document how to add extra information
|
"--traceconfig" and also document how to add extra information
|
||||||
to reporting test header
|
to reporting test header
|
||||||
|
|
|
@ -19,10 +19,8 @@ def addouterr(rep, outerr):
|
||||||
if content:
|
if content:
|
||||||
repr.addsection("Captured std%s" % secname, content.rstrip())
|
repr.addsection("Captured std%s" % secname, content.rstrip())
|
||||||
|
|
||||||
def pytest_configure(config):
|
|
||||||
config.pluginmanager.register(CaptureManager(), 'capturemanager')
|
|
||||||
|
|
||||||
def pytest_unconfigure(config):
|
def pytest_unconfigure(config):
|
||||||
|
# registered in config.py during early conftest.py loading
|
||||||
capman = config.pluginmanager.getplugin('capturemanager')
|
capman = config.pluginmanager.getplugin('capturemanager')
|
||||||
while capman._method2capture:
|
while capman._method2capture:
|
||||||
name, cap = capman._method2capture.popitem()
|
name, cap = capman._method2capture.popitem()
|
||||||
|
@ -67,6 +65,14 @@ class CaptureManager:
|
||||||
else:
|
else:
|
||||||
raise ValueError("unknown capturing method: %r" % method)
|
raise ValueError("unknown capturing method: %r" % method)
|
||||||
|
|
||||||
|
def _getmethod_preoptionparse(self, args):
|
||||||
|
if '-s' in args or "--capture=no" in args:
|
||||||
|
return "no"
|
||||||
|
elif hasattr(os, 'dup') and '--capture=sys' not in args:
|
||||||
|
return "fd"
|
||||||
|
else:
|
||||||
|
return "sys"
|
||||||
|
|
||||||
def _getmethod(self, config, fspath):
|
def _getmethod(self, config, fspath):
|
||||||
if config.option.capture:
|
if config.option.capture:
|
||||||
method = config.option.capture
|
method = config.option.capture
|
||||||
|
|
|
@ -270,13 +270,16 @@ class Config(object):
|
||||||
|
|
||||||
def _setinitialconftest(self, args):
|
def _setinitialconftest(self, args):
|
||||||
# capture output during conftest init (#issue93)
|
# capture output during conftest init (#issue93)
|
||||||
name = hasattr(os, 'dup') and 'StdCaptureFD' or 'StdCapture'
|
from _pytest.capture import CaptureManager
|
||||||
cap = getattr(py.io, name)()
|
capman = CaptureManager()
|
||||||
|
self.pluginmanager.register(capman, 'capturemanager')
|
||||||
|
# will be unregistered in capture.py's unconfigure()
|
||||||
|
capman.resumecapture(capman._getmethod_preoptionparse(args))
|
||||||
try:
|
try:
|
||||||
try:
|
try:
|
||||||
self._conftest.setinitial(args)
|
self._conftest.setinitial(args)
|
||||||
finally:
|
finally:
|
||||||
out, err = cap.reset()
|
out, err = capman.suspendcapture() # logging might have got it
|
||||||
except:
|
except:
|
||||||
sys.stdout.write(out)
|
sys.stdout.write(out)
|
||||||
sys.stderr.write(err)
|
sys.stderr.write(err)
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
"""
|
"""
|
||||||
unit and functional testing with Python.
|
unit and functional testing with Python.
|
||||||
"""
|
"""
|
||||||
__version__ = '2.0.1.dev8'
|
__version__ = '2.0.1.dev9'
|
||||||
__all__ = ['main']
|
__all__ = ['main']
|
||||||
|
|
||||||
from _pytest.core import main, UsageError, _preloadplugins
|
from _pytest.core import main, UsageError, _preloadplugins
|
||||||
|
|
2
setup.py
2
setup.py
|
@ -22,7 +22,7 @@ def main():
|
||||||
name='pytest',
|
name='pytest',
|
||||||
description='py.test: simple powerful testing with Python',
|
description='py.test: simple powerful testing with Python',
|
||||||
long_description = long_description,
|
long_description = long_description,
|
||||||
version='2.0.1.dev8',
|
version='2.0.1.dev9',
|
||||||
url='http://pytest.org',
|
url='http://pytest.org',
|
||||||
license='MIT license',
|
license='MIT license',
|
||||||
platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'],
|
platforms=['unix', 'linux', 'osx', 'cygwin', 'win32'],
|
||||||
|
|
|
@ -325,6 +325,40 @@ class TestLoggingInteraction:
|
||||||
])
|
])
|
||||||
assert 'operation on closed file' not in result.stderr.str()
|
assert 'operation on closed file' not in result.stderr.str()
|
||||||
|
|
||||||
|
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()
|
||||||
|
|
||||||
|
|
||||||
class TestCaptureFuncarg:
|
class TestCaptureFuncarg:
|
||||||
def test_std_functional(self, testdir):
|
def test_std_functional(self, testdir):
|
||||||
|
|
Loading…
Reference in New Issue