How to Customize Enterprise Test Reports Using Pytest and Allure
This article explains why customized test report templates are needed in enterprise API automation, and provides step‑by‑step instructions for customizing Allure reports—including logo replacement, color schemes, layout tweaks, custom tags, dynamic titles, Chinese localization, and automated generation and email distribution using Pytest.
In enterprise‑level API automation testing, customized test reports improve efficiency and professional image. Using Pytest and Allure, you can create highly tailored report templates that meet specific corporate requirements.
Why a customized report template?
Brand consistency: add company logo and custom styles to enhance internal and external presentation.
Team collaboration: custom styles align with project characteristics, making it easier to distinguish reports for different projects.
Personalized display: adjust colors, fonts, and layout to fit business needs.
Allure report customization features
Allure supports replacing the default logo, customizing color schemes, adjusting fonts and layout, and adding custom tags and descriptions.
Implementation steps
1. Modify Logo
Allure’s default logo is located in the frontend directory, typically at:
allure-2.x.x/plugins/behaviors-plugin/static/logo.svgReplace this file with your own logo, ensuring the format and size match the original.
2. Customize color scheme
Edit the styles.less file under the allure-report directory to define custom colors, fonts, etc., then regenerate the report:
allure generate ./results -o ./report --clean3. Add custom tags and descriptions
Use Allure decorators in your test code:
import allure
@allure.feature('用户模块')
@allure.story('用户登录')
@allure.description('测试用户登录功能')
def test_user_login():
# test code
passYou can also add links and attachments:
@allure.link('https://example.com', name='测试链接')
@allure.attachment.file('path/to/file', attachment_type=allure.attachment_type.TEXT)
def test_with_link_and_attachment():
# test code
pass4. Dynamic module name and test title
Configure pytest.ini to control test discovery:
[pytest]
addopts = -v -s --alluredir ./report/results --clean-alluredir
testpaths = ./tests
python_files = test_*.py
python_classes = Test*
python_functions = test_*Set a dynamic title inside the test function:
import allure
def test_dynamic_title():
allure.dynamic.title('动态测试标题')
# test code
pass5. Set report language to Chinese
Generate the report with the locale option:
allure generate ./results -o ./report --clean -l zh-CN6. Automatic report generation and email sending
Run Pytest and generate the report programmatically:
import pytest
import os
if __name__ == "__main__":
pytest.main(["-s", "./tests", "--alluredir=./report/results"])
os.system('allure generate ./report/results -o ./report/html --clean')Send the generated report via email using Python’s smtplib and email.mime modules:
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
def send_email(file_path):
msg = MIMEMultipart()
msg['From'] = '[email protected]'
msg['To'] = '[email protected]'
msg['Subject'] = '测试报告'
with open(file_path, 'rb') as f:
attachment = MIMEText(f.read(), 'base64', 'utf-8')
attachment['Content-Type'] = 'application/octet-stream'
attachment['Content-Disposition'] = 'attachment; filename="report.html"'
msg.attach(attachment)
server = smtplib.SMTP('smtp.example.com')
server.login('[email protected]', 'password')
server.sendmail('[email protected]', ['[email protected]'], msg.as_string())
server.quit()By leveraging Pytest and Allure’s powerful features, you can achieve highly customized test reports that satisfy enterprise needs, enhance professionalism, and improve team collaboration and project management efficiency.
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.