2011-12-28 05:03:15 +08:00
|
|
|
import pytest, py, sys, os
|
2010-11-13 18:10:45 +08:00
|
|
|
from _pytest import runner
|
2010-01-14 00:15:54 +08:00
|
|
|
from py._code.code import ReprExceptionInfo
|
2009-04-05 04:19:18 +08:00
|
|
|
|
|
|
|
class TestSetupState:
|
2009-05-19 01:06:16 +08:00
|
|
|
def test_setup(self, testdir):
|
2009-05-23 01:56:05 +08:00
|
|
|
ss = runner.SetupState()
|
2009-05-19 01:06:16 +08:00
|
|
|
item = testdir.getitem("def test_func(): pass")
|
|
|
|
l = [1]
|
2009-05-21 20:37:30 +08:00
|
|
|
ss.prepare(item)
|
2009-05-19 01:06:16 +08:00
|
|
|
ss.addfinalizer(l.pop, colitem=item)
|
2009-05-21 20:37:30 +08:00
|
|
|
assert l
|
|
|
|
ss._pop_and_teardown()
|
2010-07-27 03:15:15 +08:00
|
|
|
assert not l
|
2009-05-21 20:37:30 +08:00
|
|
|
|
|
|
|
def test_setup_scope_None(self, testdir):
|
|
|
|
item = testdir.getitem("def test_func(): pass")
|
2009-05-23 01:56:05 +08:00
|
|
|
ss = runner.SetupState()
|
2009-05-21 20:37:30 +08:00
|
|
|
l = [1]
|
|
|
|
ss.prepare(item)
|
|
|
|
ss.addfinalizer(l.pop, colitem=None)
|
|
|
|
assert l
|
|
|
|
ss._pop_and_teardown()
|
2010-07-27 03:15:15 +08:00
|
|
|
assert l
|
2009-05-21 20:37:30 +08:00
|
|
|
ss._pop_and_teardown()
|
2010-07-27 03:15:15 +08:00
|
|
|
assert l
|
2009-05-21 20:37:30 +08:00
|
|
|
ss.teardown_all()
|
2010-07-27 03:15:15 +08:00
|
|
|
assert not l
|
2009-05-19 01:06:16 +08:00
|
|
|
|
2009-10-15 22:18:57 +08:00
|
|
|
def test_teardown_exact_stack_empty(self, testdir):
|
|
|
|
item = testdir.getitem("def test_func(): pass")
|
|
|
|
ss = runner.SetupState()
|
2011-12-03 05:00:19 +08:00
|
|
|
ss.teardown_exact(item, None)
|
|
|
|
ss.teardown_exact(item, None)
|
|
|
|
ss.teardown_exact(item, None)
|
2009-05-23 01:56:05 +08:00
|
|
|
|
2010-01-28 21:20:58 +08:00
|
|
|
def test_setup_fails_and_failure_is_cached(self, testdir):
|
|
|
|
item = testdir.getitem("""
|
|
|
|
def setup_module(mod):
|
|
|
|
raise ValueError(42)
|
|
|
|
def test_func(): pass
|
|
|
|
""")
|
|
|
|
ss = runner.SetupState()
|
2010-11-18 05:12:16 +08:00
|
|
|
pytest.raises(ValueError, "ss.prepare(item)")
|
|
|
|
pytest.raises(ValueError, "ss.prepare(item)")
|
2010-01-28 21:20:58 +08:00
|
|
|
|
2009-05-23 01:56:05 +08:00
|
|
|
class BaseFunctionalTests:
|
|
|
|
def test_passfunction(self, testdir):
|
2009-06-09 22:08:34 +08:00
|
|
|
reports = testdir.runitem("""
|
2009-04-05 04:19:18 +08:00
|
|
|
def test_func():
|
|
|
|
pass
|
|
|
|
""")
|
2009-06-09 22:08:34 +08:00
|
|
|
rep = reports[1]
|
2010-07-27 03:15:15 +08:00
|
|
|
assert rep.passed
|
2009-05-23 01:56:05 +08:00
|
|
|
assert not rep.failed
|
2010-09-26 22:23:44 +08:00
|
|
|
assert rep.outcome == "passed"
|
|
|
|
assert not rep.longrepr
|
2010-07-27 03:15:15 +08:00
|
|
|
|
2009-05-23 01:56:05 +08:00
|
|
|
def test_failfunction(self, testdir):
|
2009-06-09 22:08:34 +08:00
|
|
|
reports = testdir.runitem("""
|
2009-05-23 01:56:05 +08:00
|
|
|
def test_func():
|
|
|
|
assert 0
|
|
|
|
""")
|
2009-06-09 22:08:34 +08:00
|
|
|
rep = reports[1]
|
2010-07-27 03:15:15 +08:00
|
|
|
assert not rep.passed
|
|
|
|
assert not rep.skipped
|
|
|
|
assert rep.failed
|
2009-06-09 00:31:10 +08:00
|
|
|
assert rep.when == "call"
|
2010-09-26 22:23:44 +08:00
|
|
|
assert rep.outcome == "failed"
|
|
|
|
#assert isinstance(rep.longrepr, ReprExceptionInfo)
|
2010-07-07 18:41:15 +08:00
|
|
|
|
2009-05-23 01:56:05 +08:00
|
|
|
def test_skipfunction(self, testdir):
|
2009-06-09 22:08:34 +08:00
|
|
|
reports = testdir.runitem("""
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
2009-05-23 01:56:05 +08:00
|
|
|
def test_func():
|
2010-11-18 05:12:16 +08:00
|
|
|
pytest.skip("hello")
|
2009-05-23 01:56:05 +08:00
|
|
|
""")
|
2009-06-09 22:08:34 +08:00
|
|
|
rep = reports[1]
|
2010-07-27 03:15:15 +08:00
|
|
|
assert not rep.failed
|
|
|
|
assert not rep.passed
|
|
|
|
assert rep.skipped
|
2010-09-26 22:23:44 +08:00
|
|
|
assert rep.outcome == "skipped"
|
2009-06-09 00:31:10 +08:00
|
|
|
#assert rep.skipped.when == "call"
|
|
|
|
#assert rep.skipped.when == "call"
|
2009-05-23 01:56:05 +08:00
|
|
|
#assert rep.skipped == "%sreason == "hello"
|
|
|
|
#assert rep.skipped.location.lineno == 3
|
|
|
|
#assert rep.skipped.location.path
|
2010-07-27 03:15:15 +08:00
|
|
|
#assert not rep.skipped.failurerepr
|
2009-05-23 01:56:05 +08:00
|
|
|
|
|
|
|
def test_skip_in_setup_function(self, testdir):
|
2009-06-09 22:08:34 +08:00
|
|
|
reports = testdir.runitem("""
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
2009-05-23 01:56:05 +08:00
|
|
|
def setup_function(func):
|
2010-11-18 05:12:16 +08:00
|
|
|
pytest.skip("hello")
|
2009-05-23 01:56:05 +08:00
|
|
|
def test_func():
|
|
|
|
pass
|
|
|
|
""")
|
2009-08-30 02:04:48 +08:00
|
|
|
print(reports)
|
2009-06-09 22:08:34 +08:00
|
|
|
rep = reports[0]
|
2010-07-27 03:15:15 +08:00
|
|
|
assert not rep.failed
|
|
|
|
assert not rep.passed
|
|
|
|
assert rep.skipped
|
2009-05-23 01:56:05 +08:00
|
|
|
#assert rep.skipped.reason == "hello"
|
|
|
|
#assert rep.skipped.location.lineno == 3
|
|
|
|
#assert rep.skipped.location.lineno == 3
|
2009-07-08 22:41:30 +08:00
|
|
|
assert len(reports) == 2
|
2010-07-27 03:15:15 +08:00
|
|
|
assert reports[1].passed # teardown
|
2009-05-23 01:56:05 +08:00
|
|
|
|
|
|
|
def test_failure_in_setup_function(self, testdir):
|
2009-06-09 22:08:34 +08:00
|
|
|
reports = testdir.runitem("""
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
2009-05-23 01:56:05 +08:00
|
|
|
def setup_function(func):
|
2009-04-05 04:19:18 +08:00
|
|
|
raise ValueError(42)
|
|
|
|
def test_func():
|
|
|
|
pass
|
|
|
|
""")
|
2009-06-09 22:08:34 +08:00
|
|
|
rep = reports[0]
|
2010-07-27 03:15:15 +08:00
|
|
|
assert not rep.skipped
|
|
|
|
assert not rep.passed
|
|
|
|
assert rep.failed
|
2009-05-23 01:56:05 +08:00
|
|
|
assert rep.when == "setup"
|
2009-07-08 22:41:30 +08:00
|
|
|
assert len(reports) == 2
|
2009-04-05 04:19:18 +08:00
|
|
|
|
2009-05-23 01:56:05 +08:00
|
|
|
def test_failure_in_teardown_function(self, testdir):
|
2009-06-09 22:08:34 +08:00
|
|
|
reports = testdir.runitem("""
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
2009-05-23 01:56:05 +08:00
|
|
|
def teardown_function(func):
|
|
|
|
raise ValueError(42)
|
2009-04-05 04:19:18 +08:00
|
|
|
def test_func():
|
|
|
|
pass
|
|
|
|
""")
|
2009-08-30 02:04:48 +08:00
|
|
|
print(reports)
|
2009-06-09 22:08:34 +08:00
|
|
|
assert len(reports) == 3
|
|
|
|
rep = reports[2]
|
2010-07-27 03:15:15 +08:00
|
|
|
assert not rep.skipped
|
|
|
|
assert not rep.passed
|
|
|
|
assert rep.failed
|
|
|
|
assert rep.when == "teardown"
|
2010-09-26 22:23:44 +08:00
|
|
|
#assert rep.longrepr.reprcrash.lineno == 3
|
|
|
|
#assert rep.longrepr.reprtraceback.reprentries
|
2009-05-23 01:56:05 +08:00
|
|
|
|
|
|
|
def test_custom_failure_repr(self, testdir):
|
|
|
|
testdir.makepyfile(conftest="""
|
2010-11-13 16:05:11 +08:00
|
|
|
import pytest
|
|
|
|
class Function(pytest.Function):
|
2009-07-26 00:09:01 +08:00
|
|
|
def repr_failure(self, excinfo):
|
2010-07-27 03:15:15 +08:00
|
|
|
return "hello"
|
2009-05-23 01:56:05 +08:00
|
|
|
""")
|
2009-06-09 22:08:34 +08:00
|
|
|
reports = testdir.runitem("""
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
2009-05-23 01:56:05 +08:00
|
|
|
def test_func():
|
|
|
|
assert 0
|
|
|
|
""")
|
2009-06-09 22:08:34 +08:00
|
|
|
rep = reports[1]
|
2010-07-27 03:15:15 +08:00
|
|
|
assert not rep.skipped
|
|
|
|
assert not rep.passed
|
|
|
|
assert rep.failed
|
2009-06-09 00:31:10 +08:00
|
|
|
#assert rep.outcome.when == "call"
|
2009-05-23 01:56:05 +08:00
|
|
|
#assert rep.failed.where.lineno == 3
|
2010-07-27 03:15:15 +08:00
|
|
|
#assert rep.failed.where.path.basename == "test_func.py"
|
2009-05-23 01:56:05 +08:00
|
|
|
#assert rep.failed.failurerepr == "hello"
|
2009-04-05 04:19:18 +08:00
|
|
|
|
2011-11-19 00:01:29 +08:00
|
|
|
def test_teardown_final_returncode(self, testdir):
|
|
|
|
rec = testdir.inline_runsource("""
|
|
|
|
def test_func():
|
|
|
|
pass
|
|
|
|
def teardown_function(func):
|
|
|
|
raise ValueError(42)
|
|
|
|
""")
|
|
|
|
assert rec.ret == 1
|
|
|
|
|
|
|
|
def test_exact_teardown_issue90(self, testdir):
|
|
|
|
rec = testdir.inline_runsource("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
class TestClass:
|
|
|
|
def test_method(self):
|
|
|
|
pass
|
|
|
|
def teardown_class(cls):
|
|
|
|
raise Exception()
|
|
|
|
|
|
|
|
def test_func():
|
|
|
|
pass
|
|
|
|
def teardown_function(func):
|
|
|
|
raise ValueError(42)
|
|
|
|
""")
|
|
|
|
reps = rec.getreports("pytest_runtest_logreport")
|
|
|
|
print (reps)
|
|
|
|
for i in range(2):
|
|
|
|
assert reps[i].nodeid.endswith("test_method")
|
|
|
|
assert reps[i].passed
|
|
|
|
assert reps[2].when == "teardown"
|
|
|
|
assert reps[2].failed
|
|
|
|
assert len(reps) == 6
|
|
|
|
for i in range(3,5):
|
|
|
|
assert reps[i].nodeid.endswith("test_func")
|
|
|
|
assert reps[i].passed
|
|
|
|
assert reps[5].when == "teardown"
|
|
|
|
assert reps[5].nodeid.endswith("test_func")
|
|
|
|
assert reps[5].failed
|
|
|
|
|
2009-07-26 00:09:01 +08:00
|
|
|
def test_failure_in_setup_function_ignores_custom_repr(self, testdir):
|
2009-05-23 01:56:05 +08:00
|
|
|
testdir.makepyfile(conftest="""
|
2010-11-13 16:05:11 +08:00
|
|
|
import pytest
|
|
|
|
class Function(pytest.Function):
|
2009-05-23 01:56:05 +08:00
|
|
|
def repr_failure(self, excinfo):
|
|
|
|
assert 0
|
|
|
|
""")
|
2009-06-09 22:08:34 +08:00
|
|
|
reports = testdir.runitem("""
|
2009-05-23 01:56:05 +08:00
|
|
|
def setup_function(func):
|
|
|
|
raise ValueError(42)
|
2009-04-05 04:19:18 +08:00
|
|
|
def test_func():
|
|
|
|
pass
|
|
|
|
""")
|
2009-07-08 22:41:30 +08:00
|
|
|
assert len(reports) == 2
|
2009-06-09 22:08:34 +08:00
|
|
|
rep = reports[0]
|
2009-08-30 02:04:48 +08:00
|
|
|
print(rep)
|
2010-07-27 03:15:15 +08:00
|
|
|
assert not rep.skipped
|
|
|
|
assert not rep.passed
|
|
|
|
assert rep.failed
|
2009-05-23 01:56:05 +08:00
|
|
|
#assert rep.outcome.when == "setup"
|
|
|
|
#assert rep.outcome.where.lineno == 3
|
2010-07-27 03:15:15 +08:00
|
|
|
#assert rep.outcome.where.path.basename == "test_func.py"
|
2009-05-23 01:56:05 +08:00
|
|
|
#assert instanace(rep.failed.failurerepr, PythonFailureRepr)
|
2009-04-05 04:19:18 +08:00
|
|
|
|
2009-05-23 01:56:05 +08:00
|
|
|
def test_systemexit_does_not_bail_out(self, testdir):
|
|
|
|
try:
|
2009-06-09 22:08:34 +08:00
|
|
|
reports = testdir.runitem("""
|
2009-05-23 01:56:05 +08:00
|
|
|
def test_func():
|
|
|
|
raise SystemExit(42)
|
|
|
|
""")
|
|
|
|
except SystemExit:
|
|
|
|
py.test.fail("runner did not catch SystemExit")
|
2009-06-09 22:08:34 +08:00
|
|
|
rep = reports[1]
|
2009-05-23 01:56:05 +08:00
|
|
|
assert rep.failed
|
2009-06-09 00:31:10 +08:00
|
|
|
assert rep.when == "call"
|
2009-05-23 01:56:05 +08:00
|
|
|
|
|
|
|
def test_exit_propagates(self, testdir):
|
|
|
|
try:
|
|
|
|
testdir.runitem("""
|
2010-11-13 16:05:11 +08:00
|
|
|
import pytest
|
2009-05-23 01:56:05 +08:00
|
|
|
def test_func():
|
2010-11-13 16:05:11 +08:00
|
|
|
raise pytest.exit.Exception()
|
2009-05-23 01:56:05 +08:00
|
|
|
""")
|
2010-04-28 14:42:56 +08:00
|
|
|
except py.test.exit.Exception:
|
2009-05-23 01:56:05 +08:00
|
|
|
pass
|
2010-07-27 03:15:15 +08:00
|
|
|
else:
|
2009-05-23 01:56:05 +08:00
|
|
|
py.test.fail("did not raise")
|
|
|
|
|
|
|
|
class TestExecutionNonForked(BaseFunctionalTests):
|
|
|
|
def getrunner(self):
|
2009-06-09 22:08:34 +08:00
|
|
|
def f(item):
|
|
|
|
return runner.runtestprotocol(item, log=False)
|
|
|
|
return f
|
2009-05-23 01:56:05 +08:00
|
|
|
|
|
|
|
def test_keyboardinterrupt_propagates(self, testdir):
|
|
|
|
try:
|
|
|
|
testdir.runitem("""
|
|
|
|
def test_func():
|
|
|
|
raise KeyboardInterrupt("fake")
|
|
|
|
""")
|
2009-08-30 02:04:48 +08:00
|
|
|
except KeyboardInterrupt:
|
2009-05-23 01:56:05 +08:00
|
|
|
pass
|
2010-07-27 03:15:15 +08:00
|
|
|
else:
|
2009-05-23 01:56:05 +08:00
|
|
|
py.test.fail("did not raise")
|
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
class TestExecutionForked(BaseFunctionalTests):
|
2010-11-18 05:12:16 +08:00
|
|
|
pytestmark = pytest.mark.skipif("not hasattr(os, 'fork')")
|
2009-10-23 00:37:24 +08:00
|
|
|
|
2009-05-23 01:56:05 +08:00
|
|
|
def getrunner(self):
|
2010-01-13 23:00:33 +08:00
|
|
|
# XXX re-arrange this test to live in pytest-xdist
|
|
|
|
xplugin = py.test.importorskip("xdist.plugin")
|
|
|
|
return xplugin.forked_run_report
|
2009-05-23 01:56:05 +08:00
|
|
|
|
|
|
|
def test_suicide(self, testdir):
|
2009-06-09 22:08:34 +08:00
|
|
|
reports = testdir.runitem("""
|
2009-05-23 01:56:05 +08:00
|
|
|
def test_func():
|
|
|
|
import os
|
|
|
|
os.kill(os.getpid(), 15)
|
|
|
|
""")
|
2009-06-09 22:08:34 +08:00
|
|
|
rep = reports[0]
|
2009-05-23 01:56:05 +08:00
|
|
|
assert rep.failed
|
|
|
|
assert rep.when == "???"
|
|
|
|
|
2010-11-07 17:19:58 +08:00
|
|
|
class TestSessionReports:
|
2009-05-23 01:56:05 +08:00
|
|
|
def test_collect_result(self, testdir):
|
|
|
|
col = testdir.getmodulecol("""
|
|
|
|
def test_func1():
|
|
|
|
pass
|
|
|
|
class TestClass:
|
|
|
|
pass
|
|
|
|
""")
|
2009-06-09 00:31:10 +08:00
|
|
|
rep = runner.pytest_make_collect_report(col)
|
2009-05-23 01:56:05 +08:00
|
|
|
assert not rep.failed
|
|
|
|
assert not rep.skipped
|
2010-07-27 03:15:15 +08:00
|
|
|
assert rep.passed
|
2010-09-26 22:23:44 +08:00
|
|
|
locinfo = rep.location
|
2010-11-06 16:58:04 +08:00
|
|
|
assert locinfo[0] == col.fspath.basename
|
2010-09-26 22:23:44 +08:00
|
|
|
assert not locinfo[1]
|
2010-11-06 16:58:04 +08:00
|
|
|
assert locinfo[2] == col.fspath.basename
|
2010-07-27 03:15:15 +08:00
|
|
|
res = rep.result
|
2009-05-23 01:56:05 +08:00
|
|
|
assert len(res) == 2
|
2010-07-27 03:15:15 +08:00
|
|
|
assert res[0].name == "test_func1"
|
|
|
|
assert res[1].name == "TestClass"
|
2009-05-23 01:56:05 +08:00
|
|
|
|
|
|
|
def test_skip_at_module_scope(self, testdir):
|
|
|
|
col = testdir.getmodulecol("""
|
2010-11-13 16:05:11 +08:00
|
|
|
import pytest
|
|
|
|
pytest.skip("hello")
|
2009-05-23 01:56:05 +08:00
|
|
|
def test_func():
|
|
|
|
pass
|
|
|
|
""")
|
2009-06-09 00:31:10 +08:00
|
|
|
rep = runner.pytest_make_collect_report(col)
|
2010-07-27 03:15:15 +08:00
|
|
|
assert not rep.failed
|
|
|
|
assert not rep.passed
|
|
|
|
assert rep.skipped
|
2009-06-09 22:08:34 +08:00
|
|
|
|
2009-08-06 20:34:19 +08:00
|
|
|
def test_callinfo():
|
|
|
|
ci = runner.CallInfo(lambda: 0, '123')
|
|
|
|
assert ci.when == "123"
|
|
|
|
assert ci.result == 0
|
2010-07-27 03:15:15 +08:00
|
|
|
assert "result" in repr(ci)
|
2009-08-06 20:34:19 +08:00
|
|
|
ci = runner.CallInfo(lambda: 0/0, '123')
|
|
|
|
assert ci.when == "123"
|
|
|
|
assert not hasattr(ci, 'result')
|
2010-07-27 03:15:15 +08:00
|
|
|
assert ci.excinfo
|
2009-08-06 20:34:19 +08:00
|
|
|
assert "exc" in repr(ci)
|
2009-12-30 17:42:01 +08:00
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
# design question: do we want general hooks in python files?
|
2010-09-26 22:23:44 +08:00
|
|
|
# then something like the following functional tests makes sense
|
2010-11-18 05:12:16 +08:00
|
|
|
@pytest.mark.xfail
|
2009-12-30 17:42:01 +08:00
|
|
|
def test_runtest_in_module_ordering(testdir):
|
|
|
|
p1 = testdir.makepyfile("""
|
|
|
|
def pytest_runtest_setup(item): # runs after class-level!
|
|
|
|
item.function.mylist.append("module")
|
|
|
|
class TestClass:
|
|
|
|
def pytest_runtest_setup(self, item):
|
|
|
|
assert not hasattr(item.function, 'mylist')
|
|
|
|
item.function.mylist = ['class']
|
|
|
|
def pytest_funcarg__mylist(self, request):
|
|
|
|
return request.function.mylist
|
|
|
|
def pytest_runtest_call(self, item, __multicall__):
|
|
|
|
try:
|
|
|
|
__multicall__.execute()
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
def test_hello1(self, mylist):
|
|
|
|
assert mylist == ['class', 'module'], mylist
|
|
|
|
raise ValueError()
|
|
|
|
def test_hello2(self, mylist):
|
|
|
|
assert mylist == ['class', 'module'], mylist
|
|
|
|
def pytest_runtest_teardown(item):
|
2010-07-27 03:15:15 +08:00
|
|
|
del item.function.mylist
|
2009-12-30 17:42:01 +08:00
|
|
|
""")
|
|
|
|
result = testdir.runpytest(p1)
|
2010-04-29 06:12:38 +08:00
|
|
|
result.stdout.fnmatch_lines([
|
2009-12-30 17:42:01 +08:00
|
|
|
"*2 passed*"
|
|
|
|
])
|
2010-04-28 14:42:56 +08:00
|
|
|
|
2010-06-09 16:50:00 +08:00
|
|
|
|
2010-04-28 14:42:56 +08:00
|
|
|
def test_pytest_exit():
|
|
|
|
try:
|
|
|
|
py.test.exit("hello")
|
|
|
|
except py.test.exit.Exception:
|
|
|
|
excinfo = py.code.ExceptionInfo()
|
|
|
|
assert excinfo.errisinstance(KeyboardInterrupt)
|
|
|
|
|
|
|
|
def test_pytest_fail():
|
|
|
|
try:
|
|
|
|
py.test.fail("hello")
|
|
|
|
except py.test.fail.Exception:
|
|
|
|
excinfo = py.code.ExceptionInfo()
|
|
|
|
s = excinfo.exconly(tryshort=True)
|
|
|
|
assert s.startswith("Failed")
|
|
|
|
|
2010-11-23 22:42:23 +08:00
|
|
|
def test_pytest_fail_notrace(testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
def test_hello():
|
|
|
|
pytest.fail("hello", pytrace=False)
|
2010-11-24 23:43:55 +08:00
|
|
|
def teardown_function(function):
|
|
|
|
pytest.fail("world", pytrace=False)
|
2010-11-23 22:42:23 +08:00
|
|
|
""")
|
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stdout.fnmatch_lines([
|
2010-11-24 23:43:55 +08:00
|
|
|
"world",
|
|
|
|
"hello",
|
2010-11-23 22:42:23 +08:00
|
|
|
])
|
2010-11-24 23:43:55 +08:00
|
|
|
assert 'def teardown_function' not in result.stdout.str()
|
2010-11-23 22:42:23 +08:00
|
|
|
|
2010-04-28 14:42:56 +08:00
|
|
|
def test_exception_printing_skip():
|
|
|
|
try:
|
2010-11-18 05:12:16 +08:00
|
|
|
pytest.skip("hello")
|
|
|
|
except pytest.skip.Exception:
|
2010-04-28 14:42:56 +08:00
|
|
|
excinfo = py.code.ExceptionInfo()
|
|
|
|
s = excinfo.exconly(tryshort=True)
|
|
|
|
assert s.startswith("Skipped")
|
|
|
|
|
|
|
|
def test_importorskip():
|
|
|
|
importorskip = py.test.importorskip
|
2010-11-14 04:03:28 +08:00
|
|
|
def f():
|
|
|
|
importorskip("asdlkj")
|
2010-04-28 14:42:56 +08:00
|
|
|
try:
|
|
|
|
sys = importorskip("sys")
|
|
|
|
assert sys == py.std.sys
|
|
|
|
#path = py.test.importorskip("os.path")
|
|
|
|
#assert path == py.std.os.path
|
2010-11-18 05:12:16 +08:00
|
|
|
excinfo = pytest.raises(pytest.skip.Exception, f)
|
2010-11-14 04:03:28 +08:00
|
|
|
path = py.path.local(excinfo.getrepr().reprcrash.path)
|
|
|
|
# check that importorskip reports the actual call
|
|
|
|
# in this test the test_runner.py file
|
|
|
|
assert path.purebasename == "test_runner"
|
2010-11-18 05:12:16 +08:00
|
|
|
pytest.raises(SyntaxError, "py.test.importorskip('x y z')")
|
|
|
|
pytest.raises(SyntaxError, "py.test.importorskip('x=y')")
|
2010-04-28 14:42:56 +08:00
|
|
|
path = importorskip("py", minversion=".".join(py.__version__))
|
|
|
|
mod = py.std.types.ModuleType("hello123")
|
|
|
|
mod.__version__ = "1.3"
|
2010-11-18 05:12:16 +08:00
|
|
|
pytest.raises(pytest.skip.Exception, """
|
2010-04-28 14:42:56 +08:00
|
|
|
py.test.importorskip("hello123", minversion="5.0")
|
|
|
|
""")
|
2010-11-18 05:12:16 +08:00
|
|
|
except pytest.skip.Exception:
|
2010-04-28 14:42:56 +08:00
|
|
|
print(py.code.ExceptionInfo())
|
|
|
|
py.test.fail("spurious skip")
|
|
|
|
|
|
|
|
def test_importorskip_imports_last_module_part():
|
|
|
|
ospath = py.test.importorskip("os.path")
|
|
|
|
assert os.path == ospath
|
|
|
|
|
|
|
|
|
|
|
|
def test_pytest_cmdline_main(testdir):
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
import py
|
|
|
|
def test_hello():
|
|
|
|
assert 1
|
|
|
|
if __name__ == '__main__':
|
|
|
|
py.test.cmdline.main([__file__])
|
2011-12-28 05:03:15 +08:00
|
|
|
""")
|
2010-04-28 14:42:56 +08:00
|
|
|
import subprocess
|
2011-12-28 05:03:15 +08:00
|
|
|
popen = subprocess.Popen([sys.executable, str(p)],
|
|
|
|
stdout=subprocess.PIPE, env={})
|
2010-04-28 14:42:56 +08:00
|
|
|
s = popen.stdout.read()
|
|
|
|
ret = popen.wait()
|
|
|
|
assert ret == 0
|
2010-07-07 18:41:15 +08:00
|
|
|
|