Building an Automated Test Project with Jenkins, Allure, and Pytest
This tutorial walks through setting up a Python testing environment, installing required libraries, creating and coding test cases with Selenium, integrating Allure for reporting, and configuring Jenkins to run the tests and publish the results automatically.
In this article we introduce how to use Jenkins, Allure, and Pytest to create a practical test project that automates testing and generates detailed reports.
1. Environment Setup
Before starting, ensure the following prerequisites are met:
- Install Python 3.x and Pip - Install Jenkins and Allure2. Install Required Libraries
We need to install the following libraries: pytest-selenium , pytest-html , and allure-pytest . Run the command:
pip install pytest-selenium pytest-html allure-pytest3. Initialize Project
Create a new Python file named test_sample.py in the project root; this file will hold the test cases.
4. Write Test Cases
Example test case code:
import unittest from selenium import webdriver class TestSample(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.maximize_window() self.driver.get("http://www.google.com") def test_search(self): self.driver.find_element_by_name("q").send_keys("Jenkins") self.driver.find_element_by_name("btnK").click() assert "Jenkins" in self.driver.title def tearDown(self): self.driver.close() if __name__ == "__main__": unittest.main()This test launches Chrome, searches for "Jenkins" on Google, and verifies that the page title contains the keyword.
5. Generate Allure Report
Import Allure and use its @allure.step decorator to define test steps:
import allure @allure.step("Enter keyword '{1}' and click search") def input_search(self, keyword): self.driver.find_element_by_name("q").send_keys(keyword) self.driver.find_element_by_name("btnK").click() @allure.step("Assert title contains '{1}'") def assert_title(self, keyword): assert keyword in self.driver.titleApply the decorators in setUp() and test_search() :
@allure.title("Test search functionality") def test_search(self): keyword = "Jenkins" self.input_search(keyword) self.assert_title(keyword)Run the tests and generate the Allure report with:
pytest test_sample.py --alluredir=allure-resultsThe command creates an Allure report in the allure-results directory.
6. Integrate Jenkins
In Jenkins, install the Allure plugin and HTML Publisher plugin. Create a new job, add a shell build step with the same pytest command, and configure Post‑build Actions to publish the Allure report (path: allure-results/**/.xml ) and the HTML report (directory: allure-report , index page: index.html ).
After configuring, run the job to see the automated test results and reports.
Test Development Learning Exchange
Test Development Learning Exchange
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.