2013-11-21 19:31:22 +08:00
|
|
|
from __future__ import with_statement
|
|
|
|
|
2011-12-28 05:03:15 +08:00
|
|
|
import pytest, py, sys, os
|
2013-09-06 17:56:04 +08:00
|
|
|
from _pytest import runner, main
|
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
|
|
|
|
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
|
2013-10-12 21:39:22 +08:00
|
|
|
""") # noqa
|
2010-01-28 21:20:58 +08:00
|
|
|
ss = runner.SetupState()
|
2013-10-12 21:39:22 +08:00
|
|
|
pytest.raises(ValueError, lambda: ss.prepare(item))
|
|
|
|
pytest.raises(ValueError, lambda: ss.prepare(item))
|
|
|
|
|
2013-11-20 01:26:18 +08:00
|
|
|
def test_teardown_multiple_one_fails(self, testdir):
|
|
|
|
r = []
|
|
|
|
def fin1(): r.append('fin1')
|
|
|
|
def fin2(): raise Exception('oops')
|
|
|
|
def fin3(): r.append('fin3')
|
|
|
|
item = testdir.getitem("def test_func(): pass")
|
|
|
|
ss = runner.SetupState()
|
|
|
|
ss.addfinalizer(fin1, item)
|
|
|
|
ss.addfinalizer(fin2, item)
|
|
|
|
ss.addfinalizer(fin3, item)
|
|
|
|
with pytest.raises(Exception) as err:
|
|
|
|
ss._callfinalizers(item)
|
|
|
|
assert err.value.args == ('oops',)
|
|
|
|
assert r == ['fin3', 'fin1']
|
|
|
|
|
2013-11-21 09:15:24 +08:00
|
|
|
def test_teardown_multiple_fail(self, testdir):
|
|
|
|
# Ensure the first exception is the one which is re-raised.
|
|
|
|
# Ideally both would be reported however.
|
|
|
|
def fin1(): raise Exception('oops1')
|
|
|
|
def fin2(): raise Exception('oops2')
|
|
|
|
item = testdir.getitem("def test_func(): pass")
|
|
|
|
ss = runner.SetupState()
|
|
|
|
ss.addfinalizer(fin1, item)
|
|
|
|
ss.addfinalizer(fin2, item)
|
|
|
|
with pytest.raises(Exception) as err:
|
|
|
|
ss._callfinalizers(item)
|
|
|
|
assert err.value.args == ('oops2',)
|
|
|
|
|
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():
|
2012-09-15 21:20:49 +08:00
|
|
|
import sys
|
|
|
|
# on python2 exc_info is keept till a function exits
|
|
|
|
# so we would end up calling test functions while
|
|
|
|
# sys.exc_info would return the indexerror
|
|
|
|
# from guessing the lastitem
|
2013-09-06 04:22:14 +08:00
|
|
|
excinfo = sys.exc_info()
|
|
|
|
import traceback
|
|
|
|
assert excinfo[0] is None, \
|
|
|
|
traceback.format_exception(*excinfo)
|
2011-11-19 00:01:29 +08:00
|
|
|
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:
|
2014-01-18 19:31:33 +08:00
|
|
|
pytest.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
|
|
|
""")
|
2014-01-18 19:31:33 +08:00
|
|
|
except pytest.exit.Exception:
|
2009-05-23 01:56:05 +08:00
|
|
|
pass
|
2010-07-27 03:15:15 +08:00
|
|
|
else:
|
2014-01-18 19:31:33 +08:00
|
|
|
pytest.fail("did not raise")
|
2009-05-23 01:56:05 +08:00
|
|
|
|
|
|
|
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:
|
2014-01-18 19:31:33 +08:00
|
|
|
pytest.fail("did not raise")
|
2009-05-23 01:56:05 +08:00
|
|
|
|
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
|
2014-01-18 19:31:33 +08:00
|
|
|
xplugin = pytest.importorskip("xdist.plugin")
|
2010-01-13 23:00:33 +08:00
|
|
|
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
|
|
|
|
""")
|
2013-09-06 17:56:04 +08:00
|
|
|
rep = runner.collect_one_node(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
|
|
|
|
""")
|
2013-09-06 17:56:04 +08:00
|
|
|
rep = main.collect_one_node(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
|
|
|
|
2012-01-21 02:50:45 +08:00
|
|
|
|
|
|
|
reporttypes = [
|
|
|
|
runner.BaseReport,
|
|
|
|
runner.TestReport,
|
|
|
|
runner.TeardownErrorReport,
|
|
|
|
runner.CollectReport,
|
|
|
|
]
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('reporttype', reporttypes, ids=[x.__name__ for x in reporttypes])
|
|
|
|
def test_report_extra_parameters(reporttype):
|
|
|
|
args = py.std.inspect.getargspec(reporttype.__init__)[0][1:]
|
|
|
|
basekw = dict.fromkeys(args, [])
|
|
|
|
report = reporttype(newthing=1, **basekw)
|
|
|
|
assert report.newthing == 1
|
|
|
|
|
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
|
|
|
|
2013-01-27 09:06:19 +08:00
|
|
|
def test_outcomeexception_exceptionattributes():
|
|
|
|
outcome = runner.OutcomeException('test')
|
2013-02-04 23:07:51 +08:00
|
|
|
assert outcome.args[0] == outcome.msg
|
2013-01-27 09:06:19 +08:00
|
|
|
|
2010-04-28 14:42:56 +08:00
|
|
|
def test_pytest_exit():
|
|
|
|
try:
|
2014-01-18 19:31:33 +08:00
|
|
|
pytest.exit("hello")
|
|
|
|
except pytest.exit.Exception:
|
2010-04-28 14:42:56 +08:00
|
|
|
excinfo = py.code.ExceptionInfo()
|
|
|
|
assert excinfo.errisinstance(KeyboardInterrupt)
|
|
|
|
|
|
|
|
def test_pytest_fail():
|
|
|
|
try:
|
2014-01-18 19:31:33 +08:00
|
|
|
pytest.fail("hello")
|
|
|
|
except pytest.fail.Exception:
|
2010-04-28 14:42:56 +08:00
|
|
|
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():
|
2014-01-18 19:31:33 +08:00
|
|
|
importorskip = pytest.importorskip
|
2010-11-14 04:03:28 +08:00
|
|
|
def f():
|
|
|
|
importorskip("asdlkj")
|
2010-04-28 14:42:56 +08:00
|
|
|
try:
|
2013-10-12 21:39:22 +08:00
|
|
|
sys = importorskip("sys") # noqa
|
2010-04-28 14:42:56 +08:00
|
|
|
assert sys == py.std.sys
|
2014-01-18 19:31:33 +08:00
|
|
|
#path = pytest.importorskip("os.path")
|
2010-04-28 14:42:56 +08:00
|
|
|
#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"
|
2014-01-18 19:31:33 +08:00
|
|
|
pytest.raises(SyntaxError, "pytest.importorskip('x y z')")
|
|
|
|
pytest.raises(SyntaxError, "pytest.importorskip('x=y')")
|
2010-04-28 14:42:56 +08:00
|
|
|
mod = py.std.types.ModuleType("hello123")
|
|
|
|
mod.__version__ = "1.3"
|
2013-12-03 16:40:40 +08:00
|
|
|
sys.modules["hello123"] = mod
|
2010-11-18 05:12:16 +08:00
|
|
|
pytest.raises(pytest.skip.Exception, """
|
2014-01-18 19:31:33 +08:00
|
|
|
pytest.importorskip("hello123", minversion="1.3.1")
|
2010-04-28 14:42:56 +08:00
|
|
|
""")
|
2013-12-03 16:40:40 +08:00
|
|
|
mod2 = pytest.importorskip("hello123", minversion="1.3")
|
|
|
|
assert mod2 == mod
|
2010-11-18 05:12:16 +08:00
|
|
|
except pytest.skip.Exception:
|
2010-04-28 14:42:56 +08:00
|
|
|
print(py.code.ExceptionInfo())
|
2014-01-18 19:31:33 +08:00
|
|
|
pytest.fail("spurious skip")
|
2010-04-28 14:42:56 +08:00
|
|
|
|
|
|
|
def test_importorskip_imports_last_module_part():
|
2014-01-18 19:31:33 +08:00
|
|
|
ospath = pytest.importorskip("os.path")
|
2010-04-28 14:42:56 +08:00
|
|
|
assert os.path == ospath
|
|
|
|
|
|
|
|
|
|
|
|
def test_pytest_cmdline_main(testdir):
|
|
|
|
p = testdir.makepyfile("""
|
2014-01-18 19:31:33 +08:00
|
|
|
import pytest
|
2010-04-28 14:42:56 +08:00
|
|
|
def test_hello():
|
|
|
|
assert 1
|
|
|
|
if __name__ == '__main__':
|
2014-01-18 19:31:33 +08:00
|
|
|
pytest.cmdline.main([__file__])
|
2011-12-28 05:03:15 +08:00
|
|
|
""")
|
2010-04-28 14:42:56 +08:00
|
|
|
import subprocess
|
2011-12-28 23:49:13 +08:00
|
|
|
popen = subprocess.Popen([sys.executable, str(p)], stdout=subprocess.PIPE)
|
2013-10-12 21:39:22 +08:00
|
|
|
popen.communicate()
|
2010-04-28 14:42:56 +08:00
|
|
|
ret = popen.wait()
|
|
|
|
assert ret == 0
|
2010-07-07 18:41:15 +08:00
|
|
|
|
2012-06-24 22:42:31 +08:00
|
|
|
|
|
|
|
def test_unicode_in_longrepr(testdir):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
import py
|
|
|
|
def pytest_runtest_makereport(__multicall__):
|
|
|
|
rep = __multicall__.execute()
|
|
|
|
if rep.when == "call":
|
|
|
|
rep.longrepr = py.builtin._totext("\\xc3\\xa4", "utf8")
|
|
|
|
return rep
|
|
|
|
""")
|
|
|
|
testdir.makepyfile("""
|
|
|
|
def test_out():
|
|
|
|
assert 0
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest()
|
|
|
|
assert result.ret == 1
|
|
|
|
assert "UnicodeEncodeError" not in result.stderr.str()
|
|
|
|
|
2013-09-05 21:43:19 +08:00
|
|
|
|
|
|
|
def test_failure_in_setup(testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
def setup_module():
|
|
|
|
0/0
|
|
|
|
def test_func():
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest("--tb=line")
|
|
|
|
assert "def setup_module" not in result.stdout.str()
|
2014-08-15 06:23:04 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_makereport_getsource(testdir):
|
|
|
|
testdir.makepyfile("""
|
|
|
|
def test_foo():
|
|
|
|
if False: pass
|
|
|
|
else: assert False
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest()
|
|
|
|
assert 'INTERNALERROR' not in result.stdout.str()
|
2014-08-31 04:57:01 +08:00
|
|
|
result.stdout.fnmatch_lines(['*else: assert False*'])
|