From: Kevin Brown Date: Thu, 10 Oct 2019 06:15:15 +0000 (-0400) Subject: Replaced try...catch within tests with pytest.raises X-Git-Tag: 2.11.0~46^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ca72c5f301d1aa2ce0e75b440dc6364b4a69bbeb;p=thirdparty%2Fjinja.git Replaced try...catch within tests with pytest.raises 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. --- diff --git a/tests/test_api.py b/tests/test_api.py index e37d5cfc..1829b1d0 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 2f177473..92ac2a39 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 4b8f312d..0810e8a4 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 7746c2d7..e753506a 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 f6a31298..cc941eb5 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 7477db2f..4b228efa 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