2009-05-23 01:56:05 +08:00
|
|
|
import py
|
|
|
|
from py.__.test import runner
|
|
|
|
from py.__.code.excinfo 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()
|
|
|
|
assert not l
|
|
|
|
|
|
|
|
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()
|
|
|
|
assert l
|
|
|
|
ss._pop_and_teardown()
|
|
|
|
assert l
|
|
|
|
ss.teardown_all()
|
2009-05-19 01:06:16 +08:00
|
|
|
assert not l
|
|
|
|
|
2009-05-23 01:56:05 +08:00
|
|
|
|
|
|
|
class BaseFunctionalTests:
|
|
|
|
def test_funcattr(self, testdir):
|
|
|
|
rep = testdir.runitem("""
|
|
|
|
import py
|
|
|
|
@py.test.mark(xfail="needs refactoring")
|
|
|
|
def test_func():
|
|
|
|
raise Exit()
|
|
|
|
""")
|
|
|
|
assert rep.keywords['xfail'] == "needs refactoring"
|
|
|
|
|
|
|
|
def test_passfunction(self, testdir):
|
|
|
|
rep = testdir.runitem("""
|
2009-04-05 04:19:18 +08:00
|
|
|
def test_func():
|
|
|
|
pass
|
|
|
|
""")
|
2009-05-23 01:56:05 +08:00
|
|
|
assert rep.passed
|
|
|
|
assert not rep.failed
|
|
|
|
assert rep.shortrepr == "."
|
|
|
|
assert not hasattr(rep, 'longrepr')
|
|
|
|
|
|
|
|
def test_failfunction(self, testdir):
|
|
|
|
rep = testdir.runitem("""
|
|
|
|
def test_func():
|
|
|
|
assert 0
|
|
|
|
""")
|
|
|
|
assert not rep.passed
|
|
|
|
assert not rep.skipped
|
|
|
|
assert rep.failed
|
|
|
|
assert rep.when == "runtest"
|
|
|
|
assert isinstance(rep.longrepr, ReprExceptionInfo)
|
|
|
|
assert str(rep.shortrepr) == "F"
|
|
|
|
|
|
|
|
def test_skipfunction(self, testdir):
|
|
|
|
rep = testdir.runitem("""
|
|
|
|
import py
|
|
|
|
def test_func():
|
|
|
|
py.test.skip("hello")
|
|
|
|
""")
|
|
|
|
assert not rep.failed
|
|
|
|
assert not rep.passed
|
|
|
|
assert rep.skipped
|
|
|
|
#assert rep.skipped.when == "runtest"
|
|
|
|
#assert rep.skipped.when == "runtest"
|
|
|
|
#assert rep.skipped == "%sreason == "hello"
|
|
|
|
#assert rep.skipped.location.lineno == 3
|
|
|
|
#assert rep.skipped.location.path
|
|
|
|
#assert not rep.skipped.failurerepr
|
|
|
|
|
|
|
|
def test_skip_in_setup_function(self, testdir):
|
|
|
|
rep = testdir.runitem("""
|
|
|
|
import py
|
|
|
|
def setup_function(func):
|
|
|
|
py.test.skip("hello")
|
|
|
|
def test_func():
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
print rep
|
|
|
|
assert not rep.failed
|
|
|
|
assert not rep.passed
|
|
|
|
assert rep.skipped
|
|
|
|
#assert rep.skipped.reason == "hello"
|
|
|
|
#assert rep.skipped.location.lineno == 3
|
|
|
|
#assert rep.skipped.location.lineno == 3
|
|
|
|
|
|
|
|
def test_failure_in_setup_function(self, testdir):
|
|
|
|
rep = testdir.runitem("""
|
|
|
|
import py
|
|
|
|
def setup_function(func):
|
2009-04-05 04:19:18 +08:00
|
|
|
raise ValueError(42)
|
|
|
|
def test_func():
|
|
|
|
pass
|
|
|
|
""")
|
2009-05-23 01:56:05 +08:00
|
|
|
print rep
|
|
|
|
assert not rep.skipped
|
|
|
|
assert not rep.passed
|
|
|
|
assert rep.failed
|
|
|
|
assert rep.when == "setup"
|
2009-04-05 04:19:18 +08:00
|
|
|
|
2009-05-23 01:56:05 +08:00
|
|
|
def test_failure_in_teardown_function(self, testdir):
|
|
|
|
rep = testdir.runitem("""
|
|
|
|
import py
|
|
|
|
def teardown_function(func):
|
|
|
|
raise ValueError(42)
|
2009-04-05 04:19:18 +08:00
|
|
|
def test_func():
|
|
|
|
pass
|
|
|
|
""")
|
2009-05-23 01:56:05 +08:00
|
|
|
print rep
|
|
|
|
assert not rep.skipped
|
|
|
|
assert not rep.passed
|
|
|
|
assert rep.failed
|
|
|
|
assert rep.when == "teardown"
|
|
|
|
assert rep.longrepr.reprcrash.lineno == 3
|
|
|
|
assert rep.longrepr.reprtraceback.reprentries
|
|
|
|
|
|
|
|
def test_custom_failure_repr(self, testdir):
|
|
|
|
testdir.makepyfile(conftest="""
|
|
|
|
import py
|
|
|
|
class Function(py.test.collect.Function):
|
|
|
|
def repr_failure(self, excinfo, outerr):
|
|
|
|
return "hello"
|
|
|
|
""")
|
|
|
|
rep = testdir.runitem("""
|
|
|
|
import py
|
|
|
|
def test_func():
|
|
|
|
assert 0
|
|
|
|
""")
|
|
|
|
assert not rep.skipped
|
|
|
|
assert not rep.passed
|
2009-04-05 04:19:18 +08:00
|
|
|
assert rep.failed
|
2009-05-23 01:56:05 +08:00
|
|
|
#assert rep.outcome.when == "runtest"
|
|
|
|
#assert rep.failed.where.lineno == 3
|
|
|
|
#assert rep.failed.where.path.basename == "test_func.py"
|
|
|
|
#assert rep.failed.failurerepr == "hello"
|
2009-04-05 04:19:18 +08:00
|
|
|
|
2009-05-23 01:56:05 +08:00
|
|
|
def test_failure_in_setup_function_ignores_custom_failure_repr(self, testdir):
|
|
|
|
testdir.makepyfile(conftest="""
|
|
|
|
import py
|
|
|
|
class Function(py.test.collect.Function):
|
|
|
|
def repr_failure(self, excinfo):
|
|
|
|
assert 0
|
|
|
|
""")
|
|
|
|
rep = testdir.runitem("""
|
2009-04-05 04:19:18 +08:00
|
|
|
import py
|
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-05-23 01:56:05 +08:00
|
|
|
print rep
|
|
|
|
assert not rep.skipped
|
|
|
|
assert not rep.passed
|
|
|
|
assert rep.failed
|
|
|
|
#assert rep.outcome.when == "setup"
|
|
|
|
#assert rep.outcome.where.lineno == 3
|
|
|
|
#assert rep.outcome.where.path.basename == "test_func.py"
|
|
|
|
#assert instanace(rep.failed.failurerepr, PythonFailureRepr)
|
2009-04-05 04:19:18 +08:00
|
|
|
|
2009-05-23 01:56:05 +08:00
|
|
|
def test_capture_in_func(self, testdir):
|
|
|
|
rep = testdir.runitem("""
|
|
|
|
import py
|
|
|
|
def setup_function(func):
|
|
|
|
print >>py.std.sys.stderr, "in setup"
|
|
|
|
def test_func():
|
|
|
|
print "in function"
|
|
|
|
assert 0
|
|
|
|
def teardown_func(func):
|
|
|
|
print "in teardown"
|
|
|
|
""")
|
|
|
|
assert rep.failed
|
|
|
|
# out, err = rep.failed.outerr
|
|
|
|
# assert out == ['in function\nin teardown\n']
|
|
|
|
# assert err == ['in setup\n']
|
|
|
|
|
|
|
|
def test_systemexit_does_not_bail_out(self, testdir):
|
|
|
|
try:
|
|
|
|
rep = testdir.runitem("""
|
|
|
|
def test_func():
|
|
|
|
raise SystemExit(42)
|
|
|
|
""")
|
|
|
|
except SystemExit:
|
|
|
|
py.test.fail("runner did not catch SystemExit")
|
|
|
|
assert rep.failed
|
|
|
|
assert rep.when == "runtest"
|
|
|
|
|
|
|
|
def test_exit_propagates(self, testdir):
|
|
|
|
from py.__.test.outcome import Exit
|
|
|
|
try:
|
|
|
|
testdir.runitem("""
|
|
|
|
from py.__.test.outcome import Exit
|
|
|
|
def test_func():
|
|
|
|
raise Exit()
|
|
|
|
""")
|
|
|
|
except Exit:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
py.test.fail("did not raise")
|
|
|
|
|
|
|
|
|
|
|
|
class TestExecutionNonForked(BaseFunctionalTests):
|
|
|
|
def getrunner(self):
|
|
|
|
return runner.basic_run_report
|
|
|
|
|
|
|
|
def test_keyboardinterrupt_propagates(self, testdir):
|
|
|
|
from py.__.test.outcome import Exit
|
|
|
|
try:
|
|
|
|
testdir.runitem("""
|
|
|
|
def test_func():
|
|
|
|
raise KeyboardInterrupt("fake")
|
|
|
|
""")
|
|
|
|
except KeyboardInterrupt, e:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
py.test.fail("did not raise")
|
|
|
|
|
|
|
|
class TestExecutionForked(BaseFunctionalTests):
|
|
|
|
def getrunner(self):
|
|
|
|
if not hasattr(py.std.os, 'fork'):
|
|
|
|
py.test.skip("no os.fork available")
|
|
|
|
return runner.forked_run_report
|
|
|
|
|
|
|
|
def test_suicide(self, testdir):
|
|
|
|
rep = testdir.runitem("""
|
|
|
|
def test_func():
|
|
|
|
import os
|
|
|
|
os.kill(os.getpid(), 15)
|
|
|
|
""")
|
|
|
|
assert rep.failed
|
|
|
|
assert rep.when == "???"
|
|
|
|
|
|
|
|
class TestCollectionReports:
|
|
|
|
def test_collect_result(self, testdir):
|
|
|
|
col = testdir.getmodulecol("""
|
|
|
|
def test_func1():
|
|
|
|
pass
|
|
|
|
class TestClass:
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
rep = runner.basic_collect_report(col)
|
|
|
|
assert not rep.failed
|
|
|
|
assert not rep.skipped
|
2009-04-05 04:19:18 +08:00
|
|
|
assert rep.passed
|
2009-05-23 01:56:05 +08:00
|
|
|
res = rep.result
|
|
|
|
assert len(res) == 2
|
|
|
|
assert res[0].name == "test_func1"
|
|
|
|
assert res[1].name == "TestClass"
|
|
|
|
|
|
|
|
def test_skip_at_module_scope(self, testdir):
|
|
|
|
col = testdir.getmodulecol("""
|
|
|
|
import py
|
|
|
|
py.test.skip("hello")
|
|
|
|
def test_func():
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
rep = runner.basic_collect_report(col)
|
|
|
|
assert not rep.failed
|
|
|
|
assert not rep.passed
|
|
|
|
assert rep.skipped
|
2009-04-05 04:19:18 +08:00
|
|
|
|
2009-05-19 01:06:16 +08:00
|
|
|
|