2008-08-16 23:26:59 +08:00
|
|
|
|
|
|
|
import py
|
2009-09-05 03:47:49 +08:00
|
|
|
import sys
|
2008-08-16 23:26:59 +08:00
|
|
|
|
|
|
|
class TestRaises:
|
|
|
|
def test_raises(self):
|
|
|
|
py.test.raises(ValueError, "int('qwe')")
|
|
|
|
|
|
|
|
def test_raises_exec(self):
|
|
|
|
py.test.raises(ValueError, "a,x = []")
|
|
|
|
|
|
|
|
def test_raises_syntax_error(self):
|
|
|
|
py.test.raises(SyntaxError, "qwe qwe qwe")
|
|
|
|
|
|
|
|
def test_raises_function(self):
|
|
|
|
py.test.raises(ValueError, int, 'hello')
|
|
|
|
|
2009-05-22 19:01:48 +08:00
|
|
|
def test_pytest_exit():
|
|
|
|
try:
|
|
|
|
py.test.exit("hello")
|
|
|
|
except:
|
|
|
|
excinfo = py.code.ExceptionInfo()
|
|
|
|
assert excinfo.errisinstance(KeyboardInterrupt)
|
|
|
|
|
2009-09-05 03:47:49 +08:00
|
|
|
def test_exception_printing_skip():
|
|
|
|
try:
|
|
|
|
py.test.skip("hello")
|
|
|
|
except Exception:
|
|
|
|
excinfo = py.code.ExceptionInfo()
|
|
|
|
s = excinfo.exconly(tryshort=True)
|
|
|
|
assert s.startswith("Skipped")
|
2009-10-16 02:10:06 +08:00
|
|
|
|
|
|
|
def test_importorskip():
|
2009-11-05 04:34:07 +08:00
|
|
|
from py.impl.test.outcome import Skipped, importorskip
|
2009-10-16 02:10:06 +08:00
|
|
|
assert importorskip == py.test.importorskip
|
|
|
|
try:
|
|
|
|
sys = importorskip("sys")
|
|
|
|
assert sys == py.std.sys
|
|
|
|
#path = py.test.importorskip("os.path")
|
|
|
|
#assert path == py.std.os.path
|
|
|
|
py.test.raises(Skipped, "py.test.importorskip('alskdj')")
|
|
|
|
py.test.raises(SyntaxError, "py.test.importorskip('x y z')")
|
|
|
|
py.test.raises(SyntaxError, "py.test.importorskip('x=y')")
|
|
|
|
path = importorskip("py", minversion=".".join(py.__version__))
|
|
|
|
mod = py.std.types.ModuleType("hello123")
|
|
|
|
mod.__version__ = "1.3"
|
|
|
|
py.test.raises(Skipped, """
|
|
|
|
py.test.importorskip("hello123", minversion="5.0")
|
|
|
|
""")
|
|
|
|
except Skipped:
|
|
|
|
print(py.code.ExceptionInfo())
|
|
|
|
py.test.fail("spurious skip")
|
|
|
|
|
|
|
|
def test_importorskip_imports_last_module_part():
|
|
|
|
import os
|
|
|
|
ospath = py.test.importorskip("os.path")
|
|
|
|
assert os.path == ospath
|
|
|
|
|
2009-11-20 06:13:28 +08:00
|
|
|
|
|
|
|
def test_pytest_cmdline_main(testdir):
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
import sys
|
|
|
|
sys.path.insert(0, %r)
|
|
|
|
import py
|
|
|
|
def test_hello():
|
|
|
|
assert 1
|
|
|
|
if __name__ == '__main__':
|
|
|
|
py.test.cmdline.main([__file__])
|
|
|
|
""" % (str(py._dir.dirpath())))
|
|
|
|
import subprocess
|
|
|
|
subprocess.check_call([sys.executable, str(p)])
|