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
|
2020-12-15 19:02:32 +08:00
|
|
|
from pathlib import Path
|
2019-12-03 20:48:22 +08:00
|
|
|
from typing import Dict
|
|
|
|
from typing import List
|
|
|
|
from typing import Tuple
|
2020-10-03 04:02:22 +08:00
|
|
|
from typing import Type
|
2018-10-25 15:01:29 +08:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
from _pytest import outcomes
|
|
|
|
from _pytest import reports
|
|
|
|
from _pytest import runner
|
2020-12-15 19:02:32 +08:00
|
|
|
from _pytest._code import ExceptionInfo
|
|
|
|
from _pytest._code.code import ExceptionChainRepr
|
2020-02-11 05:43:30 +08:00
|
|
|
from _pytest.config import ExitCode
|
2020-12-15 19:02:32 +08:00
|
|
|
from _pytest.monkeypatch import MonkeyPatch
|
2019-07-09 07:33:43 +08:00
|
|
|
from _pytest.outcomes import OutcomeException
|
2020-12-15 19:02:32 +08:00
|
|
|
from _pytest.pytester import Pytester
|
2019-12-03 20:48:22 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestSetupState:
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_setup(self, pytester: Pytester) -> None:
|
|
|
|
item = pytester.getitem("def test_func(): pass")
|
2021-01-01 21:53:38 +08:00
|
|
|
ss = item.session._setupstate
|
2017-11-04 23:17:20 +08:00
|
|
|
values = [1]
|
2021-01-31 18:14:06 +08:00
|
|
|
ss.setup(item)
|
2021-01-02 05:14:04 +08:00
|
|
|
ss.addfinalizer(values.pop, item)
|
2017-11-04 23:17:20 +08:00
|
|
|
assert values
|
2021-01-01 18:51:33 +08:00
|
|
|
ss.teardown_exact(None)
|
2017-11-04 23:17:20 +08:00
|
|
|
assert not values
|
2009-05-21 20:37:30 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_teardown_exact_stack_empty(self, pytester: Pytester) -> None:
|
|
|
|
item = pytester.getitem("def test_func(): pass")
|
2021-01-01 21:53:38 +08:00
|
|
|
ss = item.session._setupstate
|
2021-01-31 18:14:06 +08:00
|
|
|
ss.setup(item)
|
2021-01-01 18:08:12 +08:00
|
|
|
ss.teardown_exact(None)
|
|
|
|
ss.teardown_exact(None)
|
|
|
|
ss.teardown_exact(None)
|
2009-05-23 01:56:05 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_setup_fails_and_failure_is_cached(self, pytester: Pytester) -> None:
|
|
|
|
item = pytester.getitem(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
"""
|
|
|
|
)
|
2021-01-01 21:53:38 +08:00
|
|
|
ss = item.session._setupstate
|
|
|
|
with pytest.raises(ValueError):
|
2021-01-31 18:14:06 +08:00
|
|
|
ss.setup(item)
|
2021-01-01 21:53:38 +08:00
|
|
|
with pytest.raises(ValueError):
|
2021-01-31 18:14:06 +08:00
|
|
|
ss.setup(item)
|
2013-10-12 21:39:22 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_teardown_multiple_one_fails(self, pytester: Pytester) -> None:
|
2013-11-20 01:26:18 +08:00
|
|
|
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
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
item = pytester.getitem("def test_func(): pass")
|
2021-01-01 21:53:38 +08:00
|
|
|
ss = item.session._setupstate
|
2021-01-31 18:14:06 +08:00
|
|
|
ss.setup(item)
|
2013-11-20 01:26:18 +08:00
|
|
|
ss.addfinalizer(fin1, item)
|
|
|
|
ss.addfinalizer(fin2, item)
|
|
|
|
ss.addfinalizer(fin3, item)
|
|
|
|
with pytest.raises(Exception) as err:
|
2021-01-01 18:08:12 +08:00
|
|
|
ss.teardown_exact(None)
|
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
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_teardown_multiple_fail(self, pytester: Pytester) -> None:
|
2013-11-21 09:15:24 +08:00
|
|
|
# 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
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
item = pytester.getitem("def test_func(): pass")
|
2021-01-01 21:53:38 +08:00
|
|
|
ss = item.session._setupstate
|
2021-01-31 18:14:06 +08:00
|
|
|
ss.setup(item)
|
2013-11-21 09:15:24 +08:00
|
|
|
ss.addfinalizer(fin1, item)
|
|
|
|
ss.addfinalizer(fin2, item)
|
|
|
|
with pytest.raises(Exception) as err:
|
2021-01-01 18:08:12 +08:00
|
|
|
ss.teardown_exact(None)
|
2018-05-23 22:48:46 +08:00
|
|
|
assert err.value.args == ("oops2",)
|
2013-11-21 09:15:24 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_teardown_multiple_scopes_one_fails(self, pytester: Pytester) -> None:
|
2018-06-12 04:12:47 +08:00
|
|
|
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")
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
item = pytester.getitem("def test_func(): pass")
|
2021-01-01 22:05:54 +08:00
|
|
|
mod = item.listchain()[-2]
|
2021-01-01 21:53:38 +08:00
|
|
|
ss = item.session._setupstate
|
2021-01-31 18:14:06 +08:00
|
|
|
ss.setup(item)
|
2021-01-01 22:05:54 +08:00
|
|
|
ss.addfinalizer(fin_module, mod)
|
|
|
|
ss.addfinalizer(fin_func, item)
|
2018-06-12 07:33:13 +08:00
|
|
|
with pytest.raises(Exception, match="oops1"):
|
2021-01-01 18:08:12 +08:00
|
|
|
ss.teardown_exact(None)
|
2021-01-01 22:05:54 +08:00
|
|
|
assert module_teardown == ["fin_module"]
|
2018-06-12 04:12:47 +08:00
|
|
|
|
2010-01-28 21:20:58 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class BaseFunctionalTests:
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_passfunction(self, pytester: Pytester) -> None:
|
|
|
|
reports = pytester.runitem(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_failfunction(self, pytester: Pytester) -> None:
|
|
|
|
reports = pytester.runitem(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_skipfunction(self, pytester: Pytester) -> None:
|
|
|
|
reports = pytester.runitem(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_skip_in_setup_function(self, pytester: Pytester) -> None:
|
|
|
|
reports = pytester.runitem(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_failure_in_setup_function(self, pytester: Pytester) -> None:
|
|
|
|
reports = pytester.runitem(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_failure_in_teardown_function(self, pytester: Pytester) -> None:
|
|
|
|
reports = pytester.runitem(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_custom_failure_repr(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
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
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
reports = pytester.runitem(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_teardown_final_returncode(self, pytester: Pytester) -> None:
|
|
|
|
rec = pytester.inline_runsource(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_logstart_logfinish_hooks(self, pytester: Pytester) -> None:
|
|
|
|
rec = pytester.inline_runsource(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_exact_teardown_issue90(self, pytester: Pytester) -> None:
|
|
|
|
rec = pytester.inline_runsource(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_exact_teardown_issue1206(self, pytester: Pytester) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""Issue shadowing error with wrong number of arguments on teardown_method."""
|
2020-12-15 19:02:32 +08:00
|
|
|
rec = pytester.inline_runsource(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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"
|
2020-12-15 19:02:32 +08:00
|
|
|
longrepr = reps[2].longrepr
|
|
|
|
assert isinstance(longrepr, ExceptionChainRepr)
|
|
|
|
assert longrepr.reprcrash
|
|
|
|
assert longrepr.reprcrash.message in (
|
2017-07-17 07:25:07 +08:00
|
|
|
"TypeError: teardown_method() missing 2 required positional arguments: 'y' and 'z'",
|
2020-10-25 07:08:12 +08:00
|
|
|
# Python >= 3.10
|
|
|
|
"TypeError: TestClass.teardown_method() missing 2 required positional arguments: 'y' and 'z'",
|
2017-07-17 07:25:07 +08:00
|
|
|
)
|
2016-06-12 00:04:49 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_failure_in_setup_function_ignores_custom_repr(
|
|
|
|
self, pytester: Pytester
|
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
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
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
reports = pytester.runitem(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_systemexit_does_not_bail_out(self, pytester: Pytester) -> None:
|
2009-05-23 01:56:05 +08:00
|
|
|
try:
|
2020-12-15 19:02:32 +08:00
|
|
|
reports = pytester.runitem(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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:
|
2020-02-21 21:37:56 +08:00
|
|
|
assert False, "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
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_exit_propagates(self, pytester: Pytester) -> None:
|
2009-05-23 01:56:05 +08:00
|
|
|
try:
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.runitem(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
"""
|
|
|
|
)
|
Use a hack to make typing of pytest.fail.Exception & co work
Mypy currently is unable to handle assigning attributes on function:
https://github.com/python/mypy/issues/2087.
pytest uses this for the outcome exceptions -- `pytest.fail.Exception`,
`pytest.exit.Exception` etc, and this is the canonical name by which they
are referred.
Initially we started working around this with type: ignores, and later
by switching e.g. `pytest.fail.Exception` with the direct exception
`Failed`. But this causes a lot of churn and is not as nice. And I also
found that some code relies on it, in skipping.py:
def pytest_configure(config):
if config.option.runxfail:
# yay a hack
import pytest
old = pytest.xfail
config._cleanup.append(lambda: setattr(pytest, "xfail", old))
def nop(*args, **kwargs):
pass
nop.Exception = xfail.Exception
setattr(pytest, "xfail", nop)
...
So it seems better to support it. Use a hack to make it work. The rest
of the commit rolls back all of the workarounds we added up to now.
`pytest.raises.Exception` also exists, but it's not used much so I kept
it as-is for now.
Hopefully in the future mypy supports this and this ugliness can be
removed.
2020-02-17 03:46:11 +08:00
|
|
|
except pytest.exit.Exception:
|
2009-05-23 01:56:05 +08:00
|
|
|
pass
|
2010-07-27 03:15:15 +08:00
|
|
|
else:
|
2020-02-21 21:37:56 +08:00
|
|
|
assert False, "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
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_keyboardinterrupt_propagates(self, pytester: Pytester) -> None:
|
2009-05-23 01:56:05 +08:00
|
|
|
try:
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.runitem(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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:
|
2020-02-21 21:37:56 +08:00
|
|
|
assert False, "did not raise"
|
2009-05-23 01:56:05 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestSessionReports:
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_collect_result(self, pytester: Pytester) -> None:
|
|
|
|
col = pytester.getmodulecol(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
|
2020-10-06 09:13:05 +08:00
|
|
|
reporttypes: List[Type[reports.BaseReport]] = [
|
2019-12-03 20:48:22 +08:00
|
|
|
reports.BaseReport,
|
|
|
|
reports.TestReport,
|
|
|
|
reports.CollectReport,
|
2020-10-06 09:13:05 +08:00
|
|
|
]
|
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]
|
|
|
|
)
|
2020-10-03 04:02:22 +08:00
|
|
|
def test_report_extra_parameters(reporttype: Type[reports.BaseReport]) -> None:
|
2019-12-15 02:52:17 +08:00
|
|
|
args = list(inspect.signature(reporttype.__init__).parameters.keys())[1:]
|
2020-10-06 09:13:05 +08:00
|
|
|
basekw: Dict[str, List[object]] = dict.fromkeys(args, [])
|
2012-01-21 02:50:45 +08:00
|
|
|
report = reporttype(newthing=1, **basekw)
|
|
|
|
assert report.newthing == 1
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2019-12-03 20:48:22 +08:00
|
|
|
def test_callinfo() -> None:
|
2020-05-01 19:40:16 +08:00
|
|
|
ci = runner.CallInfo.from_call(lambda: 0, "collect")
|
|
|
|
assert ci.when == "collect"
|
2009-08-06 20:34:19 +08:00
|
|
|
assert ci.result == 0
|
2010-07-27 03:15:15 +08:00
|
|
|
assert "result" in repr(ci)
|
2020-05-01 19:40:16 +08:00
|
|
|
assert repr(ci) == "<CallInfo when='collect' result: 0>"
|
|
|
|
assert str(ci) == "<CallInfo when='collect' result: 0>"
|
2018-11-13 15:48:07 +08:00
|
|
|
|
2020-05-01 19:40:16 +08:00
|
|
|
ci2 = runner.CallInfo.from_call(lambda: 0 / 0, "collect")
|
|
|
|
assert ci2.when == "collect"
|
|
|
|
assert not hasattr(ci2, "result")
|
2020-10-03 04:16:22 +08:00
|
|
|
assert repr(ci2) == f"<CallInfo when='collect' excinfo={ci2.excinfo!r}>"
|
2020-05-01 19:40:16 +08:00
|
|
|
assert str(ci2) == repr(ci2)
|
|
|
|
assert ci2.excinfo
|
2019-10-19 17:05:12 +08:00
|
|
|
|
|
|
|
# Newlines are escaped.
|
|
|
|
def raise_assertion():
|
|
|
|
assert 0, "assert_msg"
|
|
|
|
|
2020-05-01 19:40:16 +08:00
|
|
|
ci3 = runner.CallInfo.from_call(raise_assertion, "call")
|
2020-10-03 04:16:22 +08:00
|
|
|
assert repr(ci3) == f"<CallInfo when='call' excinfo={ci3.excinfo!r}>"
|
2020-05-01 19:40:16 +08:00
|
|
|
assert "\n" not in repr(ci3)
|
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
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_runtest_in_module_ordering(pytester: Pytester) -> None:
|
|
|
|
p1 = pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
2020-06-04 02:51:55 +08:00
|
|
|
@pytest.hookimpl(hookwrapper=True)
|
|
|
|
def pytest_runtest_call(self, item):
|
2009-12-30 17:42:01 +08:00
|
|
|
try:
|
2020-06-04 02:51:55 +08:00
|
|
|
(yield).get_result()
|
2009-12-30 17:42:01 +08:00
|
|
|
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
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.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
|
|
|
|
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
|
2013-01-27 09:06:19 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2019-12-03 20:48:22 +08:00
|
|
|
def test_outcomeexception_passes_except_Exception() -> None:
|
2015-09-19 07:03:05 +08:00
|
|
|
with pytest.raises(outcomes.OutcomeException):
|
|
|
|
try:
|
2018-05-23 22:48:46 +08:00
|
|
|
raise outcomes.OutcomeException("test")
|
2020-06-19 17:53:44 +08:00
|
|
|
except Exception as e:
|
|
|
|
raise NotImplementedError from e
|
2015-09-19 07:03:05 +08:00
|
|
|
|
|
|
|
|
2019-12-03 20:48:22 +08:00
|
|
|
def test_pytest_exit() -> None:
|
Use a hack to make typing of pytest.fail.Exception & co work
Mypy currently is unable to handle assigning attributes on function:
https://github.com/python/mypy/issues/2087.
pytest uses this for the outcome exceptions -- `pytest.fail.Exception`,
`pytest.exit.Exception` etc, and this is the canonical name by which they
are referred.
Initially we started working around this with type: ignores, and later
by switching e.g. `pytest.fail.Exception` with the direct exception
`Failed`. But this causes a lot of churn and is not as nice. And I also
found that some code relies on it, in skipping.py:
def pytest_configure(config):
if config.option.runxfail:
# yay a hack
import pytest
old = pytest.xfail
config._cleanup.append(lambda: setattr(pytest, "xfail", old))
def nop(*args, **kwargs):
pass
nop.Exception = xfail.Exception
setattr(pytest, "xfail", nop)
...
So it seems better to support it. Use a hack to make it work. The rest
of the commit rolls back all of the workarounds we added up to now.
`pytest.raises.Exception` also exists, but it's not used much so I kept
it as-is for now.
Hopefully in the future mypy supports this and this ugliness can be
removed.
2020-02-17 03:46:11 +08:00
|
|
|
with pytest.raises(pytest.exit.Exception) as excinfo:
|
2014-01-18 19:31:33 +08:00
|
|
|
pytest.exit("hello")
|
Use a hack to make typing of pytest.fail.Exception & co work
Mypy currently is unable to handle assigning attributes on function:
https://github.com/python/mypy/issues/2087.
pytest uses this for the outcome exceptions -- `pytest.fail.Exception`,
`pytest.exit.Exception` etc, and this is the canonical name by which they
are referred.
Initially we started working around this with type: ignores, and later
by switching e.g. `pytest.fail.Exception` with the direct exception
`Failed`. But this causes a lot of churn and is not as nice. And I also
found that some code relies on it, in skipping.py:
def pytest_configure(config):
if config.option.runxfail:
# yay a hack
import pytest
old = pytest.xfail
config._cleanup.append(lambda: setattr(pytest, "xfail", old))
def nop(*args, **kwargs):
pass
nop.Exception = xfail.Exception
setattr(pytest, "xfail", nop)
...
So it seems better to support it. Use a hack to make it work. The rest
of the commit rolls back all of the workarounds we added up to now.
`pytest.raises.Exception` also exists, but it's not used much so I kept
it as-is for now.
Hopefully in the future mypy supports this and this ugliness can be
removed.
2020-02-17 03:46:11 +08:00
|
|
|
assert excinfo.errisinstance(pytest.exit.Exception)
|
2010-04-28 14:42:56 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2019-12-03 20:48:22 +08:00
|
|
|
def test_pytest_fail() -> None:
|
Use a hack to make typing of pytest.fail.Exception & co work
Mypy currently is unable to handle assigning attributes on function:
https://github.com/python/mypy/issues/2087.
pytest uses this for the outcome exceptions -- `pytest.fail.Exception`,
`pytest.exit.Exception` etc, and this is the canonical name by which they
are referred.
Initially we started working around this with type: ignores, and later
by switching e.g. `pytest.fail.Exception` with the direct exception
`Failed`. But this causes a lot of churn and is not as nice. And I also
found that some code relies on it, in skipping.py:
def pytest_configure(config):
if config.option.runxfail:
# yay a hack
import pytest
old = pytest.xfail
config._cleanup.append(lambda: setattr(pytest, "xfail", old))
def nop(*args, **kwargs):
pass
nop.Exception = xfail.Exception
setattr(pytest, "xfail", nop)
...
So it seems better to support it. Use a hack to make it work. The rest
of the commit rolls back all of the workarounds we added up to now.
`pytest.raises.Exception` also exists, but it's not used much so I kept
it as-is for now.
Hopefully in the future mypy supports this and this ugliness can be
removed.
2020-02-17 03:46:11 +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
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_pytest_exit_msg(pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.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
|
|
|
|
2019-10-07 10:11:33 +08:00
|
|
|
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"))
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_pytest_exit_returncode(pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2019-10-07 10:11:33 +08:00
|
|
|
"""\
|
2018-10-15 03:41:16 +08:00
|
|
|
import pytest
|
|
|
|
def test_foo():
|
|
|
|
pytest.exit("some exit msg", 99)
|
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest()
|
2019-04-03 10:07:42 +08:00
|
|
|
result.stdout.fnmatch_lines(["*! *Exit: some exit msg !*"])
|
2019-10-07 10:11:33 +08:00
|
|
|
|
2019-10-23 06:00:15 +08:00
|
|
|
assert _strip_resource_warnings(result.stderr.lines) == []
|
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.
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makeconftest(
|
2019-10-07 10:11:33 +08:00
|
|
|
"""\
|
2019-04-03 10:07:42 +08:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
def pytest_sessionstart():
|
|
|
|
pytest.exit("during_sessionstart", 98)
|
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest()
|
2019-04-03 10:07:42 +08:00
|
|
|
result.stdout.fnmatch_lines(["*! *Exit: during_sessionstart !*"])
|
2019-10-07 10:11:33 +08:00
|
|
|
assert _strip_resource_warnings(result.stderr.lines) == [
|
2019-10-23 06:00:15 +08:00
|
|
|
"Exit: during_sessionstart"
|
2019-10-07 10:11:33 +08:00
|
|
|
]
|
2019-04-03 10:07:42 +08:00
|
|
|
assert result.ret == 98
|
|
|
|
|
2018-10-14 23:20:10 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_pytest_fail_notrace_runtest(pytester: Pytester) -> None:
|
2018-10-04 07:07:59 +08:00
|
|
|
"""Test pytest.fail(..., pytrace=False) does not show tracebacks during test run."""
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["world", "hello"])
|
2019-10-06 01:18:51 +08:00
|
|
|
result.stdout.no_fnmatch_line("*def teardown_function*")
|
2010-11-23 22:42:23 +08:00
|
|
|
|
2015-07-05 01:42:22 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_pytest_fail_notrace_collection(pytester: Pytester) -> None:
|
2018-10-04 07:07:59 +08:00
|
|
|
"""Test pytest.fail(..., pytrace=False) does not show tracebacks during collection."""
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile(
|
2018-10-04 07:07:59 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
def some_internal_function():
|
|
|
|
pytest.fail("hello", pytrace=False)
|
|
|
|
some_internal_function()
|
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest()
|
2018-10-04 07:07:59 +08:00
|
|
|
result.stdout.fnmatch_lines(["hello"])
|
2019-10-06 01:18:51 +08:00
|
|
|
result.stdout.no_fnmatch_line("*def some_internal_function()*")
|
2018-10-04 07:07:59 +08:00
|
|
|
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_pytest_fail_notrace_non_ascii(pytester: Pytester) -> None:
|
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
|
|
|
"""
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.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
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest()
|
2019-05-28 07:31:52 +08:00
|
|
|
result.stdout.fnmatch_lines(["*test_hello*", "oh oh: ☺"])
|
2019-10-06 01:18:51 +08:00
|
|
|
result.stdout.no_fnmatch_line("*def test_hello*")
|
2016-03-06 03:09:01 +08:00
|
|
|
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_pytest_no_tests_collected_exit_status(pytester: Pytester) -> None:
|
|
|
|
result = pytester.runpytest()
|
2019-03-23 18:36:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["*collected 0 items*"])
|
2020-02-11 05:43:30 +08:00
|
|
|
assert result.ret == ExitCode.NO_TESTS_COLLECTED
|
2015-07-05 01:42:22 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
test_foo="""
|
2015-07-05 01:42:22 +08:00
|
|
|
def test_foo():
|
|
|
|
assert 1
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest()
|
2019-03-23 18:36:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["*collected 1 item*"])
|
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
2020-02-11 05:43:30 +08:00
|
|
|
assert result.ret == ExitCode.OK
|
2015-07-05 01:42:22 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest("-k nonmatch")
|
2019-03-23 18:36:18 +08:00
|
|
|
result.stdout.fnmatch_lines(["*collected 1 item*"])
|
|
|
|
result.stdout.fnmatch_lines(["*1 deselected*"])
|
2020-02-11 05:43:30 +08:00
|
|
|
assert result.ret == ExitCode.NO_TESTS_COLLECTED
|
2015-07-05 01:42:22 +08:00
|
|
|
|
|
|
|
|
2019-12-03 20:48:22 +08:00
|
|
|
def test_exception_printing_skip() -> None:
|
Use a hack to make typing of pytest.fail.Exception & co work
Mypy currently is unable to handle assigning attributes on function:
https://github.com/python/mypy/issues/2087.
pytest uses this for the outcome exceptions -- `pytest.fail.Exception`,
`pytest.exit.Exception` etc, and this is the canonical name by which they
are referred.
Initially we started working around this with type: ignores, and later
by switching e.g. `pytest.fail.Exception` with the direct exception
`Failed`. But this causes a lot of churn and is not as nice. And I also
found that some code relies on it, in skipping.py:
def pytest_configure(config):
if config.option.runxfail:
# yay a hack
import pytest
old = pytest.xfail
config._cleanup.append(lambda: setattr(pytest, "xfail", old))
def nop(*args, **kwargs):
pass
nop.Exception = xfail.Exception
setattr(pytest, "xfail", nop)
...
So it seems better to support it. Use a hack to make it work. The rest
of the commit rolls back all of the workarounds we added up to now.
`pytest.raises.Exception` also exists, but it's not used much so I kept
it as-is for now.
Hopefully in the future mypy supports this and this ugliness can be
removed.
2020-02-17 03:46:11 +08:00
|
|
|
assert pytest.skip.Exception == pytest.skip.Exception
|
2010-04-28 14:42:56 +08:00
|
|
|
try:
|
2010-11-18 05:12:16 +08:00
|
|
|
pytest.skip("hello")
|
Use a hack to make typing of pytest.fail.Exception & co work
Mypy currently is unable to handle assigning attributes on function:
https://github.com/python/mypy/issues/2087.
pytest uses this for the outcome exceptions -- `pytest.fail.Exception`,
`pytest.exit.Exception` etc, and this is the canonical name by which they
are referred.
Initially we started working around this with type: ignores, and later
by switching e.g. `pytest.fail.Exception` with the direct exception
`Failed`. But this causes a lot of churn and is not as nice. And I also
found that some code relies on it, in skipping.py:
def pytest_configure(config):
if config.option.runxfail:
# yay a hack
import pytest
old = pytest.xfail
config._cleanup.append(lambda: setattr(pytest, "xfail", old))
def nop(*args, **kwargs):
pass
nop.Exception = xfail.Exception
setattr(pytest, "xfail", nop)
...
So it seems better to support it. Use a hack to make it work. The rest
of the commit rolls back all of the workarounds we added up to now.
`pytest.raises.Exception` also exists, but it's not used much so I kept
it as-is for now.
Hopefully in the future mypy supports this and this ugliness can be
removed.
2020-02-17 03:46:11 +08:00
|
|
|
except pytest.skip.Exception:
|
2020-12-15 19:02:32 +08:00
|
|
|
excinfo = 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
|
|
|
|
2019-12-03 20:48:22 +08:00
|
|
|
def test_importorskip(monkeypatch) -> None:
|
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
|
Use a hack to make typing of pytest.fail.Exception & co work
Mypy currently is unable to handle assigning attributes on function:
https://github.com/python/mypy/issues/2087.
pytest uses this for the outcome exceptions -- `pytest.fail.Exception`,
`pytest.exit.Exception` etc, and this is the canonical name by which they
are referred.
Initially we started working around this with type: ignores, and later
by switching e.g. `pytest.fail.Exception` with the direct exception
`Failed`. But this causes a lot of churn and is not as nice. And I also
found that some code relies on it, in skipping.py:
def pytest_configure(config):
if config.option.runxfail:
# yay a hack
import pytest
old = pytest.xfail
config._cleanup.append(lambda: setattr(pytest, "xfail", old))
def nop(*args, **kwargs):
pass
nop.Exception = xfail.Exception
setattr(pytest, "xfail", nop)
...
So it seems better to support it. Use a hack to make it work. The rest
of the commit rolls back all of the workarounds we added up to now.
`pytest.raises.Exception` also exists, but it's not used much so I kept
it as-is for now.
Hopefully in the future mypy supports this and this ugliness can be
removed.
2020-02-17 03:46:11 +08:00
|
|
|
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
|
2020-12-15 19:02:32 +08:00
|
|
|
path = Path(excrepr.reprcrash.path)
|
2010-11-14 04:03:28 +08:00
|
|
|
# check that importorskip reports the actual call
|
|
|
|
# in this test the test_runner.py file
|
2020-12-15 19:02:32 +08:00
|
|
|
assert path.stem == "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")
|
2019-12-03 20:48:22 +08:00
|
|
|
mod.__version__ = "1.3" # type: ignore
|
2015-07-24 13:12:01 +08:00
|
|
|
monkeypatch.setitem(sys.modules, "hello123", mod)
|
Use a hack to make typing of pytest.fail.Exception & co work
Mypy currently is unable to handle assigning attributes on function:
https://github.com/python/mypy/issues/2087.
pytest uses this for the outcome exceptions -- `pytest.fail.Exception`,
`pytest.exit.Exception` etc, and this is the canonical name by which they
are referred.
Initially we started working around this with type: ignores, and later
by switching e.g. `pytest.fail.Exception` with the direct exception
`Failed`. But this causes a lot of churn and is not as nice. And I also
found that some code relies on it, in skipping.py:
def pytest_configure(config):
if config.option.runxfail:
# yay a hack
import pytest
old = pytest.xfail
config._cleanup.append(lambda: setattr(pytest, "xfail", old))
def nop(*args, **kwargs):
pass
nop.Exception = xfail.Exception
setattr(pytest, "xfail", nop)
...
So it seems better to support it. Use a hack to make it work. The rest
of the commit rolls back all of the workarounds we added up to now.
`pytest.raises.Exception` also exists, but it's not used much so I kept
it as-is for now.
Hopefully in the future mypy supports this and this ugliness can be
removed.
2020-02-17 03:46:11 +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
|
2020-02-21 21:37:56 +08:00
|
|
|
except pytest.skip.Exception: # pragma: no cover
|
2020-12-15 19:02:32 +08:00
|
|
|
assert False, f"spurious skip: {ExceptionInfo.from_current()}"
|
2010-04-28 14:42:56 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2019-12-03 20:48:22 +08:00
|
|
|
def test_importorskip_imports_last_module_part() -> None:
|
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
|
|
|
|
2019-12-03 20:48:22 +08:00
|
|
|
def test_importorskip_dev_module(monkeypatch) -> None:
|
2015-07-24 04:38:25 +08:00
|
|
|
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")
|
2015-07-24 04:38:25 +08:00
|
|
|
assert mod2 == mod
|
Use a hack to make typing of pytest.fail.Exception & co work
Mypy currently is unable to handle assigning attributes on function:
https://github.com/python/mypy/issues/2087.
pytest uses this for the outcome exceptions -- `pytest.fail.Exception`,
`pytest.exit.Exception` etc, and this is the canonical name by which they
are referred.
Initially we started working around this with type: ignores, and later
by switching e.g. `pytest.fail.Exception` with the direct exception
`Failed`. But this causes a lot of churn and is not as nice. And I also
found that some code relies on it, in skipping.py:
def pytest_configure(config):
if config.option.runxfail:
# yay a hack
import pytest
old = pytest.xfail
config._cleanup.append(lambda: setattr(pytest, "xfail", old))
def nop(*args, **kwargs):
pass
nop.Exception = xfail.Exception
setattr(pytest, "xfail", nop)
...
So it seems better to support it. Use a hack to make it work. The rest
of the commit rolls back all of the workarounds we added up to now.
`pytest.raises.Exception` also exists, but it's not used much so I kept
it as-is for now.
Hopefully in the future mypy supports this and this ugliness can be
removed.
2020-02-17 03:46:11 +08:00
|
|
|
with pytest.raises(pytest.skip.Exception):
|
2018-11-23 02:05:10 +08:00
|
|
|
pytest.importorskip("mockmodule1", minversion="0.14.0")
|
2020-02-21 21:37:56 +08:00
|
|
|
except pytest.skip.Exception: # pragma: no cover
|
2020-12-15 19:02:32 +08:00
|
|
|
assert False, f"spurious skip: {ExceptionInfo.from_current()}"
|
2015-07-24 04:38:25 +08:00
|
|
|
|
2010-04-28 14:42:56 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_importorskip_module_level(pytester: Pytester) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""`importorskip` must be able to skip entire modules when used at module level."""
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.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
|
|
|
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_importorskip_custom_reason(pytester: Pytester) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""Make sure custom reasons are used."""
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile(
|
2019-01-05 03:02:07 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
foobarbaz = pytest.importorskip("foobarbaz2", reason="just because")
|
|
|
|
|
|
|
|
def test_foo():
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest("-ra")
|
2019-01-05 03:02:07 +08:00
|
|
|
result.stdout.fnmatch_lines(["*just because*"])
|
|
|
|
result.stdout.fnmatch_lines(["*collected 0 items / 1 skipped*"])
|
|
|
|
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_pytest_cmdline_main(pytester: Pytester) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_unicode_in_longrepr(pytester: Pytester) -> None:
|
|
|
|
pytester.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
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2012-06-24 22:42:31 +08:00
|
|
|
def test_out():
|
|
|
|
assert 0
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest()
|
2012-06-24 22:42:31 +08:00
|
|
|
assert result.ret == 1
|
|
|
|
assert "UnicodeEncodeError" not in result.stderr.str()
|
|
|
|
|
2013-09-05 21:43:19 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_failure_in_setup(pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest("--tb=line")
|
2019-10-06 01:18:51 +08:00
|
|
|
result.stdout.no_fnmatch_line("*def setup_module*")
|
2014-08-15 06:23:04 +08:00
|
|
|
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_makereport_getsource(pytester: Pytester) -> None:
|
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest()
|
2019-10-06 01:18:51 +08:00
|
|
|
result.stdout.no_fnmatch_line("*INTERNALERROR*")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(["*else: assert False*"])
|
2015-03-22 00:06:24 +08:00
|
|
|
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_makereport_getsource_dynamic_code(
|
|
|
|
pytester: Pytester, monkeypatch: MonkeyPatch
|
|
|
|
) -> None:
|
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
|
|
|
|
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)
|
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
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest("-vv")
|
2019-10-06 01:18:51 +08:00
|
|
|
result.stdout.no_fnmatch_line("*INTERNALERROR*")
|
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
|
|
|
|
|
|
|
|
2019-12-03 20:48:22 +08:00
|
|
|
def test_store_except_info_on_error() -> None:
|
2020-07-18 17:35:13 +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:
|
2020-07-10 14:44:14 +08:00
|
|
|
runner.pytest_runtest_call(ItemMightRaise()) # type: ignore[arg-type]
|
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
|
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"
|
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
|
2020-07-10 14:44:14 +08:00
|
|
|
runner.pytest_runtest_call(ItemMightRaise()) # type: ignore[arg-type]
|
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")
|
2018-04-05 07:36:07 +08:00
|
|
|
|
2016-08-04 08:11:19 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_current_test_env_var(pytester: Pytester, monkeypatch: MonkeyPatch) -> None:
|
2020-10-06 09:13:05 +08:00
|
|
|
pytest_current_test_vars: 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
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest_inprocess()
|
2017-07-19 04:18:34 +08:00
|
|
|
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:
|
2020-07-18 17:35:13 +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)
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_longreprtext_pass(self, pytester: Pytester) -> None:
|
|
|
|
reports = pytester.runitem(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_longreprtext_skip(self, pytester: Pytester) -> None:
|
2020-07-29 08:02:23 +08:00
|
|
|
"""TestReport.longreprtext can handle non-str ``longrepr`` attributes (#7559)"""
|
2020-12-15 19:02:32 +08:00
|
|
|
reports = pytester.runitem(
|
2020-07-29 08:02:23 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
def test_func():
|
|
|
|
pytest.skip()
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
_, call_rep, _ = reports
|
|
|
|
assert isinstance(call_rep.longrepr, tuple)
|
|
|
|
assert "Skipped" in call_rep.longreprtext
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_longreprtext_collect_skip(self, pytester: Pytester) -> None:
|
2020-07-29 08:02:23 +08:00
|
|
|
"""CollectReport.longreprtext can handle non-str ``longrepr`` attributes (#7559)"""
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile(
|
2020-07-29 08:02:23 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
pytest.skip(allow_module_level=True)
|
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
rec = pytester.inline_run()
|
2020-07-29 08:02:23 +08:00
|
|
|
calls = rec.getcalls("pytest_collectreport")
|
|
|
|
_, call = calls
|
|
|
|
assert isinstance(call.report.longrepr, tuple)
|
|
|
|
assert "Skipped" in call.report.longreprtext
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_longreprtext_failure(self, pytester: Pytester) -> None:
|
|
|
|
reports = pytester.runitem(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_captured_text(self, pytester: Pytester) -> None:
|
|
|
|
reports = pytester.runitem(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_no_captured_text(self, pytester: Pytester) -> None:
|
|
|
|
reports = pytester.runitem(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
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 == ""
|
2019-07-09 07:33:43 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_longrepr_type(self, pytester: Pytester) -> None:
|
|
|
|
reports = pytester.runitem(
|
2020-05-30 19:10:58 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
def test_func():
|
|
|
|
pytest.fail(pytrace=False)
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
rep = reports[1]
|
2020-12-15 19:02:32 +08:00
|
|
|
assert isinstance(rep.longrepr, ExceptionChainRepr)
|
2020-05-30 19:10:58 +08:00
|
|
|
|
2019-07-09 07:33:43 +08:00
|
|
|
|
2019-12-03 20:48:22 +08:00
|
|
|
def test_outcome_exception_bad_msg() -> None:
|
2019-07-09 07:33:43 +08:00
|
|
|
"""Check that OutcomeExceptions validate their input to prevent confusing errors (#5578)"""
|
|
|
|
|
2019-12-03 20:48:22 +08:00
|
|
|
def func() -> None:
|
2020-01-22 22:36:35 +08:00
|
|
|
raise NotImplementedError()
|
2019-07-09 07:33:43 +08:00
|
|
|
|
|
|
|
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
|
2019-07-09 07:33:43 +08:00
|
|
|
assert str(excinfo.value) == expected
|