diff options
author | David Lord <davidism@gmail.com> | 2019-10-10 07:14:15 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-10-10 07:14:15 -0700 |
commit | 81d526df96799d847d67fc1404138a6dac856ef3 (patch) | |
tree | 6088b3b5f59e96cf69eb82ffe9bbfb44371cd23b | |
parent | 0b9d252a071b0b5a171e1308157da74aa079b9d8 (diff) | |
parent | 9ca80538d9e9418ae658772516f9b7dfb1e02ccd (diff) | |
download | jinja2-81d526df96799d847d67fc1404138a6dac856ef3.tar.gz |
Merge pull request #1077 from pallets/pytest-cleanup
Pytest cleanup
-rw-r--r-- | tests/test_api.py | 12 | ||||
-rw-r--r-- | tests/test_async.py | 10 | ||||
-rw-r--r-- | tests/test_imports.py | 10 | ||||
-rw-r--r-- | tests/test_inheritance.py | 8 | ||||
-rw-r--r-- | tests/test_lexnparse.py | 6 | ||||
-rw-r--r-- | tests/test_regression.py | 8 |
6 files changed, 17 insertions, 37 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..f312164 100644 --- a/tests/test_inheritance.py +++ b/tests/test_inheritance.py @@ -11,7 +11,7 @@ import pytest from jinja2 import Environment, DictLoader, TemplateError - +from jinja2 import TemplateRuntimeError LAYOUTTEMPLATE = '''\ |{% block block1 %}block 1 from layout{% endblock %} @@ -242,7 +242,5 @@ class TestBugFix(object): """Ensures that a template with more than 1 {% extends ... %} usage raises a ``TemplateError``. """ - try: - tmpl = env.get_template('doublee') - except Exception as e: - assert isinstance(e, TemplateError) + with pytest.raises(TemplateRuntimeError, match="extended multiple times"): + env.get_template('doublee').render() 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 |