o",
        }
    
    
    def test_read_item_bad_token():
        response = client.get("/items/foo", headers={"X-Token": "hailhydra"})
        assert response.status_code == 400
        assert response.json() == {"detail": "Invalid X-Token header"}
    
    
    def test_read_nonexistent_item():
        response = client.get("/items/baz", headers={"X-Token": "coneofsilence"})
        assert response.status_code == 404
        assert response.json() == {"detail": "Item not found"}
    
    
    def test_create_item():
        response = client.post(
            "/items/",
            headers={"X-Token": "coneofsilence"},
            json={"id": "foobar", "title": "Foo Bar", "description": "The Foo Barters"},
        )
        assert response.status_code == 200
        assert response.json() == {
            "id": "foobar",
            "title": "Foo Bar",
            "description": "The Foo Barters",
        }
    
    
    def test_create_item_bad_token():
        response = client.post(
            "/items/",
            headers={"X-Token": "hailhydra"},
            json={"id": "bazz", "title": "Bazz", "description": "Drop the bazz"},
        )
        assert response.status_code == 400
        assert response.json() == {"detail": "Invalid X-Token header"}
    
    
    def test_create_existing_item():
        response = client.post(
            "/items/",
            headers={"X-Token": "coneofsilence"},
            json={
                "id": "foo",
                "title": "The Foo ID Stealers",
                "description": "There goes my stealer",
            },
        )
        assert response.status_code == 409
        assert response.json() == {"detail": "Item already exists"}
    

Whenever you need the client to pass information in the request and you don't
know how to, you can search (Google) how to do it in ` httpx ` , or even how
to do it with ` requests ` , as HTTPX's design is based on Requests' design.

Then you just do the same in your tests.

E.g.:

  * To pass a _path_ or _query_ parameter, add it to the URL itself. 
  * To pass a JSON body, pass a Python object (e.g. a ` dict ` ) to the parameter ` json ` . 
  * If you need to send _Form Data_ instead of JSON, use the ` data ` parameter instead. 
  * To pass _headers_ , use a ` dict ` in the ` headers ` parameter. 
  * For _cookies_ , a ` dict ` in the ` cookies ` parameter. 

For more information about how to pass data to the backend (using ` httpx ` or
the ` TestClient ` ) check the [ HTTPX documentation ](https://www.python-
httpx.org) .

Info

Note that the ` TestClient ` receives data that can be converted to JSON, not
Pydantic models.

If you have a Pydantic model in your test and you want to send its data to the
application during testing, you can use the ` jsonable_encoder ` described in
[ JSON Compatible Encoder ](../encoder/) .

##  Run it  ¶

After that, you just need to install ` pytest ` :

    
    
    $ pip install pytest
    
    ---> 100%
    

It will detect the files and tests automatically, execute them, and report the
results back to you.

Run the tests with:

    
    
    $ pytest
    
    ================ test session starts ================
    platform linux -- Python 3.6.9, pytest-5.3.5, py-1.8.1, pluggy-0.13.1
    rootdir: /home/user/code/superawesome-cli/app
    plugins: forked-1.1.3, xdist-1.31.0, cov-2.8.1
    collected 6 items
    
    ---> 100%
    
    test_main.py <span style="color: green; white-space: pre;">......                            [100%]</span>
    
    <span style="color: green;">================= 1 passed in 0.03s =================</span>
    

The FastAPI trademark is owned by [ @tiangolo ](https://tiangolo.com) and is
registered in the US and across other regions

Made with [ Material for MkDocs ](https://squidfunk.github.io/mkdocs-
material/)

[ ](https://github.com/tiangolo/fastapi "github.com") [
](https://discord.gg/VQjSZaeJmf "discord.gg") [ ](https://twitter.com/fastapi
"twitter.com") [ ](https