Use pytest.raises() instead of try/catch with asser 0

This is somehow more readable, and enable using the features of pytest's ExeptionInfo (such as errisinstance).
This commit is contained in:
Reuven 2016-03-04 13:30:40 +02:00
parent e7d548595e
commit 4dc2ef19ea
5 changed files with 39 additions and 84 deletions

View file

@ -89,12 +89,9 @@ def test_config_from_envvar():
try:
os.environ = {}
app = flask.Flask(__name__)
try:
with pytest.raises(RuntimeError) as e:
app.config.from_envvar('FOO_SETTINGS')
except RuntimeError as e:
assert "'FOO_SETTINGS' is not set" in str(e)
else:
assert 0, 'expected exception'
assert "'FOO_SETTINGS' is not set" in str(e.value)
assert not app.config.from_envvar('FOO_SETTINGS', silent=True)
os.environ = {'FOO_SETTINGS': __file__.rsplit('.', 1)[0] + '.py'}
@ -108,16 +105,13 @@ def test_config_from_envvar_missing():
env = os.environ
try:
os.environ = {'FOO_SETTINGS': 'missing.cfg'}
try:
with pytest.raises(IOError) as e:
app = flask.Flask(__name__)
app.config.from_envvar('FOO_SETTINGS')
except IOError as e:
msg = str(e)
assert msg.startswith('[Errno 2] Unable to load configuration '
'file (No such file or directory):')
assert msg.endswith("missing.cfg'")
else:
assert False, 'expected IOError'
msg = str(e.value)
assert msg.startswith('[Errno 2] Unable to load configuration '
'file (No such file or directory):')
assert msg.endswith("missing.cfg'")
assert not app.config.from_envvar('FOO_SETTINGS', silent=True)
finally:
os.environ = env
@ -125,29 +119,23 @@ def test_config_from_envvar_missing():
def test_config_missing():
app = flask.Flask(__name__)
try:
with pytest.raises(IOError) as e:
app.config.from_pyfile('missing.cfg')
except IOError as e:
msg = str(e)
assert msg.startswith('[Errno 2] Unable to load configuration '
'file (No such file or directory):')
assert msg.endswith("missing.cfg'")
else:
assert 0, 'expected config'
msg = str(e.value)
assert msg.startswith('[Errno 2] Unable to load configuration '
'file (No such file or directory):')
assert msg.endswith("missing.cfg'")
assert not app.config.from_pyfile('missing.cfg', silent=True)
def test_config_missing_json():
app = flask.Flask(__name__)
try:
with pytest.raises(IOError) as e:
app.config.from_json('missing.json')
except IOError as e:
msg = str(e)
assert msg.startswith('[Errno 2] Unable to load configuration '
'file (No such file or directory):')
assert msg.endswith("missing.json'")
else:
assert 0, 'expected config'
msg = str(e.value)
assert msg.startswith('[Errno 2] Unable to load configuration '
'file (No such file or directory):')
assert msg.endswith("missing.json'")
assert not app.config.from_json('missing.json', silent=True)