Test and Create an HTTP Endpoint

Before we create the proper setup of the web server, we need to create the endpoint that will be exposed. Endpoints are ultimately run when a user sends a request to a certain URL. This means that we can still work with TDDTest-Driven Development, since the final goal is to have code that produces certain results.

Our objective right now is to test an endpoint while ensuring that our web server remains up and running when we hit the test URLs. The web server itself is an external system, so we won’t test it, but the code that provides the endpoint is part of our application. It’s a gateway interface that allows an HTTP framework to access the use cases.

The extension pytest-flask allows us to run Flask, simulate HTTP requests, and test the HTTP responses. This extension hides a lot of automation. When we install it, some fixtures like client become available automatically, so we don’t need to import them. Moreover, it tries to access another fixture named app. Our first step should be to define this fixture when we begin to create our endpoint.

Implement configuration for testing

Fixtures can be defined directly in our test file, but if we want a fixture to be globally available, the best place to define it is in the conftest.py file. This file is automatically loaded by pytest. As we can see, there’s a lot of automation, which could cause us some confusion if we aren’t aware of it.

Get hands-on with 1400+ tech skills courses.