Test-Driven Development with Python, Flask, and Docker

  • Updated December 10th, 2023
  • v3.5.0
Michael Herman Michael Herman

Learn how to build, test, and deploy a production-grade microservice powered by Python, Flask, and Docker!


In this course, you'll learn how to set up a development environment with Docker in order to build and deploy a microservice powered by Python and Flask. You'll also apply the practices of Test-Driven Development with pytest as you develop a RESTful API.

"Code without tests is broken by design." - Jacob Kaplan-Moss

Various logos sitting around an Octopus

What will you learn?

Select a Part

In this first part, you'll learn how to develop a RESTful API with Python, the Flask web framework, Flask-RESTX, and Postgres. You'll containerize Flask and Postgres to run inside Docker containers and configure pytest in order to practice Test-Driven Development (TDD).

Learning Objectives

  1. Develop a RESTful API with Python, Flask, and Flask-RESTX
  2. Practice Test-Driven Development
  3. Test a Flask app with pytest
  4. Containerize Flask and Postgres inside a Docker container
  5. Run unit and integration tests with code coverage
  6. Check your code for any code quality issues via a linter

In the second part, you'll start by deploying Flask, Gunicorn, and Postgres to Heroku with Docker. You'll then continue building out the RESTful API as you write unit and integration tests. Finally, we'll look at how to document the API with Swagger.

Learning Objectives

  1. Configure GitLab CI for continuous integration and deployment
  2. Speed up a Docker-based CI build with Docker Cache
  3. Deploy Flask, Gunicorn, and Postgres to Heroku with Docker
  4. Parameterize test functions and mock functionality in tests with pytest
  5. Run tests in parallel with pytest-xdist
  6. Configure Flask-Admin with SQLAlchemy models
  7. Document a RESTful API with Swagger/OpenAPI

What do you need to know?

This is not a beginner course. It's designed for the advanced-beginner -- someone with at least six months of web development experience. Before beginning, you should have some familiarity with the following topics. Refer to these resources for more info:

Meet the Author

Michael Herman

Michael Herman

Michael is a software engineer and educator who lives and works in the Denver/Boulder area. He is the co-founder/author of Real Python. Besides development, he enjoys building financial models, tech writing, content marketing, and teaching.

What developers are saying

I completed Test-Driven Development with Python, Flask, and Docker. The course is well-structured and the concepts are taught in a logical order. My main takeaways were being able to set up a RESTful API with Flask; properly containerize my development, testing, and production environments; and use a test-first development workflow. This course does an amazing job showing what proper software development looks like!

I've completed the Test-Driven Development with Python, Flask, and Docker and Deploying a Flask and React Microservice to AWS ECS courses. They both were a hugely valuable learning experience. As a self-taught programmer, having a professional explain in detail modern approaches to application architecture and deployment was incredibly valuable and has saved me countless hours of YouTube rabbit-holes.

I really enjoyed taking the Test-Driven Development with Python, Flask, and Docker course. It covers many valuable topics missed in other courses. I also appreciated the many links to resources to topics that are mentioned and not covered here. Even though my preference is video when it comes to learning, this experienced changed that for me. I will go back for more.

I'm amazed by the quality of the Test-Driven Development with Python, Flask, and Docker course. It exceeded my expectations. You use a lot of great tools that I wish I knew when I started with Flask and Docker.

Test-Driven Development with Python, Flask, and Docker is a great course for learning, not only API development, but the larger environment and workflow behind building a modern, well-tested, CI/CD-enabled API. I don't know of any other course that takes this approach from the ground up and I have found it very valuable to go through the entire workflow. I look forward to doing the next course!

Frequently Asked Questions

What tools and technologies are used in this course?

This course covers a variety of technologies and services:

Core

  1. Python
  2. Flask
  3. Docker
  4. Postgres
  5. Gunicorn
  6. Swagger/OpenAPI

Extensions

  1. Flask-RESTX
  2. Flask-SQLAlchemy
  3. Flask-Admin

Testing and Linting

  1. pytest
  2. Coverage.py
  3. Flake8
  4. Black
  5. isort
  6. HTTPie

Services

  1. GitLab
  2. Heroku

What support does TestDriven.io offer?

Since the courses mimic real-world development, support is provided via Stack Overflow. Helpful users, including the developers of the courses, read and respond to messages on Stack Overflow. If you get stuck and you can't find an answer via Stack Overflow, feel free to reach out via email directly. Just be sure to detail what you've tried. For more, review Support and Consulting.

How long does it take to complete the course?

It's dependent on your current skill level. On average, it takes approximately 10 hours to complete.