test_ok2/testing/test_runner.py

1031 lines
31 KiB
Python
Raw Normal View History

2017-12-27 11:47:26 +08:00
import inspect
import os
import sys
2017-12-27 11:47:26 +08:00
import types
2019-12-03 20:48:22 +08:00
from typing import Dict
from typing import List
from typing import Tuple
import py
import _pytest._code
import pytest
from _pytest import outcomes
from _pytest import reports
from _pytest import runner
2020-02-20 05:07:54 +08:00
from _pytest.compat import TYPE_CHECKING
from _pytest.config import ExitCode
from _pytest.outcomes import OutcomeException
2019-12-03 20:48:22 +08:00
2020-02-20 05:07:54 +08:00
if TYPE_CHECKING:
2019-12-03 20:48:22 +08:00
from typing import Type
2019-06-03 06:32:00 +08:00
class TestSetupState:
2019-12-03 20:48:22 +08:00
def test_setup(self, testdir) -> None:
ss = runner.SetupState()
item = testdir.getitem("def test_func(): pass")
values = [1]
ss.prepare(item)
ss.addfinalizer(values.pop, colitem=item)
assert values
ss._pop_and_teardown()
assert not values
2019-12-03 20:48:22 +08:00
def test_teardown_exact_stack_empty(self, testdir) -> None:
item = testdir.getitem("def test_func(): pass")
ss = runner.SetupState()
ss.teardown_exact(item, None)
ss.teardown_exact(item, None)
ss.teardown_exact(item, None)
2019-12-03 20:48:22 +08:00
def test_setup_fails_and_failure_is_cached(self, testdir) -> None:
2018-05-23 22:48:46 +08:00
item = testdir.getitem(
"""
def setup_module(mod):
raise ValueError(42)
def test_func(): pass
2018-05-23 22:48:46 +08:00
"""
)
ss = runner.SetupState()
pytest.raises(ValueError, lambda: ss.prepare(item))
pytest.raises(ValueError, lambda: ss.prepare(item))
2019-12-03 20:48:22 +08:00
def test_teardown_multiple_one_fails(self, testdir) -> None:
r = []
def fin1():
2018-05-23 22:48:46 +08:00
r.append("fin1")
def fin2():
2018-05-23 22:48:46 +08:00
raise Exception("oops")
def fin3():
2018-05-23 22:48:46 +08:00
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)
2018-05-23 22:48:46 +08:00
assert err.value.args == ("oops",)
assert r == ["fin3", "fin1"]
2019-12-03 20:48:22 +08:00
def test_teardown_multiple_fail(self, testdir) -> None:
# Ensure the first exception is the one which is re-raised.
# Ideally both would be reported however.
def fin1():
2018-05-23 22:48:46 +08:00
raise Exception("oops1")
def fin2():
2018-05-23 22:48:46 +08:00
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)
2018-05-23 22:48:46 +08:00
assert err.value.args == ("oops2",)
2019-12-03 20:48:22 +08:00
def test_teardown_multiple_scopes_one_fails(self, testdir) -> None:
module_teardown = []
2018-06-12 07:32:08 +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)
with pytest.raises(Exception, match="oops1"):
ss.teardown_exact(item, None)
assert module_teardown
2019-06-03 06:32:00 +08:00
class BaseFunctionalTests:
2019-12-03 20:48:22 +08:00
def test_passfunction(self, testdir) -> None:
2018-05-23 22:48:46 +08:00
reports = testdir.runitem(
"""
def test_func():
pass
2018-05-23 22:48:46 +08:00
"""
)
rep = reports[1]
assert rep.passed
assert not rep.failed
assert rep.outcome == "passed"
assert not rep.longrepr
2019-12-03 20:48:22 +08:00
def test_failfunction(self, testdir) -> None:
2018-05-23 22:48:46 +08:00
reports = testdir.runitem(
"""
def test_func():
assert 0
2018-05-23 22:48:46 +08:00
"""
)
rep = reports[1]
assert not rep.passed
assert not rep.skipped
assert rep.failed
assert rep.when == "call"
assert rep.outcome == "failed"
# assert isinstance(rep.longrepr, ReprExceptionInfo)
2019-12-03 20:48:22 +08:00
def test_skipfunction(self, testdir) -> None:
2018-05-23 22:48:46 +08:00
reports = testdir.runitem(
"""
import pytest
def test_func():
pytest.skip("hello")
2018-05-23 22:48:46 +08:00
"""
)
rep = reports[1]
assert not rep.failed
assert not rep.passed
assert rep.skipped
assert rep.outcome == "skipped"
# 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
2019-12-03 20:48:22 +08:00
def test_skip_in_setup_function(self, testdir) -> None:
2018-05-23 22:48:46 +08:00
reports = testdir.runitem(
"""
import pytest
def setup_function(func):
pytest.skip("hello")
def test_func():
pass
2018-05-23 22:48:46 +08:00
"""
)
print(reports)
rep = reports[0]
assert not rep.failed
assert not rep.passed
assert rep.skipped
# assert rep.skipped.reason == "hello"
# assert rep.skipped.location.lineno == 3
# assert rep.skipped.location.lineno == 3
assert len(reports) == 2
assert reports[1].passed # teardown
2019-12-03 20:48:22 +08:00
def test_failure_in_setup_function(self, testdir) -> None:
2018-05-23 22:48:46 +08:00
reports = testdir.runitem(
"""
import pytest
def setup_function(func):
raise ValueError(42)
def test_func():
pass
2018-05-23 22:48:46 +08:00
"""
)
rep = reports[0]
assert not rep.skipped
assert not rep.passed
assert rep.failed
assert rep.when == "setup"
assert len(reports) == 2
2019-12-03 20:48:22 +08:00
def test_failure_in_teardown_function(self, testdir) -> None:
2018-05-23 22:48:46 +08:00
reports = testdir.runitem(
"""
import pytest
def teardown_function(func):
raise ValueError(42)
def test_func():
pass
2018-05-23 22:48:46 +08:00
"""
)
print(reports)
assert len(reports) == 3
rep = reports[2]
assert not rep.skipped
assert not rep.passed
assert rep.failed
assert rep.when == "teardown"
# assert rep.longrepr.reprcrash.lineno == 3
# assert rep.longrepr.reprtraceback.reprentries
2019-12-03 20:48:22 +08:00
def test_custom_failure_repr(self, testdir) -> None:
2018-05-23 22:48:46 +08:00
testdir.makepyfile(
conftest="""
import pytest
class Function(pytest.Function):
def repr_failure(self, excinfo):
return "hello"
2018-05-23 22:48:46 +08:00
"""
)
reports = testdir.runitem(
"""
import pytest
def test_func():
assert 0
2018-05-23 22:48:46 +08:00
"""
)
rep = reports[1]
assert not rep.skipped
assert not rep.passed
assert rep.failed
# 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"
2019-12-03 20:48:22 +08:00
def test_teardown_final_returncode(self, testdir) -> None:
2018-05-23 22:48:46 +08:00
rec = testdir.inline_runsource(
"""
def test_func():
pass
def teardown_function(func):
raise ValueError(42)
2018-05-23 22:48:46 +08:00
"""
)
assert rec.ret == 1
2019-12-03 20:48:22 +08:00
def test_logstart_logfinish_hooks(self, testdir) -> None:
2018-05-23 22:48:46 +08:00
rec = testdir.inline_runsource(
"""
import pytest
def test_func():
pass
2018-05-23 22:48:46 +08:00
"""
)
reps = rec.getcalls("pytest_runtest_logstart pytest_runtest_logfinish")
assert [x._name for x in reps] == [
"pytest_runtest_logstart",
"pytest_runtest_logfinish",
]
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")
2019-12-03 20:48:22 +08:00
def test_exact_teardown_issue90(self, testdir) -> None:
2018-05-23 22:48:46 +08:00
rec = testdir.inline_runsource(
"""
import pytest
class TestClass(object):
def test_method(self):
pass
def teardown_class(cls):
raise Exception()
def test_func():
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)
def teardown_function(func):
raise ValueError(42)
2018-05-23 22:48:46 +08:00
"""
)
reps = rec.getreports("pytest_runtest_logreport")
2017-07-18 08:16:14 +08:00
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
2019-12-03 20:48:22 +08:00
def test_exact_teardown_issue1206(self, testdir) -> None:
"""issue shadowing error with wrong number of arguments on teardown_method."""
2018-05-23 22:48:46 +08:00
rec = testdir.inline_runsource(
"""
import pytest
class TestClass(object):
def teardown_method(self, x, y, z):
pass
def test_method(self):
assert True
2018-05-23 22:48:46 +08:00
"""
)
reps = rec.getreports("pytest_runtest_logreport")
2017-07-18 08:16:14 +08:00
print(reps)
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"
#
assert reps[1].nodeid.endswith("test_method")
assert reps[1].passed
2018-05-23 22:48:46 +08:00
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
"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)",
)
2019-12-03 20:48:22 +08:00
def test_failure_in_setup_function_ignores_custom_repr(self, testdir) -> None:
2018-05-23 22:48:46 +08:00
testdir.makepyfile(
conftest="""
import pytest
class Function(pytest.Function):
def repr_failure(self, excinfo):
assert 0
2018-05-23 22:48:46 +08:00
"""
)
reports = testdir.runitem(
"""
def setup_function(func):
raise ValueError(42)
def test_func():
pass
2018-05-23 22:48:46 +08:00
"""
)
assert len(reports) == 2
rep = reports[0]
print(rep)
assert not rep.skipped
assert not rep.passed
assert rep.failed
# assert rep.outcome.when == "setup"
# assert rep.outcome.where.lineno == 3
# assert rep.outcome.where.path.basename == "test_func.py"
# assert instanace(rep.failed.failurerepr, PythonFailureRepr)
2019-12-03 20:48:22 +08:00
def test_systemexit_does_not_bail_out(self, testdir) -> None:
try:
2018-05-23 22:48:46 +08:00
reports = testdir.runitem(
"""
def test_func():
raise SystemExit(42)
2018-05-23 22:48:46 +08:00
"""
)
except SystemExit:
assert False, "runner did not catch SystemExit"
rep = reports[1]
assert rep.failed
assert rep.when == "call"
2019-12-03 20:48:22 +08:00
def test_exit_propagates(self, testdir) -> None:
try:
2018-05-23 22:48:46 +08:00
testdir.runitem(
"""
import pytest
def test_func():
raise pytest.exit.Exception()
2018-05-23 22:48:46 +08:00
"""
)
except pytest.exit.Exception:
pass
else:
assert False, "did not raise"
class TestExecutionNonForked(BaseFunctionalTests):
def getrunner(self):
def f(item):
return runner.runtestprotocol(item, log=False)
2018-05-23 22:48:46 +08:00
return f
2019-12-03 20:48:22 +08:00
def test_keyboardinterrupt_propagates(self, testdir) -> None:
try:
2018-05-23 22:48:46 +08:00
testdir.runitem(
"""
def test_func():
raise KeyboardInterrupt("fake")
2018-05-23 22:48:46 +08:00
"""
)
except KeyboardInterrupt:
pass
else:
assert False, "did not raise"
2019-06-03 06:32:00 +08:00
class TestSessionReports:
2019-12-03 20:48:22 +08:00
def test_collect_result(self, testdir) -> None:
2018-05-23 22:48:46 +08:00
col = testdir.getmodulecol(
"""
def test_func1():
pass
class TestClass(object):
pass
2018-05-23 22:48:46 +08:00
"""
)
rep = runner.collect_one_node(col)
assert not rep.failed
assert not rep.skipped
assert rep.passed
locinfo = rep.location
assert locinfo[0] == col.fspath.basename
assert not locinfo[1]
assert locinfo[2] == col.fspath.basename
res = rep.result
assert len(res) == 2
assert res[0].name == "test_func1"
assert res[1].name == "TestClass"
2019-12-03 20:48:22 +08:00
reporttypes = [
reports.BaseReport,
reports.TestReport,
reports.CollectReport,
] # type: List[Type[reports.BaseReport]]
2018-05-23 22:48:46 +08:00
@pytest.mark.parametrize(
"reporttype", reporttypes, ids=[x.__name__ for x in reporttypes]
)
2019-12-03 20:48:22 +08:00
def test_report_extra_parameters(reporttype: "Type[reports.BaseReport]") -> None:
args = list(inspect.signature(reporttype.__init__).parameters.keys())[1:]
2019-12-03 20:48:22 +08:00
basekw = dict.fromkeys(args, []) # type: Dict[str, List[object]]
report = reporttype(newthing=1, **basekw)
assert report.newthing == 1
2019-12-03 20:48:22 +08:00
def test_callinfo() -> None:
ci = runner.CallInfo.from_call(lambda: 0, "123")
assert ci.when == "123"
assert ci.result == 0
assert "result" in repr(ci)
assert repr(ci) == "<CallInfo when='123' result: 0>"
2019-10-19 17:05:12 +08:00
assert str(ci) == "<CallInfo when='123' result: 0>"
ci = runner.CallInfo.from_call(lambda: 0 / 0, "123")
assert ci.when == "123"
2018-05-23 22:48:46 +08:00
assert not hasattr(ci, "result")
2019-10-19 17:05:12 +08:00
assert repr(ci) == "<CallInfo when='123' excinfo={!r}>".format(ci.excinfo)
assert str(ci) == repr(ci)
assert ci.excinfo
2019-10-19 17:05:12 +08:00
# Newlines are escaped.
def raise_assertion():
assert 0, "assert_msg"
ci = runner.CallInfo.from_call(raise_assertion, "call")
assert repr(ci) == "<CallInfo when='call' excinfo={!r}>".format(ci.excinfo)
assert "\n" not in repr(ci)
2018-05-23 22:48:46 +08:00
# design question: do we want general hooks in python files?
# then something like the following functional tests makes sense
@pytest.mark.xfail
2019-12-03 20:48:22 +08:00
def test_runtest_in_module_ordering(testdir) -> None:
2018-05-23 22:48:46 +08:00
p1 = testdir.makepyfile(
"""
import pytest
def pytest_runtest_setup(item): # runs after class-level!
item.function.mylist.append("module")
class TestClass(object):
def pytest_runtest_setup(self, item):
assert not hasattr(item.function, 'mylist')
item.function.mylist = ['class']
@pytest.fixture
def mylist(self, request):
return request.function.mylist
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_call(self, item):
try:
(yield).get_result()
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):
del item.function.mylist
2018-05-23 22:48:46 +08:00
"""
)
result = testdir.runpytest(p1)
2018-05-23 22:48:46 +08:00
result.stdout.fnmatch_lines(["*2 passed*"])
2019-12-03 20:48:22 +08:00
def test_outcomeexception_exceptionattributes() -> None:
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
2019-12-03 20:48:22 +08:00
def test_outcomeexception_passes_except_Exception() -> None:
with pytest.raises(outcomes.OutcomeException):
try:
2018-05-23 22:48:46 +08:00
raise outcomes.OutcomeException("test")
except Exception:
raise NotImplementedError()
2019-12-03 20:48:22 +08:00
def test_pytest_exit() -> None:
with pytest.raises(pytest.exit.Exception) as excinfo:
pytest.exit("hello")
assert excinfo.errisinstance(pytest.exit.Exception)
2019-12-03 20:48:22 +08:00
def test_pytest_fail() -> None:
with pytest.raises(pytest.fail.Exception) as excinfo:
pytest.fail("hello")
s = excinfo.exconly(tryshort=True)
assert s.startswith("Failed")
2019-12-03 20:48:22 +08:00
def test_pytest_exit_msg(testdir) -> None:
2018-05-23 22:48:46 +08:00
testdir.makeconftest(
"""
import pytest
def pytest_configure(config):
pytest.exit('oh noes')
2018-05-23 22:48:46 +08:00
"""
)
result = testdir.runpytest()
2018-05-23 22:48:46 +08:00
result.stderr.fnmatch_lines(["Exit: oh noes"])
def _strip_resource_warnings(lines):
# Assert no output on stderr, except for unreliable ResourceWarnings.
# (https://github.com/pytest-dev/pytest/issues/5088)
return [
x
for x in lines
if not x.startswith(("Exception ignored in:", "ResourceWarning"))
]
2019-12-03 20:48:22 +08:00
def test_pytest_exit_returncode(testdir) -> None:
testdir.makepyfile(
"""\
import pytest
def test_foo():
pytest.exit("some exit msg", 99)
"""
)
result = testdir.runpytest()
result.stdout.fnmatch_lines(["*! *Exit: some exit msg !*"])
assert _strip_resource_warnings(result.stderr.lines) == []
assert result.ret == 99
# 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 _strip_resource_warnings(result.stderr.lines) == [
"Exit: during_sessionstart"
]
assert result.ret == 98
2019-12-03 20:48:22 +08:00
def test_pytest_fail_notrace_runtest(testdir) -> None:
"""Test pytest.fail(..., pytrace=False) does not show tracebacks during test run."""
2018-05-23 22:48:46 +08:00
testdir.makepyfile(
"""
import pytest
def test_hello():
pytest.fail("hello", pytrace=False)
def teardown_function(function):
pytest.fail("world", pytrace=False)
2018-05-23 22:48:46 +08:00
"""
)
result = testdir.runpytest()
2018-05-23 22:48:46 +08:00
result.stdout.fnmatch_lines(["world", "hello"])
result.stdout.no_fnmatch_line("*def teardown_function*")
2019-12-03 20:48:22 +08:00
def test_pytest_fail_notrace_collection(testdir) -> None:
"""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"])
result.stdout.no_fnmatch_line("*def some_internal_function()*")
2019-12-03 20:48:22 +08:00
def test_pytest_fail_notrace_non_ascii(testdir) -> None:
"""Fix pytest.fail with pytrace=False with non-ascii characters (#1178).
This tests with native and unicode strings containing non-ascii chars.
"""
2018-05-23 22:48:46 +08:00
testdir.makepyfile(
"""\
import pytest
def test_hello():
pytest.fail('oh oh: ☺', pytrace=False)
"""
2018-05-23 22:48:46 +08:00
)
result = testdir.runpytest()
result.stdout.fnmatch_lines(["*test_hello*", "oh oh: ☺"])
result.stdout.no_fnmatch_line("*def test_hello*")
2019-12-03 20:48:22 +08:00
def test_pytest_no_tests_collected_exit_status(testdir) -> None:
result = testdir.runpytest()
result.stdout.fnmatch_lines(["*collected 0 items*"])
assert result.ret == ExitCode.NO_TESTS_COLLECTED
2018-05-23 22:48:46 +08:00
testdir.makepyfile(
test_foo="""
def test_foo():
assert 1
2018-05-23 22:48:46 +08:00
"""
)
result = testdir.runpytest()
result.stdout.fnmatch_lines(["*collected 1 item*"])
result.stdout.fnmatch_lines(["*1 passed*"])
assert result.ret == ExitCode.OK
2018-05-23 22:48:46 +08:00
result = testdir.runpytest("-k nonmatch")
result.stdout.fnmatch_lines(["*collected 1 item*"])
result.stdout.fnmatch_lines(["*1 deselected*"])
assert result.ret == ExitCode.NO_TESTS_COLLECTED
2019-12-03 20:48:22 +08:00
def test_exception_printing_skip() -> None:
assert pytest.skip.Exception == pytest.skip.Exception
try:
pytest.skip("hello")
except pytest.skip.Exception:
excinfo = _pytest._code.ExceptionInfo.from_current()
s = excinfo.exconly(tryshort=True)
assert s.startswith("Skipped")
2019-12-03 20:48:22 +08:00
def test_importorskip(monkeypatch) -> None:
importorskip = pytest.importorskip
def f():
importorskip("asdlkj")
try:
2017-12-27 11:47:26 +08:00
sysmod = importorskip("sys")
assert sysmod is sys
# path = pytest.importorskip("os.path")
2017-12-27 11:47:26 +08:00
# assert path == os.path
excinfo = pytest.raises(pytest.skip.Exception, f)
2019-12-03 20:48:22 +08:00
assert excinfo is not None
excrepr = excinfo.getrepr()
assert excrepr is not None
assert excrepr.reprcrash is not None
path = py.path.local(excrepr.reprcrash.path)
# check that importorskip reports the actual call
# in this test the test_runner.py file
assert path.purebasename == "test_runner"
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")
2019-12-03 20:48:22 +08:00
mod.__version__ = "1.3" # type: ignore
monkeypatch.setitem(sys.modules, "hello123", mod)
with pytest.raises(pytest.skip.Exception):
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
except pytest.skip.Exception: # pragma: no cover
assert False, "spurious skip: {}".format(
_pytest._code.ExceptionInfo.from_current()
)
2019-12-03 20:48:22 +08:00
def test_importorskip_imports_last_module_part() -> None:
ospath = pytest.importorskip("os.path")
assert os.path == ospath
2019-12-03 20:48:22 +08:00
def test_importorskip_dev_module(monkeypatch) -> None:
try:
2017-12-27 11:47:26 +08:00
mod = types.ModuleType("mockmodule")
2019-12-03 20:48:22 +08:00
mod.__version__ = "0.13.0.dev-43290" # type: ignore
2018-05-23 22:48:46 +08:00
monkeypatch.setitem(sys.modules, "mockmodule", mod)
mod2 = pytest.importorskip("mockmodule", minversion="0.12.0")
assert mod2 == mod
with pytest.raises(pytest.skip.Exception):
pytest.importorskip("mockmodule1", minversion="0.14.0")
except pytest.skip.Exception: # pragma: no cover
assert False, "spurious skip: {}".format(
_pytest._code.ExceptionInfo.from_current()
)
2019-12-03 20:48:22 +08:00
def test_importorskip_module_level(testdir) -> None:
"""importorskip must be able to skip entire modules when used at module level"""
2018-05-23 22:48:46 +08:00
testdir.makepyfile(
"""
import pytest
foobarbaz = pytest.importorskip("foobarbaz")
def test_foo():
pass
2018-05-23 22:48:46 +08:00
"""
)
result = testdir.runpytest()
2018-05-23 22:48:46 +08:00
result.stdout.fnmatch_lines(["*collected 0 items / 1 skipped*"])
2019-12-03 20:48:22 +08:00
def test_importorskip_custom_reason(testdir) -> None:
"""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*"])
2019-12-03 20:48:22 +08:00
def test_pytest_cmdline_main(testdir) -> None:
2018-05-23 22:48:46 +08:00
p = testdir.makepyfile(
"""
import pytest
def test_hello():
assert 1
if __name__ == '__main__':
pytest.cmdline.main([__file__])
2018-05-23 22:48:46 +08:00
"""
)
import subprocess
2018-05-23 22:48:46 +08:00
popen = subprocess.Popen([sys.executable, str(p)], stdout=subprocess.PIPE)
popen.communicate()
ret = popen.wait()
assert ret == 0
2019-12-03 20:48:22 +08:00
def test_unicode_in_longrepr(testdir) -> None:
2018-05-23 22:48:46 +08:00
testdir.makeconftest(
"""\
import pytest
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport():
outcome = yield
rep = outcome.get_result()
if rep.when == "call":
rep.longrepr = 'ä'
"""
2018-05-23 22:48:46 +08:00
)
testdir.makepyfile(
"""
def test_out():
assert 0
2018-05-23 22:48:46 +08:00
"""
)
result = testdir.runpytest()
assert result.ret == 1
assert "UnicodeEncodeError" not in result.stderr.str()
2019-12-03 20:48:22 +08:00
def test_failure_in_setup(testdir) -> None:
2018-05-23 22:48:46 +08:00
testdir.makepyfile(
"""
def setup_module():
0/0
def test_func():
pass
2018-05-23 22:48:46 +08:00
"""
)
result = testdir.runpytest("--tb=line")
result.stdout.no_fnmatch_line("*def setup_module*")
2019-12-03 20:48:22 +08:00
def test_makereport_getsource(testdir) -> None:
2018-05-23 22:48:46 +08:00
testdir.makepyfile(
"""
def test_foo():
if False: pass
else: assert False
2018-05-23 22:48:46 +08:00
"""
)
result = testdir.runpytest()
result.stdout.no_fnmatch_line("*INTERNALERROR*")
2018-05-23 22:48:46 +08:00
result.stdout.fnmatch_lines(["*else: assert False*"])
2019-12-03 20:48:22 +08:00
def test_makereport_getsource_dynamic_code(testdir, monkeypatch) -> None:
"""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
2019-12-03 20:48:22 +08:00
def findsource(obj):
2016-02-26 20:14:55 +08:00
# 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()
2019-12-03 20:48:22 +08:00
return original_findsource(obj)
2018-05-23 22:48:46 +08:00
monkeypatch.setattr(inspect, "findsource", findsource)
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")
result.stdout.no_fnmatch_line("*INTERNALERROR*")
2016-02-26 20:14:55 +08:00
result.stdout.fnmatch_lines(["*test_fix*", "*fixture*'missing'*not found*"])
2019-12-03 20:48:22 +08:00
def test_store_except_info_on_error() -> None:
2015-03-22 00:26:23 +08:00
""" Test that upon test failure, the exception info is stored on
sys.last_traceback and friends.
"""
# 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"
raise_error = True
def runtest(self):
if self.raise_error:
2018-05-23 22:48:46 +08:00
raise IndexError("TEST")
try:
2020-05-01 19:40:15 +08:00
runner.pytest_runtest_call(ItemMightRaise()) # type: ignore[arg-type] # noqa: F821
except IndexError:
pass
# Check that exception info is stored on sys
assert sys.last_type is IndexError
2019-12-03 20:48:22 +08:00
assert isinstance(sys.last_value, IndexError)
2018-05-23 22:48:46 +08:00
assert sys.last_value.args[0] == "TEST"
assert sys.last_traceback
# The next run should clear the exception info stored by the previous run
ItemMightRaise.raise_error = False
2020-05-01 19:40:15 +08:00
runner.pytest_runtest_call(ItemMightRaise()) # type: ignore[arg-type] # noqa: F821
2019-11-21 19:50:40 +08:00
assert not hasattr(sys, "last_type")
assert not hasattr(sys, "last_value")
assert not hasattr(sys, "last_traceback")
2019-12-03 20:48:22 +08:00
def test_current_test_env_var(testdir, monkeypatch) -> None:
pytest_current_test_vars = [] # type: List[Tuple[str, str]]
2018-05-23 22:48:46 +08:00
monkeypatch.setattr(
sys, "pytest_current_test_vars", pytest_current_test_vars, raising=False
)
testdir.makepyfile(
"""
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
"""
)
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"
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
2019-06-03 06:32:00 +08:00
class TestReportContents:
"""
Test user-level API of ``TestReport`` objects.
"""
def getrunner(self):
return lambda item: runner.runtestprotocol(item, log=False)
2019-12-03 20:48:22 +08:00
def test_longreprtext_pass(self, testdir) -> None:
2018-05-23 22:48:46 +08:00
reports = testdir.runitem(
"""
def test_func():
pass
2018-05-23 22:48:46 +08:00
"""
)
rep = reports[1]
2018-05-23 22:48:46 +08:00
assert rep.longreprtext == ""
2019-12-03 20:48:22 +08:00
def test_longreprtext_failure(self, testdir) -> None:
2018-05-23 22:48:46 +08:00
reports = testdir.runitem(
"""
def test_func():
x = 1
assert x == 4
2018-05-23 22:48:46 +08:00
"""
)
rep = reports[1]
2018-05-23 22:48:46 +08:00
assert "assert 1 == 4" in rep.longreprtext
2019-12-03 20:48:22 +08:00
def test_captured_text(self, testdir) -> None:
2018-05-23 22:48:46 +08:00
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
2018-05-23 22:48:46 +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"
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"
2019-12-03 20:48:22 +08:00
def test_no_captured_text(self, testdir) -> None:
2018-05-23 22:48:46 +08:00
reports = testdir.runitem(
"""
def test_func():
pass
2018-05-23 22:48:46 +08:00
"""
)
rep = reports[1]
2018-05-23 22:48:46 +08:00
assert rep.capstdout == ""
assert rep.capstderr == ""
def test_longrepr_type(self, testdir) -> None:
reports = testdir.runitem(
"""
import pytest
def test_func():
pytest.fail(pytrace=False)
"""
)
rep = reports[1]
assert isinstance(rep.longrepr, _pytest._code.code.ExceptionRepr)
2019-12-03 20:48:22 +08:00
def test_outcome_exception_bad_msg() -> None:
"""Check that OutcomeExceptions validate their input to prevent confusing errors (#5578)"""
2019-12-03 20:48:22 +08:00
def func() -> None:
raise NotImplementedError()
expected = (
"OutcomeException expected string as 'msg' parameter, got 'function' instead.\n"
"Perhaps you meant to use a mark?"
)
with pytest.raises(TypeError) as excinfo:
2019-12-03 20:48:22 +08:00
OutcomeException(func) # type: ignore
assert str(excinfo.value) == expected