e  ](../../history-design-future/)
    * [ Benchmarks  ](../../benchmarks/)
  * [ Help  ](../../help/)

Help

    * [ Help FastAPI - Get Help  ](../../help-fastapi/)
    * [ Development - Contributing  ](../../contributing/)
  * [ Release Notes  ](../../release-notes/)

Table of contents

  * Using ` TestClient `
  * Separating tests 
    * **FastAPI** app file 
    * Testing file 
  * Testing: extended example 
    * Extended **FastAPI** app file 
    * Extended testing file 
  * Run it 

  1. [ FastAPI  ](../..)
  2. [ Learn  ](../../learn/)
  3. [ Tutorial - User Guide  ](../)

#  Testing  ¶

Thanks to [ Starlette ](https://www.starlette.io/testclient/) , testing
**FastAPI** applications is easy and enjoyable.

It is based on [ HTTPX ](https://www.python-httpx.org) , which in turn is
designed based on Requests, so it's very familiar and intuitive.

With it, you can use [ pytest ](https://docs.pytest.org/) directly with
**FastAPI** .

##  Using ` TestClient ` ¶

Info

To use ` TestClient ` , first install [ ` httpx ` ](https://www.python-
httpx.org) .

E.g. ` pip install httpx ` .

Import ` TestClient ` .

Create a ` TestClient ` by passing your **FastAPI** application to it.

Create functions with a name that starts with ` test_ ` (this is standard `
pytest ` conventions).

Use the ` TestClient ` object the same way as you do with ` httpx ` .

Write simple ` assert ` statements with the standard Python expressions that
you need to check (again, standard ` pytest ` ).

    
    
    from fastapi import FastAPI
    from fastapi.testclient import TestClient
    
    app = FastAPI()
    
    
    @app.get("/")
    async def read_main():
        return {"msg": "Hello World"}
    
    
    client = TestClient(app)
    
    
    def test_read_main():
        response = client.get("/")
        assert response.status_code == 200
        assert response.json() == {"msg": "Hello World"}
    

Tip

Notice that the testing functions are normal ` def ` , not ` async def ` .

And the calls to the client are also normal calls, not using ` await ` .

This allows you to use ` pytest ` directly without complications.

Technical Details

You could also use ` from starlette.testclient import TestClient ` .

**FastAPI** provides the same ` starlette.testclient ` as ` fastapi.testclient
` just as a convenience for you, the developer. But it comes directly from
Starlette.

Tip

If you want to call ` async ` functions in your tests apart from sending
requests to your FastAPI application (e.g. asynchronous database functions),
have a look at the [ Async Tests ](../../advanced/async-tests/) in the
advanced tutorial.

##  Separating tests  ¶

In a real application, you probably would have your tests in a different file.

And your **FastAPI** application might also be composed of several
files/modules, etc.

###  **FastAPI** app file  ¶

Let's say you have a file structure as described in [ Bigger Applications
](../bigger-applications/) :

    
    
    .
    ├── app
    │   ├── __init__.py
    │   └── main.py
    

In the file ` main.py ` you have your **FastAPI** app:

    
    
    from fastapi import FastAPI
    
    app = FastAPI()
    
    
    @app.get("/")
    async def read_main():
        return {"msg": "Hello World"}
    

###  Testing file  ¶

Then you could have a file ` test_main.py ` with your tests. It could live on
the same Python package (the same directory with a ` __init__.py ` file):

    
    
    .
    ├── app
    │   ├── __init__.py
    │   ├── main.py
    │   └── test_main.py
    

Because this file is in the same package, you can use relative imports to
import the object ` app ` from the ` main ` module ( ` main.py ` ):

    
    
    from fastapi.testclient import TestClient
    
    from .main import app
    
    client = TestClient(app)
    
    
    def test_read_main():
        response = client.get("/")
        assert response.status_code == 200
        assert response.json() == {"msg": "Hello World"}
    

...and have t