Skip to main content
In this guide, we demonstrate how to set up a Jenkins Pipeline to run automated tests on a Flask application using Pytest. Follow the step-by-step instructions below to integrate your Flask project with Jenkins and ensure your code remains robust with continuous testing.

Setting Up the Jenkins Pipeline

Begin by creating a new pipeline in Jenkins for your Flask project:
  1. In Jenkins, create a new item, name it “Flask Pipeline,” and select Pipeline as the project type.
The image shows a Jenkins dashboard with two pipeline projects listed: "Flask-pipeline" and "HelloWorldPipeline," displaying their last success times and durations. The build queue and executor status are also visible, indicating no builds in the queue and two idle executors.
  1. Configure the build trigger to use the GitHub hook trigger for Git SCM polling.
The image shows a Jenkins interface where a user is creating a new item named "flaskpipeline." Various project types like Freestyle project, Pipeline, and Multi-configuration project are listed as options.
  1. For the pipeline configuration, use a Jenkinsfile stored in the application’s Git repository instead of defining the script directly in Jenkins. This way, Jenkins fetches its instructions from your GitHub repo.
The image shows a Jenkins configuration page for a project named "flaskpipeline," with options for build triggers and pipeline script settings.
  1. Select Pipeline script from SCM, choose Git, and enter the URL of your Git repository. If your repository is private, be sure to provide the appropriate credentials.
The image shows a Jenkins configuration page for setting up a pipeline with a Git repository URL and credential options. The interface includes options to save or apply the configuration.
  1. Specify the branch to build as “main” (or your designated branch) if your repository contains only that branch.
The image shows a GitHub repository page for a project named "jenkins-project" by "kodekloudhub," displaying a list of files and a branch selection dropdown. The repository has one branch, several commits, and contributors listed on the right.
  1. By default, Jenkins will look for a file named Jenkinsfile in the repository’s root directory. If your file is located elsewhere or under a different name, provide the full path accordingly. In our case, the Jenkinsfile is in the root directory.

Exploring the Application Files

Our project is a straightforward Python Flask application. Below is an overview of the key files:

Main Application Code: app.py

Unit Tests: test_app.py

This file sets up the automated unit tests using Pytest.

Dependency File: requirements.txt

This file lists the external dependencies needed to run the Flask application:
Use the command below to install the required packages:

HTML Template: index.html

The user interface for the Flask application is defined in the templates/index.html file. Here is an excerpt including the relevant styling:
Ensure that all files are correctly committed to your repository before triggering the Jenkins pipeline.

Configuring the Jenkins Pipeline with a Jenkinsfile

Below is the initial version of the Jenkinsfile that sets up the stages of the pipeline:
This configuration consists of:
  • Checkout Stage: Retrieves the source code from Git and lists the files.
  • Setup Stage: Installs dependencies as specified in requirements.txt.
  • Test Stage: Executes the tests using Pytest.
After verifying that everything works locally with the tests in test_app.py, commit your changes and push them to your repository using:
Jenkins will pull the code, read the Jenkinsfile, and execute the defined steps. Click on a running build in Jenkins to view the console output, which shows details such as the checkout process, dependency installation, and test results.
The image shows a Jenkins build interface for "flaskpipeline," displaying build details such as start time, duration, and GitHub repository information.
The console output will confirm the following sequence:
  1. The source code is checked out (twice if the default checkout is not skipped).
  2. The file list is printed using ls -ltr.
  3. Dependencies are installed from requirements.txt.
  4. Pytest runs and confirms that all tests pass.

Optimizing the Pipeline: Skipping the Default Checkout

To avoid the redundant default checkout, update your Jenkinsfile with the skipDefaultCheckout() option:
This adjustment ensures that Jenkins performs only a single checkout, executing the steps as defined in the “Checkout” stage. After committing and pushing these changes, your pipeline will run with the optimized configuration. Below is an excerpt of the test output:
This output confirms that the tests have passed and the pipeline is executing as expected.
Integrating continuous testing within your CI/CD pipeline ensures that issues are identified early, making maintenance and scaling more efficient.

Conclusion

By following this approach, you can seamlessly integrate a Flask application with Jenkins to perform automated unit tests using Pytest. This pipeline configuration supports continuous integration and assists in maintaining code quality throughout the development lifecycle. For more details on continuous integration with Jenkins, visit the Jenkins Documentation.
Further reading and additional materials can be found in links below:
Happy coding and testing!

Watch Video

Practice Lab