2017-12-27 11:47:26 +08:00
|
|
|
import inspect
|
2015-11-27 22:43:01 +08:00
|
|
|
import os
|
|
|
|
import sys
|
2017-12-27 11:47:26 +08:00
|
|
|
import types
|
2018-10-25 15:01:29 +08:00
|
|
|
|
|
|
|
import py
|
|
|
|
|
|
|
|
import _pytest._code
|
|
|
|
import pytest
|
|
|
|
from _pytest import main
|
|
|
|
from _pytest import outcomes
|
|
|
|
from _pytest import reports
|
|
|
|
from _pytest import runner
|
2009-04-05 04:19:18 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2019-06-03 06:32:00 +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")
|
2017-11-04 23:17:20 +08:00
|
|
|
values = [1]
|
2009-05-21 20:37:30 +08:00
|
|
|
ss.prepare(item)
|
2017-11-04 23:17:20 +08:00
|
|
|
ss.addfinalizer(values.pop, colitem=item)
|
|
|
|
assert values
|
2009-05-21 20:37:30 +08:00
|
|
|
ss._pop_and_teardown()
|
2017-11-04 23:17:20 +08:00
|
|
|
assert not values
|
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):
|
2018-05-23 22:48:46 +08:00
|
|
|
item = testdir.getitem(
|
|
|
|
"""
|
2010-01-28 21:20:58 +08:00
|
|
|
def setup_module(mod):
|
|
|
|
raise ValueError(42)
|
|
|
|
def test_func(): pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
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
|
|
|
|
2017-07-17 07:25:10 +08:00
|
|
|
def fin1():
|
2018-05-23 22:48:46 +08:00
|
|
|
r.append("fin1")
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2017-07-17 07:25:10 +08:00
|
|
|
def fin2():
|
2018-05-23 22:48:46 +08:00
|
|
|
raise Exception("oops")
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2017-07-17 07:25:10 +08:00
|
|
|
def fin3():
|
2018-05-23 22:48:46 +08:00
|
|
|
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)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert err.value.args == ("oops",)
|
|
|
|
assert r == ["fin3", "fin1"]
|
2013-11-20 01:26:18 +08:00
|
|
|
|
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.
|
2017-07-17 07:25:10 +08:00
|
|
|
def fin1():
|
2018-05-23 22:48:46 +08:00
|
|
|
raise Exception("oops1")
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2017-07-17 07:25:10 +08:00
|
|
|
def fin2():
|
2018-05-23 22:48:46 +08:00
|
|
|
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)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert err.value.args == ("oops2",)
|
2013-11-21 09:15:24 +08:00
|
|
|
|
2018-06-12 04:12:47 +08:00
|
|
|
def test_teardown_multiple_scopes_one_fails(self, testdir):
|
|
|
|
module_teardown = []
|
2018-06-12 07:32:08 +08:00
|
|
|
|
2018-06-12 04:12:47 +08:00
|
|
|
def fin_func():
|
|
|
|
raise Exception("oops1")
|
|
|
|
|
|
|
|
def fin_module():
|
|
|
|
module_teardown.append("fin_module")
|
|
|
|
|
|
|
|
item = testdir.getitem("def test_func(): pass")
|
|
|
|
ss = runner.SetupState()
|
|
|
|
ss.addfinalizer(fin_module, item.listchain()[-2])
|
|
|
|
ss.addfinalizer(fin_func, item)
|
|
|
|
ss.prepare(item)
|
2018-06-12 07:33:13 +08:00
|
|
|
with pytest.raises(Exception, match="oops1"):
|
2018-06-12 04:12:47 +08:00
|
|
|
ss.teardown_exact(item, None)
|
|
|
|
assert module_teardown
|
|
|
|
|
2010-01-28 21:20:58 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class BaseFunctionalTests:
|
2009-05-23 01:56:05 +08:00
|
|
|
def test_passfunction(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
reports = testdir.runitem(
|
|
|
|
"""
|
2009-04-05 04:19:18 +08:00
|
|
|
def test_func():
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
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):
|
2018-05-23 22:48:46 +08:00
|
|
|
reports = testdir.runitem(
|
|
|
|
"""
|
2009-05-23 01:56:05 +08:00
|
|
|
def test_func():
|
|
|
|
assert 0
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
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"
|
2017-07-17 07:25:09 +08:00
|
|
|
# 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):
|
2018-05-23 22:48:46 +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")
|
2018-05-23 22:48:46 +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"
|
2017-07-17 07:25:09 +08:00
|
|
|
# assert rep.skipped.when == "call"
|
|
|
|
# assert rep.skipped.when == "call"
|
|
|
|
# assert rep.skipped == "%sreason == "hello"
|
|
|
|
# assert rep.skipped.location.lineno == 3
|
|
|
|
# assert rep.skipped.location.path
|
|
|
|
# assert not rep.skipped.failurerepr
|
2009-05-23 01:56:05 +08:00
|
|
|
|
|
|
|
def test_skip_in_setup_function(self, testdir):
|
2018-05-23 22:48:46 +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
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
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
|
2017-07-17 07:25:09 +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
|
2017-07-17 07:25:09 +08:00
|
|
|
assert reports[1].passed # teardown
|
2009-05-23 01:56:05 +08:00
|
|
|
|
|
|
|
def test_failure_in_setup_function(self, testdir):
|
2018-05-23 22:48:46 +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
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
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):
|
2018-05-23 22:48:46 +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
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
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"
|
2017-07-17 07:25:09 +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):
|
2018-05-23 22:48:46 +08:00
|
|
|
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"
|
2018-05-23 22:48:46 +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
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
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
|
2017-07-17 07:25:09 +08:00
|
|
|
# assert rep.outcome.when == "call"
|
|
|
|
# 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
|
|
|
|
2011-11-19 00:01:29 +08:00
|
|
|
def test_teardown_final_returncode(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
rec = testdir.inline_runsource(
|
|
|
|
"""
|
2011-11-19 00:01:29 +08:00
|
|
|
def test_func():
|
|
|
|
pass
|
|
|
|
def teardown_function(func):
|
|
|
|
raise ValueError(42)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2011-11-19 00:01:29 +08:00
|
|
|
assert rec.ret == 1
|
|
|
|
|
2018-01-10 08:17:39 +08:00
|
|
|
def test_logstart_logfinish_hooks(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
rec = testdir.inline_runsource(
|
|
|
|
"""
|
2018-01-10 08:17:39 +08:00
|
|
|
import pytest
|
|
|
|
def test_func():
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2018-01-10 08:17:39 +08:00
|
|
|
reps = rec.getcalls("pytest_runtest_logstart pytest_runtest_logfinish")
|
2018-06-26 21:35:27 +08:00
|
|
|
assert [x._name for x in reps] == [
|
|
|
|
"pytest_runtest_logstart",
|
|
|
|
"pytest_runtest_logfinish",
|
|
|
|
]
|
2018-01-10 08:17:39 +08:00
|
|
|
for rep in reps:
|
2018-05-23 22:48:46 +08:00
|
|
|
assert rep.nodeid == "test_logstart_logfinish_hooks.py::test_func"
|
|
|
|
assert rep.location == ("test_logstart_logfinish_hooks.py", 1, "test_func")
|
2018-01-10 08:17:39 +08:00
|
|
|
|
2011-11-19 00:01:29 +08:00
|
|
|
def test_exact_teardown_issue90(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
rec = testdir.inline_runsource(
|
|
|
|
"""
|
2011-11-19 00:01:29 +08:00
|
|
|
import pytest
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2011-11-19 00:01:29 +08:00
|
|
|
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)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2011-11-19 00:01:29 +08:00
|
|
|
reps = rec.getreports("pytest_runtest_logreport")
|
2017-07-18 08:16:14 +08:00
|
|
|
print(reps)
|
2011-11-19 00:01:29 +08:00
|
|
|
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
|
2017-07-17 07:25:08 +08:00
|
|
|
for i in range(3, 5):
|
2011-11-19 00:01:29 +08:00
|
|
|
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."""
|
2018-05-23 22:48:46 +08:00
|
|
|
rec = testdir.inline_runsource(
|
|
|
|
"""
|
2016-06-12 00:04:49 +08:00
|
|
|
import pytest
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
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
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2016-06-12 00:04:49 +08:00
|
|
|
reps = rec.getreports("pytest_runtest_logreport")
|
2017-07-18 08:16:14 +08:00
|
|
|
print(reps)
|
2016-06-12 00:04:49 +08:00
|
|
|
assert len(reps) == 3
|
|
|
|
#
|
|
|
|
assert reps[0].nodeid.endswith("test_method")
|
|
|
|
assert reps[0].passed
|
2018-05-23 22:48:46 +08:00
|
|
|
assert reps[0].when == "setup"
|
2016-06-12 00:04:49 +08:00
|
|
|
#
|
|
|
|
assert reps[1].nodeid.endswith("test_method")
|
|
|
|
assert reps[1].passed
|
2018-05-23 22:48:46 +08:00
|
|
|
assert reps[1].when == "call"
|
2016-06-12 00:04:49 +08:00
|
|
|
#
|
|
|
|
assert reps[2].nodeid.endswith("test_method")
|
|
|
|
assert reps[2].failed
|
|
|
|
assert reps[2].when == "teardown"
|
|
|
|
assert reps[2].longrepr.reprcrash.message in (
|
2017-07-17 07:25:07 +08:00
|
|
|
# python3 error
|
|
|
|
"TypeError: teardown_method() missing 2 required positional arguments: 'y' and 'z'",
|
|
|
|
# python2 error
|
2018-05-23 22:48:46 +08:00
|
|
|
"TypeError: teardown_method() takes exactly 4 arguments (2 given)",
|
2017-07-17 07:25:07 +08:00
|
|
|
)
|
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):
|
2018-05-23 22:48:46 +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
|
2018-05-23 22:48:46 +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
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
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
|
2017-07-17 07:25:09 +08:00
|
|
|
# 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_systemexit_does_not_bail_out(self, testdir):
|
|
|
|
try:
|
2018-05-23 22:48:46 +08:00
|
|
|
reports = testdir.runitem(
|
|
|
|
"""
|
2009-05-23 01:56:05 +08:00
|
|
|
def test_func():
|
|
|
|
raise SystemExit(42)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2009-05-23 01:56:05 +08:00
|
|
|
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:
|
2018-05-23 22:48:46 +08:00
|
|
|
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()
|
2018-05-23 22:48:46 +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
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
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)
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2009-06-09 22:08:34 +08:00
|
|
|
return f
|
2009-05-23 01:56:05 +08:00
|
|
|
|
|
|
|
def test_keyboardinterrupt_propagates(self, testdir):
|
|
|
|
try:
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.runitem(
|
|
|
|
"""
|
2009-05-23 01:56:05 +08:00
|
|
|
def test_func():
|
|
|
|
raise KeyboardInterrupt("fake")
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
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
|
|
|
|
2017-07-17 07:25:09 +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):
|
2018-05-23 22:48:46 +08:00
|
|
|
reports = testdir.runitem(
|
|
|
|
"""
|
2009-05-23 01:56:05 +08:00
|
|
|
def test_func():
|
|
|
|
import os
|
|
|
|
os.kill(os.getpid(), 15)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
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 == "???"
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestSessionReports:
|
2009-05-23 01:56:05 +08:00
|
|
|
def test_collect_result(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
col = testdir.getmodulecol(
|
|
|
|
"""
|
2009-05-23 01:56:05 +08:00
|
|
|
def test_func1():
|
|
|
|
pass
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2009-05-23 01:56:05 +08:00
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
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
|
|
|
|
2019-01-08 12:46:57 +08:00
|
|
|
reporttypes = [reports.BaseReport, reports.TestReport, reports.CollectReport]
|
2012-01-21 02:50:45 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"reporttype", reporttypes, ids=[x.__name__ for x in reporttypes]
|
|
|
|
)
|
2012-01-21 02:50:45 +08:00
|
|
|
def test_report_extra_parameters(reporttype):
|
2018-05-23 22:48:46 +08:00
|
|
|
if hasattr(inspect, "signature"):
|
2017-12-27 11:47:26 +08:00
|
|
|
args = list(inspect.signature(reporttype.__init__).parameters.keys())[1:]
|
2015-09-16 18:33:53 +08:00
|
|
|
else:
|
2017-12-27 11:47:26 +08:00
|
|
|
args = 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
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2009-08-06 20:34:19 +08:00
|
|
|
def test_callinfo():
|
2018-11-04 04:50:13 +08:00
|
|
|
ci = runner.CallInfo.from_call(lambda: 0, "123")
|
2009-08-06 20:34:19 +08:00
|
|
|
assert ci.when == "123"
|
|
|
|
assert ci.result == 0
|
2010-07-27 03:15:15 +08:00
|
|
|
assert "result" in repr(ci)
|
2018-11-13 15:48:07 +08:00
|
|
|
assert repr(ci) == "<CallInfo when='123' result: 0>"
|
|
|
|
|
2018-11-04 04:50:13 +08:00
|
|
|
ci = runner.CallInfo.from_call(lambda: 0 / 0, "123")
|
2009-08-06 20:34:19 +08:00
|
|
|
assert ci.when == "123"
|
2018-05-23 22:48:46 +08:00
|
|
|
assert not hasattr(ci, "result")
|
2018-11-13 15:48:07 +08:00
|
|
|
assert repr(ci) == "<CallInfo when='123' exception: division by zero>"
|
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
|
|
|
|
2018-05-23 22:48:46 +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
|
2017-07-17 07:25:09 +08:00
|
|
|
|
|
|
|
|
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):
|
2018-05-23 22:48:46 +08:00
|
|
|
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")
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestClass(object):
|
2009-12-30 17:42:01 +08:00
|
|
|
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
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2009-12-30 17:42:01 +08:00
|
|
|
result = testdir.runpytest(p1)
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*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():
|
2018-05-23 22:48:46 +08:00
|
|
|
outcome = outcomes.OutcomeException("test")
|
2013-02-04 23:07:51 +08:00
|
|
|
assert outcome.args[0] == outcome.msg
|
2013-01-27 09:06:19 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-09-19 07:03:05 +08:00
|
|
|
def test_outcomeexception_passes_except_Exception():
|
|
|
|
with pytest.raises(outcomes.OutcomeException):
|
|
|
|
try:
|
2018-05-23 22:48:46 +08:00
|
|
|
raise outcomes.OutcomeException("test")
|
2015-09-19 07:03:05 +08:00
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2010-04-28 14:42:56 +08:00
|
|
|
def test_pytest_exit():
|
2018-11-22 19:20:14 +08:00
|
|
|
with pytest.raises(pytest.exit.Exception) as excinfo:
|
2014-01-18 19:31:33 +08:00
|
|
|
pytest.exit("hello")
|
2018-11-03 02:50:29 +08:00
|
|
|
assert excinfo.errisinstance(pytest.exit.Exception)
|
2010-04-28 14:42:56 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2010-04-28 14:42:56 +08:00
|
|
|
def test_pytest_fail():
|
2018-11-22 19:20:14 +08:00
|
|
|
with pytest.raises(pytest.fail.Exception) as excinfo:
|
2014-01-18 19:31:33 +08:00
|
|
|
pytest.fail("hello")
|
2018-11-22 19:20:14 +08:00
|
|
|
s = excinfo.exconly(tryshort=True)
|
|
|
|
assert s.startswith("Failed")
|
2010-04-28 14:42:56 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2016-07-24 20:13:43 +08:00
|
|
|
def test_pytest_exit_msg(testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makeconftest(
|
|
|
|
"""
|
2016-07-24 20:13:43 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
def pytest_configure(config):
|
|
|
|
pytest.exit('oh noes')
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2016-07-24 20:13:43 +08:00
|
|
|
result = testdir.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stderr.fnmatch_lines(["Exit: oh noes"])
|
2016-07-24 20:13:43 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2018-10-15 03:41:16 +08:00
|
|
|
def test_pytest_exit_returncode(testdir):
|
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
def test_foo():
|
|
|
|
pytest.exit("some exit msg", 99)
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = testdir.runpytest()
|
2019-04-03 10:07:42 +08:00
|
|
|
result.stdout.fnmatch_lines(["*! *Exit: some exit msg !*"])
|
2019-04-15 05:26:56 +08:00
|
|
|
# Assert no output on stderr, except for unreliable ResourceWarnings.
|
2019-04-15 03:55:15 +08:00
|
|
|
# (https://github.com/pytest-dev/pytest/issues/5088)
|
|
|
|
assert [
|
|
|
|
x
|
|
|
|
for x in result.stderr.lines
|
|
|
|
if not x.startswith("Exception ignored in:")
|
|
|
|
and not x.startswith("ResourceWarning")
|
|
|
|
] == [""]
|
2018-10-15 03:41:16 +08:00
|
|
|
assert result.ret == 99
|
2018-10-14 23:20:10 +08:00
|
|
|
|
2019-04-03 10:07:42 +08:00
|
|
|
# It prints to stderr also in case of exit during pytest_sessionstart.
|
|
|
|
testdir.makeconftest(
|
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
def pytest_sessionstart():
|
|
|
|
pytest.exit("during_sessionstart", 98)
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stdout.fnmatch_lines(["*! *Exit: during_sessionstart !*"])
|
|
|
|
assert result.stderr.lines == ["Exit: during_sessionstart", ""]
|
|
|
|
assert result.ret == 98
|
|
|
|
|
2018-10-14 23:20:10 +08:00
|
|
|
|
2018-10-04 07:07:59 +08:00
|
|
|
def test_pytest_fail_notrace_runtest(testdir):
|
|
|
|
"""Test pytest.fail(..., pytrace=False) does not show tracebacks during test run."""
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2010-11-23 22:42:23 +08:00
|
|
|
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)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2010-11-23 22:42:23 +08:00
|
|
|
result = testdir.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["world", "hello"])
|
|
|
|
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
|
|
|
|
2018-10-04 07:07:59 +08:00
|
|
|
def test_pytest_fail_notrace_collection(testdir):
|
|
|
|
"""Test pytest.fail(..., pytrace=False) does not show tracebacks during collection."""
|
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
def some_internal_function():
|
|
|
|
pytest.fail("hello", pytrace=False)
|
|
|
|
some_internal_function()
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = testdir.runpytest()
|
|
|
|
result.stdout.fnmatch_lines(["hello"])
|
|
|
|
assert "def some_internal_function()" not in result.stdout.str()
|
|
|
|
|
|
|
|
|
2019-06-05 08:48:06 +08:00
|
|
|
def test_pytest_fail_notrace_non_ascii(testdir):
|
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
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
2019-06-03 06:40:34 +08:00
|
|
|
"""\
|
2016-03-06 03:09:01 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
def test_hello():
|
2019-06-05 08:48:06 +08:00
|
|
|
pytest.fail('oh oh: ☺', pytrace=False)
|
2019-06-03 06:40:34 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2016-03-06 03:09:01 +08:00
|
|
|
result = testdir.runpytest()
|
2019-05-28 07:31:52 +08:00
|
|
|
result.stdout.fnmatch_lines(["*test_hello*", "oh oh: ☺"])
|
2018-05-23 22:48:46 +08:00
|
|
|
assert "def test_hello" not in result.stdout.str()
|
2016-03-06 03:09:01 +08:00
|
|
|
|
|
|
|
|
2015-07-05 01:42:22 +08:00
|
|
|
def test_pytest_no_tests_collected_exit_status(testdir):
|
|
|
|
result = testdir.runpytest()
|
2019-03-23 18:36:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["*collected 0 items*"])
|
2019-06-07 18:58:51 +08:00
|
|
|
assert result.ret == main.ExitCode.NO_TESTS_COLLECTED
|
2015-07-05 01:42:22 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
test_foo="""
|
2015-07-05 01:42:22 +08:00
|
|
|
def test_foo():
|
|
|
|
assert 1
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-07-05 01:42:22 +08:00
|
|
|
result = testdir.runpytest()
|
2019-03-23 18:36:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["*collected 1 item*"])
|
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
2019-06-07 18:58:51 +08:00
|
|
|
assert result.ret == main.ExitCode.OK
|
2015-07-05 01:42:22 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
result = testdir.runpytest("-k nonmatch")
|
2019-03-23 18:36:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["*collected 1 item*"])
|
|
|
|
result.stdout.fnmatch_lines(["*1 deselected*"])
|
2019-06-07 18:58:51 +08:00
|
|
|
assert result.ret == main.ExitCode.NO_TESTS_COLLECTED
|
2015-07-05 01:42:22 +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:
|
2018-11-22 19:20:14 +08:00
|
|
|
excinfo = _pytest._code.ExceptionInfo.from_current()
|
2010-04-28 14:42:56 +08:00
|
|
|
s = excinfo.exconly(tryshort=True)
|
|
|
|
assert s.startswith("Skipped")
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
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:
|
2017-12-27 11:47:26 +08:00
|
|
|
sysmod = importorskip("sys")
|
|
|
|
assert sysmod is sys
|
2017-07-17 07:25:09 +08:00
|
|
|
# path = pytest.importorskip("os.path")
|
2017-12-27 11:47:26 +08:00
|
|
|
# assert path == 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"
|
2018-11-23 02:05:10 +08:00
|
|
|
pytest.raises(SyntaxError, pytest.importorskip, "x y z")
|
|
|
|
pytest.raises(SyntaxError, pytest.importorskip, "x=y")
|
2017-12-27 11:47:26 +08:00
|
|
|
mod = types.ModuleType("hello123")
|
2010-04-28 14:42:56 +08:00
|
|
|
mod.__version__ = "1.3"
|
2015-07-24 13:12:01 +08:00
|
|
|
monkeypatch.setitem(sys.modules, "hello123", mod)
|
2018-11-23 02:05:10 +08:00
|
|
|
with pytest.raises(pytest.skip.Exception):
|
2014-01-18 19:31:33 +08:00
|
|
|
pytest.importorskip("hello123", minversion="1.3.1")
|
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:
|
2018-11-22 19:20:14 +08:00
|
|
|
print(_pytest._code.ExceptionInfo.from_current())
|
2014-01-18 19:31:33 +08:00
|
|
|
pytest.fail("spurious skip")
|
2010-04-28 14:42:56 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
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
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2015-07-24 13:12:01 +08:00
|
|
|
def test_importorskip_dev_module(monkeypatch):
|
2015-07-24 04:38:25 +08:00
|
|
|
try:
|
2017-12-27 11:47:26 +08:00
|
|
|
mod = types.ModuleType("mockmodule")
|
2018-05-23 22:48:46 +08:00
|
|
|
mod.__version__ = "0.13.0.dev-43290"
|
|
|
|
monkeypatch.setitem(sys.modules, "mockmodule", mod)
|
|
|
|
mod2 = pytest.importorskip("mockmodule", minversion="0.12.0")
|
2015-07-24 04:38:25 +08:00
|
|
|
assert mod2 == mod
|
2018-11-23 02:05:10 +08:00
|
|
|
with pytest.raises(pytest.skip.Exception):
|
|
|
|
pytest.importorskip("mockmodule1", minversion="0.14.0")
|
2015-07-24 04:38:25 +08:00
|
|
|
except pytest.skip.Exception:
|
2018-11-22 19:20:14 +08:00
|
|
|
print(_pytest._code.ExceptionInfo.from_current())
|
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"""
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2016-08-20 05:21:25 +08:00
|
|
|
import pytest
|
|
|
|
foobarbaz = pytest.importorskip("foobarbaz")
|
|
|
|
|
|
|
|
def test_foo():
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2016-08-20 05:21:25 +08:00
|
|
|
result = testdir.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*collected 0 items / 1 skipped*"])
|
2016-08-20 05:21:25 +08:00
|
|
|
|
|
|
|
|
2019-01-05 03:02:07 +08:00
|
|
|
def test_importorskip_custom_reason(testdir):
|
|
|
|
"""make sure custom reasons are used"""
|
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
foobarbaz = pytest.importorskip("foobarbaz2", reason="just because")
|
|
|
|
|
|
|
|
def test_foo():
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = testdir.runpytest("-ra")
|
|
|
|
result.stdout.fnmatch_lines(["*just because*"])
|
|
|
|
result.stdout.fnmatch_lines(["*collected 0 items / 1 skipped*"])
|
|
|
|
|
|
|
|
|
2010-04-28 14:42:56 +08:00
|
|
|
def test_pytest_cmdline_main(testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
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__])
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2010-04-28 14:42:56 +08:00
|
|
|
import subprocess
|
2018-05-23 22:48:46 +08:00
|
|
|
|
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):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makeconftest(
|
2019-06-03 06:40:34 +08:00
|
|
|
"""\
|
2017-08-31 07:23:55 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.hookimpl(hookwrapper=True)
|
|
|
|
def pytest_runtest_makereport():
|
|
|
|
outcome = yield
|
|
|
|
rep = outcome.get_result()
|
2012-06-24 22:42:31 +08:00
|
|
|
if rep.when == "call":
|
2019-06-05 08:48:06 +08:00
|
|
|
rep.longrepr = 'ä'
|
2019-06-03 06:40:34 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2012-06-24 22:42:31 +08:00
|
|
|
def test_out():
|
|
|
|
assert 0
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2012-06-24 22:42:31 +08:00
|
|
|
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):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2013-09-05 21:43:19 +08:00
|
|
|
def setup_module():
|
|
|
|
0/0
|
|
|
|
def test_func():
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2013-09-05 21:43:19 +08:00
|
|
|
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):
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2014-08-15 06:23:04 +08:00
|
|
|
def test_foo():
|
|
|
|
if False: pass
|
|
|
|
else: assert False
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2014-08-15 06:23:04 +08:00
|
|
|
result = testdir.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
assert "INTERNALERROR" not in result.stdout.str()
|
|
|
|
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
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2016-02-26 20:14:55 +08:00
|
|
|
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
|
2018-05-23 22:48:46 +08:00
|
|
|
if obj.__name__ == "foo":
|
2016-02-26 20:14:55 +08:00
|
|
|
raise IndexError()
|
|
|
|
return original_findsource(obj, *args, **kwargs)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
monkeypatch.setattr(inspect, "findsource", findsource)
|
2015-09-15 07:14:58 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2016-02-26 20:14:55 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def foo(missing):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_fix(foo):
|
|
|
|
assert False
|
2018-05-23 22:48:46 +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
|
|
|
|
|
|
|
|
2018-04-05 07:36:07 +08:00
|
|
|
def test_store_except_info_on_error():
|
2015-03-22 00:26:23 +08:00
|
|
|
""" Test that upon test failure, the exception info is stored on
|
|
|
|
sys.last_traceback and friends.
|
|
|
|
"""
|
2018-04-05 07:36:07 +08:00
|
|
|
# Simulate item that might raise a specific exception, depending on `raise_error` class var
|
2019-06-03 06:32:00 +08:00
|
|
|
class ItemMightRaise:
|
2018-05-23 22:48:46 +08:00
|
|
|
nodeid = "item_that_raises"
|
2018-04-05 07:36:07 +08:00
|
|
|
raise_error = True
|
2017-07-20 04:09:05 +08:00
|
|
|
|
2015-03-22 00:06:24 +08:00
|
|
|
def runtest(self):
|
2018-04-05 07:36:07 +08:00
|
|
|
if self.raise_error:
|
2018-05-23 22:48:46 +08:00
|
|
|
raise IndexError("TEST")
|
|
|
|
|
2015-03-22 00:06:24 +08:00
|
|
|
try:
|
2018-04-05 07:36:07 +08:00
|
|
|
runner.pytest_runtest_call(ItemMightRaise())
|
2015-03-22 00:06:24 +08:00
|
|
|
except IndexError:
|
|
|
|
pass
|
|
|
|
# Check that exception info is stored on sys
|
|
|
|
assert sys.last_type is IndexError
|
2018-05-23 22:48:46 +08:00
|
|
|
assert sys.last_value.args[0] == "TEST"
|
2015-03-22 00:06:24 +08:00
|
|
|
assert sys.last_traceback
|
2016-08-04 08:11:19 +08:00
|
|
|
|
2018-04-05 07:36:07 +08:00
|
|
|
# The next run should clear the exception info stored by the previous run
|
|
|
|
ItemMightRaise.raise_error = False
|
|
|
|
runner.pytest_runtest_call(ItemMightRaise())
|
|
|
|
assert sys.last_type is None
|
|
|
|
assert sys.last_value is None
|
|
|
|
assert sys.last_traceback is None
|
|
|
|
|
2016-08-04 08:11:19 +08:00
|
|
|
|
2017-07-19 04:18:34 +08:00
|
|
|
def test_current_test_env_var(testdir, monkeypatch):
|
|
|
|
pytest_current_test_vars = []
|
2018-05-23 22:48:46 +08:00
|
|
|
monkeypatch.setattr(
|
|
|
|
sys, "pytest_current_test_vars", pytest_current_test_vars, raising=False
|
|
|
|
)
|
|
|
|
testdir.makepyfile(
|
|
|
|
"""
|
2017-07-19 04:18:34 +08:00
|
|
|
import pytest
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def fix():
|
|
|
|
sys.pytest_current_test_vars.append(('setup', os.environ['PYTEST_CURRENT_TEST']))
|
|
|
|
yield
|
|
|
|
sys.pytest_current_test_vars.append(('teardown', os.environ['PYTEST_CURRENT_TEST']))
|
|
|
|
|
|
|
|
def test(fix):
|
|
|
|
sys.pytest_current_test_vars.append(('call', os.environ['PYTEST_CURRENT_TEST']))
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2017-07-19 04:18:34 +08:00
|
|
|
result = testdir.runpytest_inprocess()
|
|
|
|
assert result.ret == 0
|
2018-05-23 22:48:46 +08:00
|
|
|
test_id = "test_current_test_env_var.py::test"
|
2018-06-26 21:35:27 +08:00
|
|
|
assert pytest_current_test_vars == [
|
|
|
|
("setup", test_id + " (setup)"),
|
|
|
|
("call", test_id + " (call)"),
|
|
|
|
("teardown", test_id + " (teardown)"),
|
|
|
|
]
|
2018-05-23 22:48:46 +08:00
|
|
|
assert "PYTEST_CURRENT_TEST" not in os.environ
|
2017-07-19 04:18:34 +08:00
|
|
|
|
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestReportContents:
|
2016-08-04 08:11:19 +08:00
|
|
|
"""
|
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):
|
2018-05-23 22:48:46 +08:00
|
|
|
reports = testdir.runitem(
|
|
|
|
"""
|
2016-08-04 08:11:19 +08:00
|
|
|
def test_func():
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2016-08-04 08:11:19 +08:00
|
|
|
rep = reports[1]
|
2018-05-23 22:48:46 +08:00
|
|
|
assert rep.longreprtext == ""
|
2016-08-04 08:11:19 +08:00
|
|
|
|
2016-08-04 08:49:43 +08:00
|
|
|
def test_longreprtext_failure(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
reports = testdir.runitem(
|
|
|
|
"""
|
2016-08-04 08:11:19 +08:00
|
|
|
def test_func():
|
|
|
|
x = 1
|
|
|
|
assert x == 4
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2016-08-04 08:11:19 +08:00
|
|
|
rep = reports[1]
|
2018-05-23 22:48:46 +08:00
|
|
|
assert "assert 1 == 4" in rep.longreprtext
|
2016-08-04 08:11:19 +08:00
|
|
|
|
2016-08-04 08:49:43 +08:00
|
|
|
def test_captured_text(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
reports = testdir.runitem(
|
|
|
|
"""
|
2016-08-04 08:49:43 +08:00
|
|
|
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
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2016-08-04 08:49:43 +08:00
|
|
|
setup, call, teardown = reports
|
2018-05-23 22:48:46 +08:00
|
|
|
assert setup.capstdout == "setup: stdout\n"
|
|
|
|
assert call.capstdout == "setup: stdout\ncall: stdout\n"
|
|
|
|
assert teardown.capstdout == "setup: stdout\ncall: stdout\nteardown: stdout\n"
|
2016-08-04 08:49:43 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
assert setup.capstderr == "setup: stderr\n"
|
|
|
|
assert call.capstderr == "setup: stderr\ncall: stderr\n"
|
|
|
|
assert teardown.capstderr == "setup: stderr\ncall: stderr\nteardown: stderr\n"
|
2016-08-04 08:49:43 +08:00
|
|
|
|
|
|
|
def test_no_captured_text(self, testdir):
|
2018-05-23 22:48:46 +08:00
|
|
|
reports = testdir.runitem(
|
|
|
|
"""
|
2016-08-04 08:49:43 +08:00
|
|
|
def test_func():
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2016-08-04 08:49:43 +08:00
|
|
|
rep = reports[1]
|
2018-05-23 22:48:46 +08:00
|
|
|
assert rep.capstdout == ""
|
|
|
|
assert rep.capstderr == ""
|