Replaced try...catch within tests with pytest.raises
Before adoption: this commit came in with the history the project adopted, so no landing records it.
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.
authored by Kevin Brown · committed by Kevin Brown · parent 0b9d252 · Browse files · 6 files·+15 added−35 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+15 added−35 removed
- tests:+15 added−35 removed
Provenance
- Commit
ca72c5f- Tree
5286cd5- Author
Kevin Brown <kevin@kevin-brown.com>- Committer
Kevin Brown <kevin@kevin-brown.com>- Parent
0b9d252
6 files changed+15 added−35 removed
modified:tests/test_api.py+2 added-10 removed
334 unmodified lines335 t = Template("{{ var[42].foo }}")336 pytest.raises(UndefinedError, t.render, var=0)337 338 def test_none_gives_proper_error(self):339- try:339+ with pytest.raises(UndefinedError, match= "'None' has no attribute 'split'"):340 Environment().getattr(None, 'split')()341- except UndefinedError as e:342- assert e.message == "'None' has no attribute 'split'"343- else:344- assert False, 'expected exception'341 342 def test_object_repr(self):347- try:343+ with pytest.raises(UndefinedError, match="'int object' has no attribute 'upper'"):344 Undefined(obj=42, name='upper')()349- except UndefinedError as e:350- assert e.message == "'int object' has no attribute 'upper'"351- else:352- assert False, 'expected exception'345 346 347 @pytest.mark.api348 @pytest.mark.lowlevel29 unmodified linesmodified:tests/test_async.py+4 added-6 removed
189 unmodified lines190 assert t.render(foo=42) == ''191 192 t = test_env_async.from_string('{% include ["missing", "missing2"] %}')193 pytest.raises(TemplateNotFound, t.render)194- try:194+ with pytest.raises(TemplatesNotFound) as e:195 t.render()196- except TemplatesNotFound as e:197- assert e.templates == ['missing', 'missing2']198- assert e.name == 'missing2'199- else:200- assert False, 'thou shalt raise'196 197+ assert e.value.templates == ['missing', 'missing2']198+ assert e.value.name == 'missing2'199+200 def test_includes(t, **ctx):201 ctx['foo'] = 42202 assert t.render(ctx) == '[42|23]'203 307 unmodified linesmodified:tests/test_imports.py+4 added-6 removed
117 unmodified lines118 assert t.render(foo=42) == ''119 120 t = test_env.from_string('{% include ["missing", "missing2"] %}')121 pytest.raises(TemplateNotFound, t.render)122- try:122+ with pytest.raises(TemplatesNotFound) as e:123 t.render()124- except TemplatesNotFound as e:125- assert e.templates == ['missing', 'missing2']126- assert e.name == 'missing2'127- else:128- assert False, 'thou shalt raise'124 125+ assert e.value.templates == ['missing', 'missing2']126+ assert e.value.name == 'missing2'127+128 def test_includes(t, **ctx):129 ctx['foo'] = 42130 assert t.render(ctx) == '[42|23]'131 45 unmodified linesmodified:tests/test_inheritance.py+1 added-3 removed
240 unmodified lines241 def test_double_extends(self, env):242 """Ensures that a template with more than 1 {% extends ... %} usage243 raises a ``TemplateError``.244 """245- try:245+ with pytest.raises(TemplateError):246 tmpl = env.get_template('doublee')247- except Exception as e:248- assert isinstance(e, TemplateError)modified:tests/test_lexnparse.py+1 added-5 removed
249 unmodified lines250 assert tmpl.render(seq=[1, 2]).strip() == '* 1\n\n* 2'251 252 def test_error_messages(self, env):253 def assert_error(code, expected):254- try:254+ with pytest.raises(TemplateSyntaxError, match=expected):255 Template(code)256- except TemplateSyntaxError as e:257- assert str(e) == expected, 'unexpected error message'258- else:259- assert False, 'that was supposed to be an error'256 257 assert_error('{% for item in seq %}...{% endif %}',258 "Encountered unknown tag 'endif'. Jinja was looking "259 "for the following tags: 'endfor' or 'else'. The "430 unmodified linesmodified:tests/test_regression.py+3 added-5 removed
255 unmodified lines256 def test_correct_prefix_loader_name(self, env):257 env = Environment(loader=PrefixLoader({258 'foo': DictLoader({})259 }))260- try:260+ with pytest.raises(TemplateNotFound) as e:261 env.get_template('foo/bar.html')262- except TemplateNotFound as e:263- assert e.name == 'foo/bar.html'264- else:265- assert False, 'expected error here'262 263+ assert e.value.name == 'foo/bar.html'264+265 def test_contextfunction_callable_classes(self, env):266 from jinja2.utils import contextfunction267 268 class CallableClass(object):280 unmodified lines