Skip to content

Commit a0bb10b

Browse files
authored
Merge pull request #4555 from pallets/pytest_raises_cleanup
clean up `pytest.raises` tests
2 parents ef7d01f + ef6c2b9 commit a0bb10b

File tree

4 files changed

+25
-22
lines changed

4 files changed

+25
-22
lines changed

tests/test_appctx.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def cleanup(exception):
120120
def index():
121121
raise Exception("dummy")
122122

123-
with pytest.raises(Exception):
123+
with pytest.raises(Exception, match="dummy"):
124124
with app.app_context():
125125
client.get("/")
126126

tests/test_basic.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,7 @@ def more():
150150

151151
def test_disallow_string_for_allowed_methods(app):
152152
with pytest.raises(TypeError):
153-
154-
@app.route("/", methods="GET POST")
155-
def index():
156-
return "Hey"
153+
app.add_url_rule("/", methods="GET POST", endpoint="test")
157154

158155

159156
def test_url_mapping(app, client):
@@ -937,8 +934,9 @@ def test_baseexception_error_handling(app, client):
937934
def broken_func():
938935
raise KeyboardInterrupt()
939936

940-
with pytest.raises(KeyboardInterrupt):
941-
client.get("/")
937+
with client:
938+
with pytest.raises(KeyboardInterrupt):
939+
client.get("/")
942940

943941
ctx = flask._request_ctx_stack.top
944942
assert ctx.preserved
@@ -1243,20 +1241,25 @@ def from_bad_wsgi():
12431241

12441242
with pytest.raises(TypeError) as e:
12451243
c.get("/none")
1246-
assert "returned None" in str(e.value)
1247-
assert "from_none" in str(e.value)
1244+
1245+
assert "returned None" in str(e.value)
1246+
assert "from_none" in str(e.value)
12481247

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

1253-
pytest.raises(TypeError, c.get, "/large_tuple")
1251+
assert "tuple must have the form" in str(e.value)
1252+
1253+
with pytest.raises(TypeError):
1254+
c.get("/large_tuple")
12541255

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

1259-
pytest.raises(TypeError, c.get, "/bad_wsgi")
1259+
assert "it was a bool" in str(e.value)
1260+
1261+
with pytest.raises(TypeError):
1262+
c.get("/bad_wsgi")
12601263

12611264

12621265
def test_make_response(app, req_ctx):
@@ -1674,11 +1677,9 @@ def index():
16741677

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

1679-
@app.route("/foo")
1680-
def broken():
1681-
return "Meh"
1681+
with pytest.raises(AssertionError) as e:
1682+
app.add_url_rule("/foo", endpoint="late")
16821683

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

tests/test_cli.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,9 @@ def bad_load():
305305

306306
lazy = DispatchingApp(bad_load, use_eager_loading=False)
307307

308-
with pytest.raises(BadExc):
309-
# reduce flakiness by waiting for the internal loading lock
310-
with lazy._lock:
308+
# reduce flakiness by waiting for the internal loading lock
309+
with lazy._lock:
310+
with pytest.raises(BadExc):
311311
lazy._flush_bg_loading_exception()
312312

313313

tests/test_config.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,11 @@ class Test(Base):
133133
def test_config_from_envvar(monkeypatch):
134134
monkeypatch.setattr("os.environ", {})
135135
app = flask.Flask(__name__)
136+
136137
with pytest.raises(RuntimeError) as e:
137138
app.config.from_envvar("FOO_SETTINGS")
138-
assert "'FOO_SETTINGS' is not set" in str(e.value)
139+
140+
assert "'FOO_SETTINGS' is not set" in str(e.value)
139141
assert not app.config.from_envvar("FOO_SETTINGS", silent=True)
140142

141143
monkeypatch.setattr(
@@ -147,8 +149,8 @@ def test_config_from_envvar(monkeypatch):
147149

148150
def test_config_from_envvar_missing(monkeypatch):
149151
monkeypatch.setattr("os.environ", {"FOO_SETTINGS": "missing.cfg"})
152+
app = flask.Flask(__name__)
150153
with pytest.raises(IOError) as e:
151-
app = flask.Flask(__name__)
152154
app.config.from_envvar("FOO_SETTINGS")
153155
msg = str(e.value)
154156
assert msg.startswith(

0 commit comments

Comments
 (0)