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 product regularly scans and identifies resource changes that have occurred outside of a Terraform workflow (e.g. drift) so that dev teams can have a Cloud environment that is fully represented as code. All of our tools are self-hosted by our customers, with no data ever leaving their servers. 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