Complete Unit Testing of a Flask API

Feb 1, 2023Web App Development

Flask API Logo

Motivation

Tech Stack

Setting up Conftest

 

Conftest file

Authentication header fixture:

conftest.py file II

Now we create a test client from the Flask application. This is run with every unit test function, and at the end of a particular unit test, we clear any created data from the databases.

conftest.py III

Writing Unit Tests

Endpoint to test.

We can write a unit test for this endpoint as follows:

Unit test

Running Locally w/Docker + Docker Compose

Docker compose.yml for unit test execution.

Our dockerfile, pytest-dockerfile, looks as follows, and runs pytest with basic code coverage output:

pytest-dockerfile

Running in GitHub Actions

GitHub Action for Running Unit Tests

Conclusion

dragondrop.cloud’s mission is to automate developer best practices while working with Infrastructure as Code. Our flagship OSS product, cloud-concierge, allows developers to codify their cloud, detect drift, estimate cloud costs and security risks, and more — while delivering the results via a Pull Request. For enterprises running cloud-concierge at scale, we provide a management platform. To learn more, schedule a demo or get started today!

    Learn More About Web Development

    Running Remote DB Migrations via GitHub Actions

    Motivation Database migrations are an essential component in the life cycle of any relational database. We needed a way to trigger database migrations in our development and production environments within our existing CI/CD pipelines running in GitHub Actions. We...

    read more