This guide explains setting up a Jenkins Pipeline to automate tests for a Flask application using Pytest.
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.
Begin by creating a new pipeline in Jenkins for your Flask project:
In Jenkins, create a new item, name it “Flask Pipeline,” and select Pipeline as the project type.
Configure the build trigger to use the GitHub hook trigger for Git SCM polling.
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.
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.
Specify the branch to build as “main” (or your designated branch) if your repository contains only that branch.
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.
from flask import Flask, render_template, requestapp = Flask(__name__)# A dictionary to store tasks with an IDtasks = {}task_id_counter = 1@app.route('/', methods=['GET', 'POST'])def index(): global task_id_counter response_text = "" if request.method == 'POST': if 'add_task' in request.form: task_content = request.form.get('task_content') if task_content: tasks[task_id_counter] = task_content task_id_counter += 1 elif 'delete_task' in request.form: task_id_to_delete = int(request.form.get('task_id_to_delete')) tasks.pop(task_id_to_delete, None) return render_template('index.html', tasks=tasks)
Configuring the Jenkins Pipeline with a Jenkinsfile
Below is the initial version of the Jenkinsfile that sets up the stages of the pipeline:
Copy
Ask AI
pipeline { agent any stages { stage('Checkout') { steps { git url: 'https://github.com/kodekloudhub/jenkins-project.git', branch: 'main' sh "ls -ltr" } } stage('Setup') { steps { sh "pip install -r requirements.txt" } } stage('Test') { steps { sh "pytest" } } }}
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:
Copy
Ask AI
git add .git commit -m "initial commit"git push origin main
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 console output will confirm the following sequence:
The source code is checked out (twice if the default checkout is not skipped).
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:
Copy
Ask AI
+ pytest============================= test session starts ==============================platform linux -- Python 3.9.16, pytest-7.4.4, pluggy-1.3.0rootdir: /var/lib/jenkins/workspace/flaskpipelinecollected 3 itemstest_app.py ... [100%]============================= 3 passed in 0.14s ==============================
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.
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: