Learn How to Test Flask Applications with Pytest
Tags: python, tutorial, devops, testing
Published: 2023-03-22 06:17:35

Welcome to this tutorial on how to test Flask applications with Pytest. Flask is a popular web framework in Python that allows developers to build web applications quickly and easily. However, as your application grows in complexity, ensuring that your code is reliable and bug-free becomes increasingly important. That's where testing comes in. Pytest is a powerful and popular testing framework in Python that makes it easy to write and run tests for your Flask application. In this tutorial, you will learn how to automate tests for your Flask application using Pytest, and how to ensure that your code is robust and error-free. So let's get started!

## Prerequisites
- [Free Harness account](https://app.harness.io/auth/#/signup/?module=ci&utm_source=internal&utm_medium=social&utm_campaign=devadvocacy&utm_content=pavan_python_article&utm_term=get-started) to automate your tests
- Sample Flask application. We have already created a [sample Flask application](https://github.com/pavanbelagatti/flask-application); feel free to fork the same and use.

## Tutorial
First, sign in to your Harness account. Harness is a continuous delivery platform. 
![signup Harness](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6lyk0wxigi9a2kqak2fi.png)

Get started with creating your first pipeline
![first pipeline](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v6rgri1gpcbmbt8q2jx1.png)

Authenticate with your GitHub account as your application code is present there.
![add github](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pcdkkrm132wok2r37his.png)

![authenticate with GitHub](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v1efbg84t05szpe9lh1c.png)

Select your application repository. The Flask application you forked above.
![select repo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/got6n1t8i50rlsrlaicc.png)

Select 'Python' from the list and start configuring the pipeline. The right side in the below screenshot you see is a default yaml configuration of your pipeline. 
![configure pipeline](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j8xl2kfvazyvroa9li36.png)

Once you continue, you will land on the pipeline studio which looks as below
![pipeline studio](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l32asp8ywxhk2bc167bo.png)

Click on 'Build Python App' and you should see the step configured under the execution.
![build python app](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/edrypo47lrh447x7yh26.png)

When you click on the 'Build Python App' step under execution, you will see the commands used.
![build python](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fddgjmb82on8m6f9ha45.png)

Don't change anything. Apply changes, save and run the pipeline.
![run pipeline](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dlz4u5j8fy48zpv28yop.png)

You should see a successful pipeline execution:)
![pipeline execution](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1wv06fm3qlx4x22djxn3.png)

Your pipeline is all automated to run tests whenever any developer pushes code to the main branch.
You can verify this by going to the 'Triggers' tab from the pipeline studio.
![Triggers tab](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/77vc098k76bzl4hefahf.png)

You can see push and pull triggers already configured for you.
![pull and push triggers](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2rv41aa068wui3px73nk.png)

Let's push some code to the main branch.
As soon as you push the code, the build gets triggered and you can see the same below.
![build triggered](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yx488sz92qlnnbl9xuzo.png)

**_Congratulations!_** We just configured an automated CI pipeline for our Flask application.

This way, you can easily automate your tests and complete pipeline tasks. Also, you can see who pushed the last changes and what code they pushed. 

**Check out my other article to learn about automating tests for your Node.js applications.** 
{% embed https://dev.to/pavanbelagatti/automating-tests-for-your-nodejs-application-5hd9 %}