jarus / flask-testing

Unittest extensions for Flask
http://pythonhosted.org/Flask-Testing/
Other
502 stars 111 forks source link

Re-use app in LiveServerTestCase subclass #115

Open acidjunk opened 6 years ago

acidjunk commented 6 years ago

First of all: thanks for this excellent testing project :)

I subclassed LiveServerTestCase to do some acceptance testing, and run it in a py.test env. In our case the startup of the app is rather expensive as it needs to connect to a SOAP server with a big wsdl, startup time around 5-6 seconds. Is there a way to reuse the already running app for test in the same TestCase class? Now the app is teared down and (re)started after each test.

jcomo commented 6 years ago

I can see the usefulness for a feature like that. I would like it to be controlled by a class level flag that defaults to the current behavior to maintain backwards compatibility and give users of the library the option about whether they want this behavior in their TestCase class or not. If you get a PR together, I can take a look at it.

acidjunk commented 6 years ago

I now achieved almost what I wanted with an env variabele that let's the developer choose between the LiveCaseServer or the normal flask one, but this makes it more difficult to integrate in the CI, as the server has to be running in background mode (not ideal):

This doesn't use the LiveTestCase server but relies on the user starting it's own:

DISABLE_LIVE_SERVER = True if os.getenv('DISABLE_LIVE_SERVER') else False

class MyLiveTestCase(LiveServerTestCase):
    def create_app(self):
        if DISABLE_LIVE_SERVER:
            return
        return run_application(False)

    def __call__(self, result=None):
        if DISABLE_LIVE_SERVER:
            return super(LiveServerTestCase, self).__call__(result)
        else:
            return super().__call__(result)

    def get_server_url(self):
        """
        Return the url of the test server
        """
        if DISABLE_LIVE_SERVER:
            return 'http://localhost:8080'
        else:
            return super().get_server_url()