summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Brown <kevin@kevin-brown.com>2019-10-10 02:15:15 -0400
committerKevin Brown <kevin@kevin-brown.com>2019-10-10 02:32:29 -0400
commitca72c5f301d1aa2ce0e75b440dc6364b4a69bbeb (patch)
tree5286cd51e11f0b1b343453c043618781514f44de
parent0b9d252a071b0b5a171e1308157da74aa079b9d8 (diff)
downloadjinja2-pytest-cleanup.tar.gz
Replaced try...catch within tests with pytest.raisespytest-cleanup
This still leaves one in test_debug which relies on reading out the traceback and cannot easily be replaced by pytest.raises like the others.
-rw-r--r--tests/test_api.py12
-rw-r--r--tests/test_async.py10
-rw-r--r--tests/test_imports.py10
-rw-r--r--tests/test_inheritance.py4
-rw-r--r--tests/test_lexnparse.py6
-rw-r--r--tests/test_regression.py8
6 files changed, 15 insertions, 35 deletions
diff --git a/tests/test_api.py b/tests/test_api.py
index e37d5cf..1829b1d 100644
--- a/tests/test_api.py
+++ b/tests/test_api.py
@@ -336,20 +336,12 @@ class TestUndefined(object):
pytest.raises(UndefinedError, t.render, var=0)
def test_none_gives_proper_error(self):
- try:
+ with pytest.raises(UndefinedError, match= "'None' has no attribute 'split'"):
Environment().getattr(None, 'split')()
- except UndefinedError as e:
- assert e.message == "'None' has no attribute 'split'"
- else:
- assert False, 'expected exception'
def test_object_repr(self):
- try:
+ with pytest.raises(UndefinedError, match="'int object' has no attribute 'upper'"):
Undefined(obj=42, name='upper')()
- except UndefinedError as e:
- assert e.message == "'int object' has no attribute 'upper'"
- else:
- assert False, 'expected exception'
@pytest.mark.api
diff --git a/tests/test_async.py b/tests/test_async.py
index 2f17747..92ac2a3 100644
--- a/tests/test_async.py
+++ b/tests/test_async.py
@@ -191,13 +191,11 @@ class TestAsyncIncludes(object):
t = test_env_async.from_string('{% include ["missing", "missing2"] %}')
pytest.raises(TemplateNotFound, t.render)
- try:
+ with pytest.raises(TemplatesNotFound) as e:
t.render()
- except TemplatesNotFound as e:
- assert e.templates == ['missing', 'missing2']
- assert e.name == 'missing2'
- else:
- assert False, 'thou shalt raise'
+
+ assert e.value.templates == ['missing', 'missing2']
+ assert e.value.name == 'missing2'
def test_includes(t, **ctx):
ctx['foo'] = 42
diff --git a/tests/test_imports.py b/tests/test_imports.py
index 4b8f312..0810e8a 100644
--- a/tests/test_imports.py
+++ b/tests/test_imports.py
@@ -119,13 +119,11 @@ class TestIncludes(object):
t = test_env.from_string('{% include ["missing", "missing2"] %}')
pytest.raises(TemplateNotFound, t.render)
- try:
+ with pytest.raises(TemplatesNotFound) as e:
t.render()
- except TemplatesNotFound as e:
- assert e.templates == ['missing', 'missing2']
- assert e.name == 'missing2'
- else:
- assert False, 'thou shalt raise'
+
+ assert e.value.templates == ['missing', 'missing2']
+ assert e.value.name == 'missing2'
def test_includes(t, **ctx):
ctx['foo'] = 42
diff --git a/tests/test_inheritance.py b/tests/test_inheritance.py
index 7746c2d..e753506 100644
--- a/tests/test_inheritance.py
+++ b/tests/test_inheritance.py
@@ -242,7 +242,5 @@ class TestBugFix(object):
"""Ensures that a template with more than 1 {% extends ... %} usage
raises a ``TemplateError``.
"""
- try:
+ with pytest.raises(TemplateError):
tmpl = env.get_template('doublee')
- except Exception as e:
- assert isinstance(e, TemplateError)
diff --git a/tests/test_lexnparse.py b/tests/test_lexnparse.py
index f6a3129..cc941eb 100644
--- a/tests/test_lexnparse.py
+++ b/tests/test_lexnparse.py
@@ -251,12 +251,8 @@ and bar comment #}
def test_error_messages(self, env):
def assert_error(code, expected):
- try:
+ with pytest.raises(TemplateSyntaxError, match=expected):
Template(code)
- except TemplateSyntaxError as e:
- assert str(e) == expected, 'unexpected error message'
- else:
- assert False, 'that was supposed to be an error'
assert_error('{% for item in seq %}...{% endif %}',
"Encountered unknown tag 'endif'. Jinja was looking "
diff --git a/tests/test_regression.py b/tests/test_regression.py
index 7477db2..4b228ef 100644
--- a/tests/test_regression.py
+++ b/tests/test_regression.py
@@ -257,12 +257,10 @@ class TestBug(object):
env = Environment(loader=PrefixLoader({
'foo': DictLoader({})
}))
- try:
+ with pytest.raises(TemplateNotFound) as e:
env.get_template('foo/bar.html')
- except TemplateNotFound as e:
- assert e.name == 'foo/bar.html'
- else:
- assert False, 'expected error here'
+
+ assert e.value.name == 'foo/bar.html'
def test_contextfunction_callable_classes(self, env):
from jinja2.utils import contextfunction