Skip to content

Commit

Permalink
Merge pull request #4555 from pallets/pytest_raises_cleanup
Browse files Browse the repository at this point in the history
clean up `pytest.raises` tests
  • Loading branch information
davidism committed Apr 28, 2022
2 parents ef7d01f + ef6c2b9 commit a0bb10b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
2 changes: 1 addition & 1 deletion tests/test_appctx.py
Expand Up @@ -120,7 +120,7 @@ def cleanup(exception):
def index():
raise Exception("dummy")

with pytest.raises(Exception):
with pytest.raises(Exception, match="dummy"):
with app.app_context():
client.get("/")

Expand Down
33 changes: 17 additions & 16 deletions tests/test_basic.py
Expand Up @@ -150,10 +150,7 @@ def more():

def test_disallow_string_for_allowed_methods(app):
with pytest.raises(TypeError):

@app.route("/", methods="GET POST")
def index():
return "Hey"
app.add_url_rule("/", methods="GET POST", endpoint="test")


def test_url_mapping(app, client):
Expand Down Expand Up @@ -937,8 +934,9 @@ def test_baseexception_error_handling(app, client):
def broken_func():
raise KeyboardInterrupt()

with pytest.raises(KeyboardInterrupt):
client.get("/")
with client:
with pytest.raises(KeyboardInterrupt):
client.get("/")

ctx = flask._request_ctx_stack.top
assert ctx.preserved
Expand Down Expand Up @@ -1243,20 +1241,25 @@ def from_bad_wsgi():

with pytest.raises(TypeError) as e:
c.get("/none")
assert "returned None" in str(e.value)
assert "from_none" in str(e.value)

assert "returned None" in str(e.value)
assert "from_none" in str(e.value)

with pytest.raises(TypeError) as e:
c.get("/small_tuple")
assert "tuple must have the form" in str(e.value)

pytest.raises(TypeError, c.get, "/large_tuple")
assert "tuple must have the form" in str(e.value)

with pytest.raises(TypeError):
c.get("/large_tuple")

with pytest.raises(TypeError) as e:
c.get("/bad_type")
assert "it was a bool" in str(e.value)

pytest.raises(TypeError, c.get, "/bad_wsgi")
assert "it was a bool" in str(e.value)

with pytest.raises(TypeError):
c.get("/bad_wsgi")


def test_make_response(app, req_ctx):
Expand Down Expand Up @@ -1674,11 +1677,9 @@ def index():

assert not app.got_first_request
assert client.get("/").data == b"Awesome"
with pytest.raises(AssertionError) as e:

@app.route("/foo")
def broken():
return "Meh"
with pytest.raises(AssertionError) as e:
app.add_url_rule("/foo", endpoint="late")

assert "A setup function was called" in str(e.value)

Expand Down
6 changes: 3 additions & 3 deletions tests/test_cli.py
Expand Up @@ -305,9 +305,9 @@ def bad_load():

lazy = DispatchingApp(bad_load, use_eager_loading=False)

with pytest.raises(BadExc):
# reduce flakiness by waiting for the internal loading lock
with lazy._lock:
# reduce flakiness by waiting for the internal loading lock
with lazy._lock:
with pytest.raises(BadExc):
lazy._flush_bg_loading_exception()


Expand Down
6 changes: 4 additions & 2 deletions tests/test_config.py
Expand Up @@ -133,9 +133,11 @@ class Test(Base):
def test_config_from_envvar(monkeypatch):
monkeypatch.setattr("os.environ", {})
app = flask.Flask(__name__)

with pytest.raises(RuntimeError) as e:
app.config.from_envvar("FOO_SETTINGS")
assert "'FOO_SETTINGS' is not set" in str(e.value)

assert "'FOO_SETTINGS' is not set" in str(e.value)
assert not app.config.from_envvar("FOO_SETTINGS", silent=True)

monkeypatch.setattr(
Expand All @@ -147,8 +149,8 @@ def test_config_from_envvar(monkeypatch):

def test_config_from_envvar_missing(monkeypatch):
monkeypatch.setattr("os.environ", {"FOO_SETTINGS": "missing.cfg"})
app = flask.Flask(__name__)
with pytest.raises(IOError) as e:
app = flask.Flask(__name__)
app.config.from_envvar("FOO_SETTINGS")
msg = str(e.value)
assert msg.startswith(
Expand Down

0 comments on commit a0bb10b

Please sign in to comment.