Fastapi: [FEATURE] Document test client

Created on 16 Mar 2019  Â·  4Comments  Â·  Source: tiangolo/fastapi

When switching from Flask / Flask_Restplus to FastAPI, tests fail
A basic test for a Flask RestPlus API will be like

class IntegrationTests(unittest.TestCase):
    def setUp(self):
        self.app = app.test_client()

    def test_base(self):
        response = self.app.get('/')
        self.assertEqual(response.status_code, 200)

but FastAPI don't have a test_client().

Solution
To have the tests pass, it would be nice, if FastAPI could provide a similar interface for Unittests (e.g. with Nose).

enhancement

All 4 comments

you can use starlette TestClient

Le sam. 16 mars 2019 à 11:11 AM, Balzer82 notifications@github.com a
écrit :

When switching from Flask / Flask_Restplus to FastAPI, tests fail
A basic test for a Flask RestPlus API will be like

class IntegrationTests(unittest.TestCase):
def setUp(self):
self.app = app.test_client()

def test_base(self):
    response = self.app.get('/')
    self.assertEqual(response.status_code, 200)

but FastAPI don't have a test_client().

Solution
To have the tests pass, it would be nice, if FastAPI could provide a
similar interface for Unittests (e.g. with Nose).

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/tiangolo/fastapi/issues/83, or mute the thread
https://github.com/notifications/unsubscribe-auth/ABDZPvXYzB2To_tzjFboGPHEiWOBppxhks5vXMMzgaJpZM4b3uRl
.

Thanks!! For anyone finding this Request, I solved it this way:

import unittest

from starlette.testclient import TestClient
from main import app # <-- App under Test

class IntegrationTests(unittest.TestCase):
    def setUp(self):
        self.app = TestClient(app)

    def test_base(self):
        response = self.app.get('/')
        self.assertEqual(response.status_code, 200)

if __name__ == '__main__':
    unittest.main()

Thanks @euri10 for your help here as always!

I'm glad you solved your problem @balzer82 .

Testing is something that is still missing from the docs, so I'll re-open this issue to remind me of that :smile:

There are new docs on testing FastAPI here: https://fastapi.tiangolo.com/tutorial/testing/ :tada: :rocket:

Was this page helpful?
0 / 5 - 0 ratings