You need to create a new application for each test, so that each test starts sending request to a brand new server. By "application" I mean the instance of class Flask
.
Unfortunately this is a non-trivial change. The basics are explained in the official documentation: Application Factories.
After you follow the instructions in the referenced page you will not be calling
app = Flask(__name__)
in the global scope anymore, instead you will have a create_app()
function that returns this app
object. Each test can make its own then. Any requests you send into a brand new app
object will be the first.
The tricky part about switching to an app factory is that once you move the app
creation into a function you cannot use app.route()
anymore to define your routes, since app
is not defined in the global scope anymore. The solution is to use a blueprint that is attached to the application at run-time.
As a side note, the standard practice of calling the test client self.app
like this:
self.app = server.app.test_client()
is pretty confusing. Now you have two app
things. I prefer to call it client, since that is what it is:
self.client = server.app.test_client()
Lastly, note that your REST API is not really RESTful due to the dependency on order of calls and this undeterministic behavior is actually causing your problem. Your /count
route is not really representing a resource, it is representing an action. GET
requests should be read-only, changes to a resource should normally be issued with a PUT
request.