This is a test framework for testing API endpoints using Python and Pytest. The tests are executed using an API client to send HTTP requests and validate the responses.
You can access the challenge in the ./design_challenge
directory at the root of this framework.
To run this test framework, you will need to have the following installed on your Windows machine:
- Python 3
- Docker Desktop
There is not much error handling from https://jsonplaceholder.typicode.com
so all of my negative tests fail. In normal circumstances I would not include these but I wanted to include the logic of what these tests could be like.
Further to this I would include unit testing for the api_client class to ensure ensure that the code was production ready.
The framework has two core directories, api_tests
containing all api tests and utils
which houses all the exception classes.
At the root of the test framework, we have the ApiClient which makes use of OOP, allowing the tests to reuse requests.
The conftest.py
contains the fixtures for the framework, such as the baseUrl
.
As stated below this framework makes use of Docker, as well as the implmentation of CircleCI.
- Clone the repository to your local machine.
- Open a command prompt or PowerShell terminal in the root directory of the project.
- Create a virtual environment for the project by running the following command:
python -m venv venv
- Activate the virtual environment by running the following command:
venv\Scripts\activate
- Install the required Python packages by running the following command:
pip install -r requirements.txt
- To run the tests using Pytest, activate the virtual environment by running the following command:
pytest -s
Pytest will discover and execute all the tests in the api_tests
directory. The test results will be displayed in the terminal.
- Build the Docker image for the API client by running the following command:
docker build -t api_client .
- Run the tests by running the Docker container based on the image you just built:
docker run api_client
To stop and clean up all your Docker resources, you can use the following commands:
- Stop all running containers:
docker stop $(docker ps -aq)
- Remove all stopped containers:
docker rm $(docker ps -aq)
- Remove all images:
docker rmi $(docker images -q)
You can access the CircileCI builds here https://app.circleci.com/pipelines/github/monochromecyan
. Permission will need to be granted by the author to view them.
The API client is configured to use the https://jsonplaceholder.typicode.com as the base URL for the API requests. If you need to test a different API, you can modify the base_url
parameter in the ApiClient
constructor.
If you get this error when trying to run the Docker build command, make sure that Docker Desktop is installed and running on your machine. You can download Docker Desktop from the official website at https://www.docker.com/products/docker-desktop.
If you get this error when trying to run the Pytest command, make sure that Pytest is installed in the virtual environment. You can install it by running the following command:
pip install pytest