You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I updated flask to 2.2.0 recently and had issues with previously passing tests start failing. I ended up determining the root cause to be the _got_first_request field on the Flask class, which I believe was added in that version. The field is initialized to False, and then when the first request is made against the application, the field is changed to True. From then on, any attempts to call add_url_rule(registering a handler) throws an exception, saying that it can't be called after the app has received it's first request.
This change did not cause me any issues in my production code as all the routes are registered at the beginning prior to the app receiving any requests.
However, one of my test classes started failing because of how ClientTestCase initializes and reuses the app field. Because it is expected to be initialized once as a class field and is the used for all the test cases, any tests that register routes as part of the 'setup' and the invoke a request cannot be run together. The first test (alphabetically, as this is the order unittest runs them) will properly run and pass. The second will throw an exception and fail when the add_url_rule is called as the first test already invoked a request.
I was able to find a workaround. In the setup method, I 'reset' this boolean with self.app._got_first_request = False. This works and passes tests as it did in earlier versions, but is not a satisfying fix because it requires manually setting a 'private' field. I could not come up with any other ways to achieve this 'reset' though because of the way the ClientTestCase reuses the app for all the tests... If there are any ideas I would appreciate them. The best idea I have is to use this workaround now and make some change to flask_unittest to handle this, perhaps by spinning up a new flask app for each test or finding some way of resetting the registered routes.
Assuming it would be helpful, I put together a trivialized version of the situation I am experiencing:
This simple program can be run normally and will create a flask application that handles requests on localhost, or tests can be run by running unittest and specifying the script as the target. Note that both the main and the tests are in the same file, this was fine for me. When I ran this with flask 2.1.3(the precursor), the tests passed. Running it with 2.2.0 causes the second test to fail.
Sorry for the long issue, but I hope the description and details makes reproducing/addressing it simple.
The text was updated successfully, but these errors were encountered:
I agree, a flask 2.0 compatibility update in general is overdue. Unfortunately, I don't have meaningful bandwidth to dedicate here as I have mostly moved on to other projects.
If someone is able to PR such an update, I'd gladly review and merge it. If not, I'll try to look into this over the weekend - but I can't promise anything at this stage.
I updated flask to 2.2.0 recently and had issues with previously passing tests start failing. I ended up determining the root cause to be the
_got_first_request
field on theFlask
class, which I believe was added in that version. The field is initialized toFalse
, and then when the first request is made against the application, the field is changed toTrue
. From then on, any attempts to calladd_url_rule
(registering a handler) throws an exception, saying that it can't be called after the app has received it's first request.This change did not cause me any issues in my production code as all the routes are registered at the beginning prior to the app receiving any requests.
However, one of my test classes started failing because of how
ClientTestCase
initializes and reuses theapp
field. Because it is expected to be initialized once as a class field and is the used for all the test cases, any tests that register routes as part of the 'setup' and the invoke a request cannot be run together. The first test (alphabetically, as this is the order unittest runs them) will properly run and pass. The second will throw an exception and fail when theadd_url_rule
is called as the first test already invoked a request.I was able to find a workaround. In the
setup
method, I 'reset' this boolean withself.app._got_first_request = False
. This works and passes tests as it did in earlier versions, but is not a satisfying fix because it requires manually setting a 'private' field. I could not come up with any other ways to achieve this 'reset' though because of the way theClientTestCase
reuses theapp
for all the tests... If there are any ideas I would appreciate them. The best idea I have is to use this workaround now and make some change toflask_unittest
to handle this, perhaps by spinning up a new flask app for each test or finding some way of resetting the registered routes.Assuming it would be helpful, I put together a trivialized version of the situation I am experiencing:
This simple program can be run normally and will create a flask application that handles requests on localhost, or tests can be run by running unittest and specifying the script as the target. Note that both the
main
and the tests are in the same file, this was fine for me. When I ran this with flask2.1.3
(the precursor), the tests passed. Running it with2.2.0
causes the second test to fail.Sorry for the long issue, but I hope the description and details makes reproducing/addressing it simple.
The text was updated successfully, but these errors were encountered: