0
0
Rest APIprogramming~30 mins

Integration testing in Rest API - Mini Project: Build & Apply

Choose your learning style9 modes available
Integration Testing for a REST API
📖 Scenario: You are working on a simple REST API that manages a list of books. Each book has a title and an author. You want to make sure that the API works correctly when you add a book and then retrieve the list of books.
🎯 Goal: Build a small integration test that sends a POST request to add a book and then sends a GET request to retrieve the list of books. You will check that the book you added appears in the list.
📋 What You'll Learn
Create a test client to interact with the REST API
Send a POST request to add a book with exact title and author
Send a GET request to retrieve the list of books
Check that the response contains the book you added
💡 Why This Matters
🌍 Real World
Integration testing is used to make sure that different parts of a web API work together correctly before releasing it to users.
💼 Career
Many software development jobs require writing integration tests to ensure APIs behave as expected and to catch bugs early.
Progress0 / 4 steps
1
Setup test client and initial data
Create a variable called test_client that represents the API client. Also create a dictionary called new_book with these exact entries: 'title': 'The Great Gatsby' and 'author': 'F. Scott Fitzgerald'.
Rest API
Need a hint?

Use APIClient() to create the test client. Define new_book as a dictionary with the exact keys and values.

2
Send POST request to add the book
Use test_client.post to send a POST request to the URL '/books/' with the JSON data new_book. Store the response in a variable called post_response.
Rest API
Need a hint?

Use test_client.post with the URL '/books/' and pass data=new_book and format='json' to send the book data.

3
Send GET request to retrieve books
Use test_client.get to send a GET request to the URL '/books/'. Store the response in a variable called get_response.
Rest API
Need a hint?

Use test_client.get with the URL '/books/' to get the list of books.

4
Check that the added book is in the response
Print the JSON content of get_response and check that it contains the dictionary new_book. Use print(get_response.json()).
Rest API
Need a hint?

Use print(get_response.json()) to see the list of books returned by the API.