diff --git a/tests/test_appctx.py b/tests/test_appctx.py index b9ed29b515..aeb75a55c0 100644 --- a/tests/test_appctx.py +++ b/tests/test_appctx.py @@ -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("/") diff --git a/tests/test_basic.py b/tests/test_basic.py index 2a177e9a70..3dc3a0e9a7 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -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): @@ -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 @@ -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): @@ -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) diff --git a/tests/test_cli.py b/tests/test_cli.py index f9f3673a67..6e8b57f05a 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -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() diff --git a/tests/test_config.py b/tests/test_config.py index bbe4f1e2d3..944b93d7c0 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -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( @@ -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(