Remove support for 'code as string' from pytest.raises and pytest.warns
This commit is contained in:
@@ -6,31 +6,17 @@ from _pytest.warning_types import PytestDeprecationWarning
|
||||
|
||||
|
||||
class TestRaises:
|
||||
def test_check_callable(self):
|
||||
with pytest.raises(TypeError, match=r".* must be callable"):
|
||||
pytest.raises(RuntimeError, "int('qwe')")
|
||||
|
||||
def test_raises(self):
|
||||
source = "int('qwe')"
|
||||
with pytest.warns(PytestDeprecationWarning):
|
||||
excinfo = pytest.raises(ValueError, source)
|
||||
code = excinfo.traceback[-1].frame.code
|
||||
s = str(code.fullsource)
|
||||
assert s == source
|
||||
|
||||
def test_raises_exec(self):
|
||||
with pytest.warns(PytestDeprecationWarning) as warninfo:
|
||||
pytest.raises(ValueError, "a,x = []")
|
||||
assert warninfo[0].filename == __file__
|
||||
|
||||
def test_raises_exec_correct_filename(self):
|
||||
with pytest.warns(PytestDeprecationWarning):
|
||||
excinfo = pytest.raises(ValueError, 'int("s")')
|
||||
assert __file__ in excinfo.traceback[-1].path
|
||||
|
||||
def test_raises_syntax_error(self):
|
||||
with pytest.warns(PytestDeprecationWarning) as warninfo:
|
||||
pytest.raises(SyntaxError, "qwe qwe qwe")
|
||||
assert warninfo[0].filename == __file__
|
||||
excinfo = pytest.raises(ValueError, int, "qwe")
|
||||
assert "invalid literal" in str(excinfo.value)
|
||||
|
||||
def test_raises_function(self):
|
||||
pytest.raises(ValueError, int, "hello")
|
||||
excinfo = pytest.raises(ValueError, int, "hello")
|
||||
assert "invalid literal" in str(excinfo.value)
|
||||
|
||||
def test_raises_callable_no_exception(self):
|
||||
class A:
|
||||
|
||||
@@ -25,7 +25,8 @@ class TestMark:
|
||||
|
||||
def test_pytest_mark_notcallable(self):
|
||||
mark = Mark()
|
||||
pytest.raises((AttributeError, TypeError), mark)
|
||||
with pytest.raises(TypeError):
|
||||
mark()
|
||||
|
||||
def test_mark_with_param(self):
|
||||
def some_function(abc):
|
||||
|
||||
@@ -3,7 +3,6 @@ import warnings
|
||||
|
||||
import pytest
|
||||
from _pytest.recwarn import WarningsRecorder
|
||||
from _pytest.warning_types import PytestDeprecationWarning
|
||||
|
||||
|
||||
def test_recwarn_stacklevel(recwarn):
|
||||
@@ -206,22 +205,17 @@ class TestDeprecatedCall:
|
||||
|
||||
|
||||
class TestWarns:
|
||||
def test_strings(self):
|
||||
def test_check_callable(self):
|
||||
source = "warnings.warn('w1', RuntimeWarning)"
|
||||
with pytest.raises(TypeError, match=r".* must be callable"):
|
||||
pytest.warns(RuntimeWarning, source)
|
||||
|
||||
def test_several_messages(self):
|
||||
# different messages, b/c Python suppresses multiple identical warnings
|
||||
source1 = "warnings.warn('w1', RuntimeWarning)"
|
||||
source2 = "warnings.warn('w2', RuntimeWarning)"
|
||||
source3 = "warnings.warn('w3', RuntimeWarning)"
|
||||
with pytest.warns(PytestDeprecationWarning) as warninfo: # yo dawg
|
||||
pytest.warns(RuntimeWarning, source1)
|
||||
pytest.raises(
|
||||
pytest.fail.Exception, lambda: pytest.warns(UserWarning, source2)
|
||||
)
|
||||
pytest.warns(RuntimeWarning, source3)
|
||||
assert len(warninfo) == 3
|
||||
for w in warninfo:
|
||||
assert w.filename == __file__
|
||||
msg, = w.message.args
|
||||
assert msg.startswith("warns(..., 'code(as_a_string)') is deprecated")
|
||||
pytest.warns(RuntimeWarning, lambda: warnings.warn("w1", RuntimeWarning))
|
||||
with pytest.raises(pytest.fail.Exception):
|
||||
pytest.warns(UserWarning, lambda: warnings.warn("w2", RuntimeWarning))
|
||||
pytest.warns(RuntimeWarning, lambda: warnings.warn("w3", RuntimeWarning))
|
||||
|
||||
def test_function(self):
|
||||
pytest.warns(
|
||||
|
||||
Reference in New Issue
Block a user