Writing Rest API Test Case using TestClient Package

Writing Rest API Test Case using TestClient Package

Content: We are writing five simple test case

  1. Test case for root is the end point
  2. Test case where there is fix end point
  3. Test case where you have end point made with path parameter
  4. Test case where you have part of end point as  fixed string and other part with  parameter

First thing is you need to keep your test module separate from you program logic file like below, this is one of the way, not necessary to adopt this way only:

Now in the test file you need to import TestClient and also you need to import the object of FastAPI object, in our case it is app = FastAPI() and then pass the app object to TestClient(app)

from fastapi.testclient import TestClient
from endpoint_parameter_two import app
import pytest

client = TestClient(app)

Now once this is done, we are ready to write the test case for our 5 case:

Test case where root is the end point

This is the method where the end point is root only, for this we will write test case

@app.get("/")
async def root():
    return {"message": "Hello science"}

The test case of above would be very simple

client = TestClient(app)


def test_case1():
    response = client.get("/")
    assert response.status_code == 200
    assert response.json() == {"message": "Hello science"}

here use TestClient object to call get method and pass the endpoint and store the result in response variable. Then we assert the response status code and response json object.

Test case where there is fix end point

Method for which we will write test case

@app.get("/hello/mrabc")
async def hello():
    return {"name": 'Abc'}

The test case for fixed path is below, here we call the end point with the testClient object “app” and store the result in response.

client = TestClient(app)

def test_case2():
    print('a')
    response = client.get("/hello/mrabc")
    assert response.status_code == 200
    assert response.json() == {"name": 'Abc'}

Then the response is asserted for status code and json output.

Test case where you have end point made with path parameter

Method for which we will write test case

@app.get("/{firstname}/{age}")
async def firstname_age(firstname: str, age: int):
    return {"first_name": firstname, "age": age}

The test case for fixed path is below, here we call the end point with the testClient object “app” and store the result in response.

def test_case3():
    print('a')
    response = client.get("/harsh/20")
    assert response.status_code == 200
    assert response.json() == {"first_name": "harsh", "age": 20}

Then the response is asserted for status code and json output.

Test case where you have part of end point as fixed string and other part with parameter

Below is the method for which we are writing test case, here if you see, first part of the end point is fix string i.e. “/hello/” where as other part is parameter i.e. “/{firstname}/{lastname}”

@app.get("/hello/{firstname}/{lastname}")
async def hello_name(firstname, lastname):
    return {"name": firstname, "lastname": lastname}

The test case for fixed path is below, here we call the end point with the testClient object “app” and store the result in response.

def test_case4():
    print('a')
    response = client.get("/hello/Raj/Patel")
    assert response.status_code == 200
    assert response.json() == {"name": "Raj", "lastname": "Patel"}

Then the response is asserted for status code and json output.

Complete Code for Methods used for writing Test case:

from fastapi import FastAPI

app = FastAPI()


@app.get("/")
async def root():
    return {"message": "Hello science"}


@app.get("/hello/mrabc")
async def hello():
    return {"name": 'Abc'}


@app.get("/{firstname}/{age}")
async def firstname_age(firstname: str, age: int):
    return {"first_name": firstname, "age": age}


@app.get("/hello/{firstname}/{lastname}")
async def hello_name(firstname, lastname):
    return {"name": firstname, "lastname": lastname}

Complete code for test case created for above methods:

from fastapi.testclient import TestClient
from endpoint_parameter_two import app
import pytest

client = TestClient(app)

def test_case1():
    response = client.get("/")
    assert response.status_code == 200
    assert response.json() == {"message": "Hello science"}

def test_case2():
    print('a')
    response = client.get("/hello/mrabc")
    assert response.status_code == 200
    assert response.json() == {"name": 'Abc'}

def test_case3():
    print('a')
    response = client.get("/harsh/20")
    assert response.status_code == 200
    assert response.json() == {"first_name": "harsh", "age": 20}

def test_case4():
    print('a')
    response = client.get("/hello/Raj/Patel")
    assert response.status_code == 200
    assert response.json() == {"name": "Raj", "lastname": "Patel"}

In next post we will cover Query Parameter.

Happy Coding!

Leave a Reply

Your email address will not be published. Required fields are marked *