mirror of https://github.com/pallets/flask.git
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:
parent
e7d548595e
commit
4dc2ef19ea
|
@ -307,12 +307,8 @@ def test_missing_session():
|
|||
app = flask.Flask(__name__)
|
||||
|
||||
def expect_exception(f, *args, **kwargs):
|
||||
try:
|
||||
f(*args, **kwargs)
|
||||
except RuntimeError as e:
|
||||
assert e.args and 'session is unavailable' in e.args[0]
|
||||
else:
|
||||
assert False, 'expected exception'
|
||||
e = pytest.raises(RuntimeError, f, *args, **kwargs)
|
||||
assert e.value.args and 'session is unavailable' in e.value.args[0]
|
||||
with app.test_request_context():
|
||||
assert flask.session.get('missing_key') is None
|
||||
expect_exception(flask.session.__setitem__, 'foo', 42)
|
||||
|
@ -853,12 +849,9 @@ def test_trapping_of_bad_request_key_errors():
|
|||
|
||||
app.config['TRAP_BAD_REQUEST_ERRORS'] = True
|
||||
c = app.test_client()
|
||||
try:
|
||||
c.get('/fail')
|
||||
except KeyError as e:
|
||||
assert isinstance(e, BadRequest)
|
||||
else:
|
||||
assert False, 'Expected exception'
|
||||
with pytest.raises(KeyError) as e:
|
||||
c.get("/fail")
|
||||
assert e.errisinstance(BadRequest)
|
||||
|
||||
|
||||
def test_trapping_of_all_http_exceptions():
|
||||
|
@ -888,13 +881,10 @@ def test_enctype_debug_helper():
|
|||
# stack otherwise and we want to ensure that this is not the case
|
||||
# to not negatively affect other tests.
|
||||
with app.test_client() as c:
|
||||
try:
|
||||
with pytest.raises(DebugFilesKeyError) as e:
|
||||
c.post('/fail', data={'foo': 'index.txt'})
|
||||
except DebugFilesKeyError as e:
|
||||
assert 'no file contents were transmitted' in str(e)
|
||||
assert 'This was submitted: "index.txt"' in str(e)
|
||||
else:
|
||||
assert False, 'Expected exception'
|
||||
assert 'no file contents were transmitted' in str(e.value)
|
||||
assert 'This was submitted: "index.txt"' in str(e.value)
|
||||
|
||||
|
||||
def test_response_creation():
|
||||
|
@ -1203,12 +1193,8 @@ def test_exception_propagation():
|
|||
c = app.test_client()
|
||||
if config_key is not None:
|
||||
app.config[config_key] = True
|
||||
try:
|
||||
with pytest.raises(Exception):
|
||||
c.get('/')
|
||||
except Exception:
|
||||
pass
|
||||
else:
|
||||
assert False, 'expected exception'
|
||||
else:
|
||||
assert c.get('/').status_code == 500
|
||||
|
||||
|
@ -1345,14 +1331,11 @@ def test_debug_mode_complains_after_first_request():
|
|||
return 'Awesome'
|
||||
assert not app.got_first_request
|
||||
assert app.test_client().get('/').data == b'Awesome'
|
||||
try:
|
||||
with pytest.raises(AssertionError) as e:
|
||||
@app.route('/foo')
|
||||
def broken():
|
||||
return 'Meh'
|
||||
except AssertionError as e:
|
||||
assert 'A setup function was called' in str(e)
|
||||
else:
|
||||
assert False, 'Expected exception'
|
||||
|
||||
app.debug = False
|
||||
|
||||
|
@ -1408,14 +1391,11 @@ def test_routing_redirect_debugging():
|
|||
def foo():
|
||||
return 'success'
|
||||
with app.test_client() as c:
|
||||
try:
|
||||
with pytest.raises(AssertionError) as e:
|
||||
c.post('/foo', data={})
|
||||
except AssertionError as e:
|
||||
assert 'http://localhost/foo/' in str(e)
|
||||
assert ('Make sure to directly send '
|
||||
'your POST-request to this URL') in str(e)
|
||||
else:
|
||||
assert False, 'Expected exception'
|
||||
|
||||
rv = c.get('/foo', data={}, follow_redirects=True)
|
||||
assert rv.data == b'success'
|
||||
|
|
|
@ -174,12 +174,9 @@ def test_templates_and_static(test_apps):
|
|||
assert flask.url_for('admin.static', filename='test.txt') == '/admin/static/test.txt'
|
||||
|
||||
with app.test_request_context():
|
||||
try:
|
||||
with pytest.raises(TemplateNotFound) as e:
|
||||
flask.render_template('missing.html')
|
||||
except TemplateNotFound as e:
|
||||
assert e.name == 'missing.html'
|
||||
else:
|
||||
assert 0, 'expected exception'
|
||||
assert e.value.name == 'missing.html'
|
||||
|
||||
with flask.Flask(__name__).test_request_context():
|
||||
assert flask.render_template('nested/nested.txt') == 'I\'m nested'
|
||||
|
|
|
@ -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)
|
||||
msg = str(e.value)
|
||||
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'
|
||||
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)
|
||||
msg = str(e.value)
|
||||
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'
|
||||
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)
|
||||
msg = str(e.value)
|
||||
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'
|
||||
assert not app.config.from_json('missing.json', silent=True)
|
||||
|
||||
|
||||
|
|
|
@ -140,12 +140,8 @@ def test_manual_context_binding():
|
|||
ctx.push()
|
||||
assert index() == 'Hello World!'
|
||||
ctx.pop()
|
||||
try:
|
||||
with pytest.raises(RuntimeError):
|
||||
index()
|
||||
except RuntimeError:
|
||||
pass
|
||||
else:
|
||||
assert 0, 'expected runtime error'
|
||||
|
||||
@pytest.mark.skipif(greenlet is None, reason='greenlet not installed')
|
||||
def test_greenlet_context_copying():
|
||||
|
|
|
@ -100,13 +100,10 @@ def test_session_transactions_no_null_sessions():
|
|||
app.testing = True
|
||||
|
||||
with app.test_client() as c:
|
||||
try:
|
||||
with pytest.raises(RuntimeError) as e:
|
||||
with c.session_transaction() as sess:
|
||||
pass
|
||||
except RuntimeError as e:
|
||||
assert 'Session backend did not open a session' in str(e)
|
||||
else:
|
||||
assert False, 'Expected runtime error'
|
||||
assert 'Session backend did not open a session' in str(e.value)
|
||||
|
||||
def test_session_transactions_keep_context():
|
||||
app = flask.Flask(__name__)
|
||||
|
@ -124,13 +121,10 @@ def test_session_transaction_needs_cookies():
|
|||
app = flask.Flask(__name__)
|
||||
app.testing = True
|
||||
c = app.test_client(use_cookies=False)
|
||||
try:
|
||||
with pytest.raises(RuntimeError) as e:
|
||||
with c.session_transaction() as s:
|
||||
pass
|
||||
except RuntimeError as e:
|
||||
assert 'cookies' in str(e)
|
||||
else:
|
||||
assert False, 'Expected runtime error'
|
||||
assert 'cookies' in str(e.value)
|
||||
|
||||
def test_test_client_context_binding():
|
||||
app = flask.Flask(__name__)
|
||||
|
|
Loading…
Reference in New Issue