2016-03-06 03:09:01 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
2013-11-21 19:31:22 +08:00
|
|
|
from __future__ import with_statement
|
|
|
|
|
2015-11-27 22:43:01 +08:00
|
|
|
import _pytest._code
|
|
|
|
import os
|
|
|
|
import py
|
|
|
|
import pytest
|
|
|
|
import sys
|
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 = []
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2013-11-20 01:26:18 +08:00
|
|
|
def fin1(): r.append('fin1')
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2013-11-20 01:26:18 +08:00
|
|
|
def fin2(): raise Exception('oops')
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2013-11-20 01:26:18 +08:00
|
|
|
def fin3(): r.append('fin3')
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2013-11-20 01:26:18 +08:00
|
|
|
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')
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2013-11-21 09:15:24 +08:00
|
|
|
def fin2(): raise Exception('oops2')
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2013-11-21 09:15:24 +08:00
|
|
|
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
|
|
|
|
|
2016-06-12 00:04:49 +08:00
|
|
|
def test_exact_teardown_issue1206(self, testdir):
|
2016-07-15 08:28:59 +08:00
|
|
|
"""issue shadowing error with wrong number of arguments on teardown_method."""
|
2016-06-12 00:04:49 +08:00
|
|
|
rec = testdir.inline_runsource("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
class TestClass:
|
2016-07-15 08:28:59 +08:00
|
|
|
def teardown_method(self, x, y, z):
|
2016-06-12 00:04:49 +08:00
|
|
|
pass
|
|
|
|
|
|
|
|
def test_method(self):
|
|
|
|
assert True
|
|
|
|
""")
|
|
|
|
reps = rec.getreports("pytest_runtest_logreport")
|
|
|
|
print (reps)
|
|
|
|
assert len(reps) == 3
|
|
|
|
#
|
|
|
|
assert reps[0].nodeid.endswith("test_method")
|
|
|
|
assert reps[0].passed
|
|
|
|
assert reps[0].when == 'setup'
|
|
|
|
#
|
|
|
|
assert reps[1].nodeid.endswith("test_method")
|
|
|
|
assert reps[1].passed
|
|
|
|
assert reps[1].when == 'call'
|
|
|
|
#
|
|
|
|
assert reps[2].nodeid.endswith("test_method")
|
|
|
|
assert reps[2].failed
|
|
|
|
assert reps[2].when == "teardown"
|
|
|
|
assert reps[2].longrepr.reprcrash.message in (
|
|
|
|
# python3 error
|
2016-07-15 08:28:59 +08:00
|
|
|
"TypeError: teardown_method() missing 2 required positional arguments: 'y' and 'z'",
|
2016-06-12 00:04:49 +08:00
|
|
|
# python2 error
|
2016-07-15 08:28:59 +08:00
|
|
|
'TypeError: teardown_method() takes exactly 4 arguments (2 given)'
|
2016-06-12 00:04:49 +08:00
|
|
|
)
|
|
|
|
|
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
|
2015-08-21 09:17:05 +08:00
|
|
|
boxed = pytest.importorskip("xdist.boxed")
|
|
|
|
return boxed.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
|
|
|
|
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):
|
2015-09-16 18:33:53 +08:00
|
|
|
if hasattr(py.std.inspect, 'signature'):
|
|
|
|
args = list(py.std.inspect.signature(reporttype.__init__).parameters.keys())[1:]
|
|
|
|
else:
|
|
|
|
args = py.std.inspect.getargspec(reporttype.__init__)[0][1:]
|
2012-01-21 02:50:45 +08:00
|
|
|
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("""
|
2016-07-12 09:03:53 +08:00
|
|
|
import pytest
|
2009-12-30 17:42:01 +08:00
|
|
|
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']
|
2016-07-12 09:03:53 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def mylist(self, request):
|
2009-12-30 17:42:01 +08:00
|
|
|
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:
|
2015-11-27 22:43:01 +08:00
|
|
|
excinfo = _pytest._code.ExceptionInfo()
|
2010-04-28 14:42:56 +08:00
|
|
|
assert excinfo.errisinstance(KeyboardInterrupt)
|
|
|
|
|
|
|
|
def test_pytest_fail():
|
|
|
|
try:
|
2014-01-18 19:31:33 +08:00
|
|
|
pytest.fail("hello")
|
|
|
|
except pytest.fail.Exception:
|
2015-11-27 22:43:01 +08:00
|
|
|
excinfo = _pytest._code.ExceptionInfo()
|
2010-04-28 14:42:56 +08:00
|
|
|
s = excinfo.exconly(tryshort=True)
|
|
|
|
assert s.startswith("Failed")
|
|
|
|
|
2016-07-24 20:13:43 +08:00
|
|
|
def test_pytest_exit_msg(testdir):
|
|
|
|
testdir.makeconftest("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
def pytest_configure(config):
|
|
|
|
pytest.exit('oh noes')
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stderr.fnmatch_lines([
|
|
|
|
"Exit: oh noes",
|
|
|
|
])
|
|
|
|
|
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
|
|
|
|
2015-07-05 01:42:22 +08:00
|
|
|
|
2016-03-06 06:34:15 +08:00
|
|
|
@pytest.mark.parametrize('str_prefix', ['u', ''])
|
|
|
|
def test_pytest_fail_notrace_non_ascii(testdir, str_prefix):
|
2016-03-06 03:09:01 +08:00
|
|
|
"""Fix pytest.fail with pytrace=False with non-ascii characters (#1178).
|
2016-03-06 06:34:15 +08:00
|
|
|
|
|
|
|
This tests with native and unicode strings containing non-ascii chars.
|
2016-03-06 03:09:01 +08:00
|
|
|
"""
|
|
|
|
testdir.makepyfile(u"""
|
|
|
|
# coding: utf-8
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
def test_hello():
|
2016-03-06 06:34:15 +08:00
|
|
|
pytest.fail(%s'oh oh: ☺', pytrace=False)
|
|
|
|
""" % str_prefix)
|
2016-03-06 03:09:01 +08:00
|
|
|
result = testdir.runpytest()
|
|
|
|
if sys.version_info[0] >= 3:
|
|
|
|
result.stdout.fnmatch_lines(['*test_hello*', "oh oh: ☺"])
|
|
|
|
else:
|
|
|
|
result.stdout.fnmatch_lines(['*test_hello*', "oh oh: *"])
|
|
|
|
assert 'def test_hello' not in result.stdout.str()
|
|
|
|
|
|
|
|
|
2015-07-05 01:42:22 +08:00
|
|
|
def test_pytest_no_tests_collected_exit_status(testdir):
|
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stdout.fnmatch_lines('*collected 0 items*')
|
|
|
|
assert result.ret == main.EXIT_NOTESTSCOLLECTED
|
|
|
|
|
|
|
|
testdir.makepyfile(test_foo="""
|
|
|
|
def test_foo():
|
|
|
|
assert 1
|
|
|
|
""")
|
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stdout.fnmatch_lines('*collected 1 items*')
|
|
|
|
result.stdout.fnmatch_lines('*1 passed*')
|
|
|
|
assert result.ret == main.EXIT_OK
|
|
|
|
|
|
|
|
result = testdir.runpytest('-k nonmatch')
|
|
|
|
result.stdout.fnmatch_lines('*collected 1 items*')
|
|
|
|
result.stdout.fnmatch_lines('*1 deselected*')
|
|
|
|
assert result.ret == main.EXIT_NOTESTSCOLLECTED
|
|
|
|
|
|
|
|
|
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:
|
2015-11-27 22:43:01 +08:00
|
|
|
excinfo = _pytest._code.ExceptionInfo()
|
2010-04-28 14:42:56 +08:00
|
|
|
s = excinfo.exconly(tryshort=True)
|
|
|
|
assert s.startswith("Skipped")
|
|
|
|
|
2015-07-24 13:12:01 +08:00
|
|
|
def test_importorskip(monkeypatch):
|
2014-01-18 19:31:33 +08:00
|
|
|
importorskip = pytest.importorskip
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2010-11-14 04:03:28 +08:00
|
|
|
def f():
|
|
|
|
importorskip("asdlkj")
|
2016-11-21 04:59:15 +08:00
|
|
|
|
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"
|
2015-07-24 13:12:01 +08:00
|
|
|
monkeypatch.setitem(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:
|
2015-11-27 22:43:01 +08:00
|
|
|
print(_pytest._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
|
|
|
|
|
2015-07-24 13:12:01 +08:00
|
|
|
def test_importorskip_dev_module(monkeypatch):
|
2015-07-24 04:38:25 +08:00
|
|
|
try:
|
|
|
|
mod = py.std.types.ModuleType("mockmodule")
|
|
|
|
mod.__version__ = '0.13.0.dev-43290'
|
2015-07-24 13:12:01 +08:00
|
|
|
monkeypatch.setitem(sys.modules, 'mockmodule', mod)
|
2015-07-24 04:38:25 +08:00
|
|
|
mod2 = pytest.importorskip('mockmodule', minversion='0.12.0')
|
|
|
|
assert mod2 == mod
|
|
|
|
pytest.raises(pytest.skip.Exception, """
|
|
|
|
pytest.importorskip('mockmodule1', minversion='0.14.0')""")
|
|
|
|
except pytest.skip.Exception:
|
2015-11-27 22:43:01 +08:00
|
|
|
print(_pytest._code.ExceptionInfo())
|
2015-07-24 04:38:25 +08:00
|
|
|
pytest.fail("spurious skip")
|
|
|
|
|
2010-04-28 14:42:56 +08:00
|
|
|
|
2016-08-20 05:21:25 +08:00
|
|
|
def test_importorskip_module_level(testdir):
|
|
|
|
"""importorskip must be able to skip entire modules when used at module level"""
|
|
|
|
testdir.makepyfile('''
|
|
|
|
import pytest
|
|
|
|
foobarbaz = pytest.importorskip("foobarbaz")
|
|
|
|
|
|
|
|
def test_foo():
|
|
|
|
pass
|
|
|
|
''')
|
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stdout.fnmatch_lines(['*collected 0 items / 1 skipped*'])
|
|
|
|
|
|
|
|
|
2010-04-28 14:42:56 +08:00
|
|
|
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*'])
|
2015-03-22 00:06:24 +08:00
|
|
|
|
|
|
|
|
2016-02-26 20:14:55 +08:00
|
|
|
def test_makereport_getsource_dynamic_code(testdir, monkeypatch):
|
2015-09-15 07:14:58 +08:00
|
|
|
"""Test that exception in dynamically generated code doesn't break getting the source line."""
|
2016-02-26 20:14:55 +08:00
|
|
|
import inspect
|
|
|
|
original_findsource = inspect.findsource
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2016-02-26 20:14:55 +08:00
|
|
|
def findsource(obj, *args, **kwargs):
|
|
|
|
# Can be triggered by dynamically created functions
|
|
|
|
if obj.__name__ == 'foo':
|
|
|
|
raise IndexError()
|
|
|
|
return original_findsource(obj, *args, **kwargs)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2016-02-26 20:14:55 +08:00
|
|
|
monkeypatch.setattr(inspect, 'findsource', findsource)
|
2015-09-15 07:14:58 +08:00
|
|
|
|
2016-02-26 20:14:55 +08:00
|
|
|
testdir.makepyfile("""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def foo(missing):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_fix(foo):
|
|
|
|
assert False
|
|
|
|
""")
|
2015-09-15 07:14:58 +08:00
|
|
|
result = testdir.runpytest('-vv')
|
|
|
|
assert 'INTERNALERROR' not in result.stdout.str()
|
2016-02-26 20:14:55 +08:00
|
|
|
result.stdout.fnmatch_lines(["*test_fix*", "*fixture*'missing'*not found*"])
|
2015-09-15 07:14:58 +08:00
|
|
|
|
|
|
|
|
2015-03-22 00:26:23 +08:00
|
|
|
def test_store_except_info_on_eror():
|
|
|
|
""" Test that upon test failure, the exception info is stored on
|
|
|
|
sys.last_traceback and friends.
|
|
|
|
"""
|
2015-03-22 00:06:24 +08:00
|
|
|
# Simulate item that raises a specific exception
|
|
|
|
class ItemThatRaises:
|
|
|
|
def runtest(self):
|
|
|
|
raise IndexError('TEST')
|
|
|
|
try:
|
|
|
|
runner.pytest_runtest_call(ItemThatRaises())
|
|
|
|
except IndexError:
|
|
|
|
pass
|
|
|
|
# Check that exception info is stored on sys
|
|
|
|
assert sys.last_type is IndexError
|
|
|
|
assert sys.last_value.args[0] == 'TEST'
|
|
|
|
assert sys.last_traceback
|
2016-08-04 08:11:19 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TestReportContents:
|
|
|
|
"""
|
2016-08-04 08:49:43 +08:00
|
|
|
Test user-level API of ``TestReport`` objects.
|
2016-08-04 08:11:19 +08:00
|
|
|
"""
|
|
|
|
|
2016-08-04 08:49:43 +08:00
|
|
|
def getrunner(self):
|
|
|
|
return lambda item: runner.runtestprotocol(item, log=False)
|
|
|
|
|
|
|
|
def test_longreprtext_pass(self, testdir):
|
2016-08-04 08:11:19 +08:00
|
|
|
reports = testdir.runitem("""
|
|
|
|
def test_func():
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
rep = reports[1]
|
|
|
|
assert rep.longreprtext == ''
|
|
|
|
|
2016-08-04 08:49:43 +08:00
|
|
|
def test_longreprtext_failure(self, testdir):
|
2016-08-04 08:11:19 +08:00
|
|
|
reports = testdir.runitem("""
|
|
|
|
def test_func():
|
|
|
|
x = 1
|
|
|
|
assert x == 4
|
|
|
|
""")
|
|
|
|
rep = reports[1]
|
|
|
|
assert 'assert 1 == 4' in rep.longreprtext
|
|
|
|
|
2016-08-04 08:49:43 +08:00
|
|
|
def test_captured_text(self, testdir):
|
|
|
|
reports = testdir.runitem("""
|
|
|
|
import pytest
|
|
|
|
import sys
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def fix():
|
|
|
|
sys.stdout.write('setup: stdout\\n')
|
|
|
|
sys.stderr.write('setup: stderr\\n')
|
|
|
|
yield
|
|
|
|
sys.stdout.write('teardown: stdout\\n')
|
|
|
|
sys.stderr.write('teardown: stderr\\n')
|
|
|
|
assert 0
|
|
|
|
|
|
|
|
def test_func(fix):
|
|
|
|
sys.stdout.write('call: stdout\\n')
|
|
|
|
sys.stderr.write('call: stderr\\n')
|
|
|
|
assert 0
|
|
|
|
""")
|
|
|
|
setup, call, teardown = reports
|
|
|
|
assert setup.capstdout == 'setup: stdout\n'
|
|
|
|
assert call.capstdout == 'setup: stdout\ncall: stdout\n'
|
|
|
|
assert teardown.capstdout == 'setup: stdout\ncall: stdout\nteardown: stdout\n'
|
|
|
|
|
|
|
|
assert setup.capstderr == 'setup: stderr\n'
|
|
|
|
assert call.capstderr == 'setup: stderr\ncall: stderr\n'
|
|
|
|
assert teardown.capstderr == 'setup: stderr\ncall: stderr\nteardown: stderr\n'
|
|
|
|
|
|
|
|
def test_no_captured_text(self, testdir):
|
|
|
|
reports = testdir.runitem("""
|
|
|
|
def test_func():
|
|
|
|
pass
|
|
|
|
""")
|
|
|
|
rep = reports[1]
|
|
|
|
assert rep.capstdout == ''
|
|
|
|
assert rep.capstderr == ''
|
|
|
|
|
|
|
|
|