Building a Forkable Pytest API Testing Framework

13th May 2025

With the latest update, I've made several significant enhancements to our testing suite, aimed at bolstering our testing capabilities and streamlining processes. These updates not only extend our test coverage but also improve the clarity and maintainability of our test code.

New Tests for POST Requests

I've implemented both positive and negative test cases for POST requests. These tests enhance our ability to catch potential issues by covering a broader range of scenarios that the application might encounter in real-world usage.

Introduction of GET Test

The new GET test was a milestone, requiring the database to contain an actual entry to successfully retrieve data. This necessity led me to initially create a user within the test itself. Recognizing the potential for reusability, I decided to abstract this function into a separate fixture.

Reusable User Creation Fixture

This new fixture efficiently handles user creation, setting the foundation for more robust and comprehensive testing. By reusing this fixture across multiple tests, not only do we save time, but we also ensure consistency whenever a user needs to be part of a test scenario.

Error Response Model and Constants

As I added negative tests, I recognized the importance of a structured way to validate error messages. Thus, I created an error response model, which helps cleanly validate error messages. To further refine this process, constants were added for these error messages, improving both readability and maintainability.

Documentation and Readme Enhancements

I've started focusing more on documentation, which is crucial for anyone interfacing with our testing framework. Initial steps included adding detailed comments throughout the codebase and updating our README file. These changes ensure that our documentation can guide not only our current team but also future contributors.

These updates lay a solid foundation for further improvements. Over the coming weeks, I plan to continue enhancing our documentation, dive deeper into refining our testing methodology, and incorporate more advanced features. Stay tuned for more enhancements that will aim to fortify our testing framework even further!



17th April 2025

• Enhancing Testing Efficiency with Pytest Fixtures and Hooks

In a recent update, I've introduced several new features to our testing framework that streamline and enhance our test processes. The star of this update is undoubtedly the conftest.py file, which acts as a central hub for managing test fixtures. Here's a glimpse into the changes and their benefits:

• Pytest Fixtures

Fixtures in pytest are a powerful mechanism that allows you to set up a specific environment for each test to run in. They enable the execution of setup code before your tests run and can also include teardown logic for cleanup after tests have completed. By defining shared logic in fixtures, you can maintain clean, organized tests without redundant code. They’re incredibly versatile, supporting parameterization, dependency injection, and fixture scoping to control when and how frequently they are executed.

• Database Connection Fixture

In the current setup, conftest.py includes a fixture that establishes a connection to our database before tests are executed. This ensures that when I need to validate the response payloads against entries in the database, the connection is primed and ready to go. This setup not only saves time but also increases reliability by ensuring that database resources are always correctly initialized for each test.

• Post-Test Cleanup

Another significant feature implemented is a database cleanup hook. By using pytest decorators, I can now mark specific tests for cleanup. After these tests run—successfully or not—all documents in the database are removed. This approach ensures a clean environment before each test session and prevents interference from stale data, enhancing test accuracy and integrity.

• Assertions in a Separate File

Improving readability to further improve the organization of our tests, I've isolated assertions into a separate file. This change keeps individual test scripts cleaner and easier to read, thereby accelerating the process of creating new tests. By centralizing common assertions, we not only maintain consistency but also reduce the likelihood of errors across test cases.

• Future Plans

Further Looking forward, I plan to extend the use of fixtures to include user creation pre-tests. This will allow us to test GET endpoints more effectively by ensuring that the necessary users are in place before tests run. This capability enhances our ability to test state-dependent operations with confidence, ensuring that the application behaves as expected under realistic conditions.


14th April 2025

In today's update, I focused on enhancing the test framework for our API by integrating a dynamic environment management, added test structure, used the previously crated Pydantic model in test and introduced storage for constants. These changes aim to improve maintainability and scalability as we continue to develop and test our application.

• Test Structure

I refined the test structure to categorize tests into different endpoints and further subdivide them into positive and negative scenarios. This organization allows us to cover a broad range of test cases efficiently and ensures comprehensive coverage of our API functionalities. By structuring our tests this way, we can clearly identify the purpose of each test and focus on both expected success conditions and potential failure points.

• Creating User Tests with Pydantic

For testing the creation of a user via a POST request, I utilized Pydantic models to define the user schema. Pydantic provides data validation and parsing, allowing us to ensure that all inputs conform to expected formats before the request is sent. This approach not only improves test reliability but also helps catch errors early in the testing phase.

• Constants Module

Additionally, to maintain a clean and organized framework, I introduced a Constants.py file. This module collects all constants that remain unchanged throughout the tests, such as fixed parameter values. Centralizing constants ensures that our tests are easy to update and understand, reducing the likelihood of errors caused by hardcoded values.


10th April 2025

After setting up a basic FastAPI application with GET and POST endpoints, it's time to lay the groundwork for a strong and effective testing framework. Testing not only ensures that our API works as intended but also provides a safety net for future development. In this post, I'll explore the initial steps for creating a robust testing suite using PyTest, focusing on simplicity and gradual complexity.

• The plan is

1. Start Simple: Write straightforward tests to verify core functionalities, such as making sure an endpoint returns the correct data or status code. Validate that required fields like username and email are functioning as expected.

2. Iterative Complexity: Gradually introduce complexity into my tests. This could involve testing edge cases, such as handling invalid input data, or simulating multiple sequential API calls.

3. Introduce Variables for Dynamism: As I become comfortable, start introducing variables to make My tests more dynamic and reusable. This approach allows tests to automatically adjust to different scenarios without changing the framework, making it far more adaptable to future requirements which is the end goal.


2nd April 2025

I feel that the API automation framework sounds quite intimidating, especially for someone with little coding experience. Would it be nice if there was a simple yet effective framework that everybody can fork and use to test their endpoints? Well, this is what I will be attempting to do.

We will start with PyTest. Used millions of times to do the job and falling in with all the AI buzz around, everybody seems to be going away from JavaScript and into cuddles with the snake (I am a dad, so dad jokes will happen). The main objective here is to keep things simple, so I will be using Pydantic Library, which will allow for rapid model creation and easy authentication.

The first step is to create a FastAPI app with GET and POST endpoints. I will run this in a container, so a Dockerfile will come next... and then well… and then we will see.