Skip to content

JapneetSachdeva1/PythonRestApiTesting

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PythonRestApiTesting

Rest Api Testing using Requests Module in Python with PyTest

To test rest-api -

  1. Requests module
  2. PyTest
  3. Python
  4. Allure report
  5. Config.ini
  6. Data Driven Testing
  7. Csv Data

1. Project Structure and Core Components

  • 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 (likely config.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.

    napkin-selection (13)

2. Key Technologies and Concepts

  • 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.

    napkin-selection (14)

3. Breakdown of Key Files and Code Snippets

  • 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.
  • 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.
  • 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 runs test_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.

    napkin-selection (15)

4. Test Execution Flow

  • Tests are executed using pytest, which discovers and runs the test functions in test_*.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.

napkin-selection (16)

About

Rest Api Testing using Requests Module in Python with PyTest

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published