[svn r58300] adding option to specify namespace for ifraises execution.

--HG--
branch : trunk
This commit is contained in:
hpk 2008-09-21 15:43:01 +02:00
parent 13c7fb6709
commit fa5c975c00
3 changed files with 12 additions and 7 deletions

View File

@ -49,19 +49,22 @@ def exit(msg):
__tracebackhide__ = True __tracebackhide__ = True
raise Exit(msg) raise Exit(msg)
def skip(msg="", ifraises=None): def skip(msg="", ifraises=None, ns=None):
""" (conditionally) skip this test/module/conftest. """ (conditionally) skip this test/module/conftest.
msg: use this message when skipping.
ifraises: ifraises:
if "exec ifraises in {'py': py}" raises an exception if "exec ifraises in {'py': py}" raises an exception
skip this test. skip this test.
msg: use this message when skipping. ns: use this namespace when executing ifraises
""" """
__tracebackhide__ = True __tracebackhide__ = True
if ifraises is not None: if ifraises is not None:
ifraises = py.code.Source(ifraises).compile() ifraises = py.code.Source(ifraises).compile()
if ns is None:
ns = {}
try: try:
exec ifraises in {'py': py} exec ifraises in ns
except (KeyboardInterrupt, SystemExit): except (KeyboardInterrupt, SystemExit):
raise raise
except Exception, e: except Exception, e:

View File

@ -387,10 +387,7 @@ class TestPyTest(AcceptBase):
class TestInteractive(AcceptBase): class TestInteractive(AcceptBase):
def getspawn(self): def getspawn(self):
try: py.test.skip(ifraises="import pexpect", ns=globals())
import pexpect
except ImportError:
py.test.skip("cannot import pexpect")
def spawn(cmd): def spawn(cmd):
return pexpect.spawn(cmd, logfile=self.tmpdir.join("spawn.out").open("w")) return pexpect.spawn(cmd, logfile=self.tmpdir.join("spawn.out").open("w"))
return spawn return spawn

View File

@ -76,6 +76,11 @@ def test_skip_ifraises():
assert excinfo.traceback[-1].ishidden() assert excinfo.traceback[-1].ishidden()
assert excinfo.value.msg.startswith("ImportError") assert excinfo.value.msg.startswith("ImportError")
def test_skip_ifraises_ns():
d = {}
py.test.skip(ns=d, ifraises="import py")
assert d['py'] == py
def test_skip_ifraises_syntaxerror(): def test_skip_ifraises_syntaxerror():
try: try:
excinfo = py.test.raises(SyntaxError, ''' excinfo = py.test.raises(SyntaxError, '''