Unit test or pytest with heavier Sanic app init


I have seen the docs which use the Sanic test client in order to test routes etc. A sample code would be:

from myservice import app    # sanic app

def test_1():
    req, rsp = app.test_client.get('/aroute')

However, my problem is that app initialization takes several seconds. Therefore, adding another test case to invoke test client on another route seems unnecessary.

The obvious solution is to init the app just once, in a pytest setup() method. But I can’t figure out a way to achieve so? How do I init the app and just use it across all test class’ methods?


Why does it take so long? There are two test clients: app.test_client and also app.asgi_client. The second reaches into the app to hit the end points as an ASGI without really standing up a server. Perhaps that could work for you?

Thanks @ahopkins. I got it working using a @pytest.fixture routine which uses the sanic_client (since test_client is deprecated now).

The app takes several seconds because it loads some huge context files to instantiate a class object. The class object is used to serve requests over a particular route.