]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
Replaced try...catch within tests with pytest.raises
authorKevin Brown <kevin@kevin-brown.com>
Thu, 10 Oct 2019 06:15:15 +0000 (02:15 -0400)
committerKevin Brown <kevin@kevin-brown.com>
Thu, 10 Oct 2019 06:32:29 +0000 (02:32 -0400)
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.

tests/test_api.py
tests/test_async.py
tests/test_imports.py
tests/test_inheritance.py
tests/test_lexnparse.py
tests/test_regression.py

index e37d5cfcaccfbb567837c7302320c6be4c8f3c4e..1829b1d0069483f47fb8e1712aeaae653f08f734 100644 (file)
@@ -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
index 2f177473d2d04e836e0802fd19c28477376aa900..92ac2a393b7b92f57ac6d213ffdfcb87a7528d31 100644 (file)
@@ -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
index 4b8f312de974c583b2fae2d14d9059f034b4b2f1..0810e8a42476cdf3941a205fc8c2708f56cd89f7 100644 (file)
@@ -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
index 7746c2d7c88e4a393d6bec0dc1a985c8bdda1d04..e753506a36e88b4a8f13da8ba7f9c1f7e726d14f 100644 (file)
@@ -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)
index f6a312988bcc66ecd78a8da5eb22b1650d9bcc1a..cc941eb53402d0575c43a596bb68055b70fd79e2 100644 (file)
@@ -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 "
index 7477db2f867ad666ec40d06db4136e35140c3767..4b228efaab806fd41ae008e004b37140a25ee8bc 100644 (file)
@@ -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