Deprecate raises(..., 'code(as_a_string)') / `warns(..., 'code(as_a_string)')

This commit is contained in:
Anthony Sottile
2018-11-22 10:05:10 -08:00
parent 5cf69fae7d
commit 1bba0a9714
26 changed files with 140 additions and 96 deletions

View File

@@ -325,7 +325,7 @@ class TestGenerator(object):
assert len(colitems) == 1
gencol = colitems[0]
assert isinstance(gencol, pytest.Generator)
pytest.raises(ValueError, "gencol.collect()")
pytest.raises(ValueError, gencol.collect)
def test_generative_methods_with_explicit_names(self, testdir):
modcol = testdir.getmodulecol(
@@ -1103,7 +1103,8 @@ def test_modulecol_roundtrip(testdir):
class TestTracebackCutting(object):
def test_skip_simple(self):
excinfo = pytest.raises(pytest.skip.Exception, 'pytest.skip("xxx")')
with pytest.raises(pytest.skip.Exception) as excinfo:
pytest.skip("xxx")
assert excinfo.traceback[-1].frame.code.name == "skip"
assert excinfo.traceback[-1].ishidden()

View File

@@ -906,7 +906,8 @@ class TestRequestMarking(object):
assert "skipif" not in item1.keywords
req1.applymarker(pytest.mark.skipif)
assert "skipif" in item1.keywords
pytest.raises(ValueError, "req1.applymarker(42)")
with pytest.raises(ValueError):
req1.applymarker(42)
def test_accesskeywords(self, testdir):
testdir.makepyfile(

View File

@@ -70,11 +70,11 @@ class TestMetafunc(object):
pass
metafunc = self.Metafunc(func)
pytest.raises(ValueError, "metafunc.addcall(id=None)")
pytest.raises(ValueError, metafunc.addcall, id=None)
metafunc.addcall(id=1)
pytest.raises(ValueError, "metafunc.addcall(id=1)")
pytest.raises(ValueError, "metafunc.addcall(id='1')")
pytest.raises(ValueError, metafunc.addcall, id=1)
pytest.raises(ValueError, metafunc.addcall, id="1")
metafunc.addcall(id=2)
assert len(metafunc._calls) == 2
assert metafunc._calls[0].id == "1"
@@ -108,7 +108,7 @@ class TestMetafunc(object):
metafunc.addcall(funcargs={"x": 2})
metafunc.addcall(funcargs={"x": 3})
pytest.raises(pytest.fail.Exception, "metafunc.addcall({'xyz': 0})")
pytest.raises(pytest.fail.Exception, metafunc.addcall, {"xyz": 0})
assert len(metafunc._calls) == 2
assert metafunc._calls[0].funcargs == {"x": 2}
assert metafunc._calls[1].funcargs == {"x": 3}

View File

@@ -4,25 +4,32 @@ import six
import pytest
from _pytest.outcomes import Failed
from _pytest.warning_types import PytestDeprecationWarning
class TestRaises(object):
def test_raises(self):
source = "int('qwe')"
excinfo = pytest.raises(ValueError, source)
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):
pytest.raises(ValueError, "a,x = []")
with pytest.warns(PytestDeprecationWarning) as warninfo:
pytest.raises(ValueError, "a,x = []")
assert warninfo[0].filename == __file__
def test_raises_exec_correct_filename(self):
excinfo = pytest.raises(ValueError, 'int("s")')
assert __file__ in excinfo.traceback[-1].path
with pytest.warns(PytestDeprecationWarning):
excinfo = pytest.raises(ValueError, 'int("s")')
assert __file__ in excinfo.traceback[-1].path
def test_raises_syntax_error(self):
pytest.raises(SyntaxError, "qwe qwe qwe")
with pytest.warns(PytestDeprecationWarning) as warninfo:
pytest.raises(SyntaxError, "qwe qwe qwe")
assert warninfo[0].filename == __file__
def test_raises_function(self):
pytest.raises(ValueError, int, "hello")