Rest Api Testing using Requests Module in Python with PyTest
To test rest-api -
- Requests module
- PyTest
- Python
- Allure report
- Config.ini
- Data Driven Testing
- Csv Data
-
Test Scripts (test_*.py): Python files containing the actual test cases using the pytest framework for test discovery and execution.
test_delete_task_delete_request.py
: Tests the DELETE endpoint for tasks.test_update_task_put_request.py
: Tests the PUT endpoint for tasks.test_create_user_post_request.py
: Tests the POST, GET, and PUT endpoints for users.
-
Utilities (Utilities directory): Contains helper functions and configurations used by the test scripts.
config_reader.py
: Reads configuration values from a file (likelyconfig.ini
), centralizing configuration management.read_csv_data.py
: Used for data-driven testing by reading test data from a CSV file.
-
Configuration (config.ini): Stores configuration settings, such as API endpoints, authentication details, and other environment-specific information.
-
Test Data (*.csv): CSV files store test data for data-driven testing, e.g., multiple sets of user data for the CreateUser API.
-
Allure Report: Generated by Allure, the
allure-report
directory contains detailed information about test execution.index.html
: The main HTML file for the Allure report.widgets/
: Contains JSON files for various report widgets (e.g., categories, summary, behaviors, timelines, history).data/attachments/
: Stores attachments like request/response logs, screenshots, or other relevant data.data/suites.json
: Contains information about test suites and their results.data/behaviors.json
: Contains information about test behaviors.data/timeline.json
: Contains information about the test execution timeline.data/history/
: Contains historical test results.
-
Python: The programming language used for writing test scripts and utility functions.
-
requests Library: A Python library for making HTTP requests, used to send requests and receive responses from API endpoints.
-
pytest Framework: A Python testing framework that simplifies test discovery, execution, and reporting. It supports features like test fixtures, parametrization, and assertions.
-
Allure Report: A reporting framework that generates visually appealing and informative test reports, featuring test categorization, timelines, graphs, and attachments.
-
Data-Driven Testing: Testing technique where test data is stored in external files (e.g., CSV) and used to run the same test with different inputs.
-
Configuration Management: Storing configuration settings (like API endpoints) in files (
config.ini
) to make tests more maintainable and adaptable.
-
test_delete_task_delete_request.py
:- Imports:
requests
,config_reader
. ENDPOINT
: Reads the DELETE task endpoint URL from the configuration file.test_delete_task_rqst_validation_error()
: Sends a DELETE request with an invalid task ID and asserts a 404 status code and error message.
- Imports:
-
test_update_task_put_request.py
:- Imports:
requests
,config_reader
. ENDPOINT
: Reads the PUT task endpoint URL from the configuration file.test_update_task_rqst_validation_error()
: Sends a PUT request with an empty payload and asserts a 422 status code and validation error message.
- Imports:
-
test_create_user_post_request.py
:- Imports:
random
,pytest
,requests
,read_csv_data
. endpoint
: Defines the base URL for the user API.@pytest.mark.parametrize(...)
: Decorator that runstest_post_rqst
multiple times with different data from a CSV file.test_post_rqst(name, email, gender, status)
: Sends a POST request to create a user and verifies the response.test_get_rqst()
: Sends a GET request to retrieve the created user.test_put_rqst()
: Sends a PUT request to update the created user.
- Imports:
- Tests are executed using
pytest
, which discovers and runs the test functions intest_*.py
files. - The test functions use the
requests
library to send HTTP requests to API endpoints. - Assertions validate the responses.
- Allure is integrated with pytest to capture test results and generate reports.
- The Allure report can be generated using
allure serve allure-report
command.