Skip to content

ksuzy31-org/api-testing-python

 
 

Repository files navigation

Virta EV Station API Testing (Python)

This application is built to do REST API testing using python scripts along with the use of Pytest module as our testing framework.

License

Demo

Page-Object-Model-Demo-Gif.gif

Languages, libraries and tools used

Above Features are used to make code simple, generic, understandable, clean and easily maintainable for future development.

Installation

Install the dependencies and start the testing.

Install Pytest:

pip3 install --user -U pytest

Install Requests:

pip3 install --user requests

Install Json Path:

pip3 install --user jsonpath

Automated tests

To run a test, you can simply write the following command on Terminal:

pytest

To run and get details of all the executed test, you can simply write the following command on Terminal:

pytest -rA

To run and generate full HTML details report of all the executed test, you can simply write the following commands on Terminal:

But first install Pytest-HTML by writing the following command on Terminal

pip3 install --user pytest-html

Then write the following command on Terminal

pytest --html=YOUR_REPORT_FILE_NAME.html

To see the reports, open the Project window, and then right-click then click on refresh then right-click on StationReport.html to open the file on the default browser.

Page-Object-Model-Demo-Gif.gif


Usage Example

A minimal example using the HTTP layer to fetch station data:

from session import HTTPSession, RequestTypes, Endpoints

params = {"latMin": 60.164101, "latMax": 60.164104, "longMin": 24, "longMax": 25}
status_code, data = HTTPSession.send_request(RequestTypes.GET, Endpoints.STATIONS, params)
print(status_code, type(data))  # e.g., '200', <class 'list'>

Notes:

  • The do_logging flag in params controls request logging. It defaults to True. Set {"do_logging": False} to skip logging.
  • On requests exceptions, send_request returns None.

Logging

  • Logs are written to the console and appended to tests_output.log in the repository root.
  • Tests mock logging to avoid file I/O during runs.
  • There is no rotation; the file grows over time. Delete it if it becomes too large.

Status Code Comparison Note

  • StatusCodes.STATUS_200 is '200' (a string). The custom assert_equal compares stringified values by default to avoid type fragility.
  • For strict type comparisons, pass compare_types=True to assert_equal.

Troubleshooting Test Discovery

If pytest has trouble collecting tests, try targeting the file or specific tests directly:

python3 -m pytest -q tests/test_stations.py
python3 -m pytest -q -k test_send_request_handles_request_exception_and_returns_none
python3 -m pytest -vv

Contributing / Development Notes

  • Prefer behavior-focused tests that mock external effects (network, file I/O).
  • Patch session.requests.get and session.Logger.log_request in tests to ensure determinism and isolation.
  • Keep tests small and readable; avoid mocking internal implementation details unnecessarily.
  • Use the provided constants in test_utils.py for station schema expectations.

License

This project references the Apache-2.0 license. Ensure a LICENSE file is present or review the badge link for details:


Prerequisites

  • Python
  • Any IDE

Built With

  • Python - Language used to build the application.
  • Pycharm - The IDE for writing Automation Test Scripts

About

A Rest-API test automation framework which is build on the top of Python language using PyTest Framework.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • Python 84.1%
  • CSS 15.9%