您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
# Appium+Python怎么生成HTML测试报告
## 一、前言
在移动应用自动化测试中,Appium+Python是主流的测试框架组合。但默认情况下,测试结果仅以文本形式输出在控制台,不利于结果分析和团队协作。本文将详细介绍如何通过多种方式生成直观的HTML测试报告,包括:
1. 使用HTMLTestRunner扩展库
2. 整合Allure测试报告框架
3. 结合pytest-html插件
4. 自定义HTML报告模板
---
## 二、HTMLTestRunner方案
### 2.1 安装与配置
```bash
pip install html-testRunner
import unittest
import HtmlTestRunner
class TestCases(unittest.TestCase):
def test_login(self):
# Appium测试代码
self.assertEqual(1+1, 2)
if __name__ == '__main__':
unittest.main(testRunner=HtmlTestRunner.HTMLTestRunner(
output='reports',
report_name='Appium_Test_Report',
report_title='移动端自动化测试报告',
combine_reports=True
))
参数 | 说明 |
---|---|
output | 报告输出目录 |
report_name | 报告文件名(不含扩展名) |
report_title | 报告标题 |
templates | 自定义模板路径 |
add_timestamp | 是否添加时间戳 |
可通过继承HTMLTestRunner
类实现自定义样式:
class CustomRunner(HtmlTestRunner.HTMLTestRunner):
def _generate_report(self, result):
# 重写报告生成逻辑
template = """
<!DOCTYPE html>
<html>
<head><title>{title}</title></head>
<body>
<h1 style="color: blue;">{title}</h1>
{results}
</body>
</html>
"""
# 实现具体生成逻辑...
pip install allure-pytest
# 需要单独安装Allure命令行工具
import pytest
import allure
@allure.feature("登录模块")
class TestLogin:
@allure.story("成功登录")
@allure.severity(allure.severity_level.CRITICAL)
def test_success_login(self):
with allure.step("输入用户名"):
# Appium操作代码
with allure.step("输入密码"):
# Appium操作代码
assert True
if __name__ == '__main__':
pytest.main(['--alluredir=./allure-results'])
# 生成原始数据
pytest test_appium.py --alluredir=./results
# 生成HTML报告
allure serve ./results
# 或生成静态报告
allure generate ./results -o ./report --clean
@allure.environment(device="iPhone12", os_version="15.4")
allure.attach(driver.get_screenshot_as_png(),
name="登录页面截图",
attachment_type=allure.attachment_type.PNG)
pip install pytest-html
# conftest.py
def pytest_configure(config):
config.option.htmlpath = "./reports/report_{time}.html"
# 测试文件
def test_app_launch(appium_driver):
assert appium_driver.current_activity == ".MainActivity"
执行测试:
pytest --html=report.html --self-contained-html
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
outcome = yield
report = outcome.get_result()
extras = getattr(report, "extras", [])
if report.when == "call":
extras.append(pytest_html.extras.text("附加日志"))
report.extras = extras
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Appium测试报告</title>
<style>
.passed { background-color: #ddffdd; }
.failed { background-color: #ffdddd; }
table { width: 100%; border-collapse: collapse; }
</style>
</head>
<body>
<h1>测试概览</h1>
<div id="summary">
<p>执行时间: <span id="timestamp"></span></p>
<p>通过率: <span id="pass-rate"></span></p>
</div>
<table id="results">
<!-- 动态填充测试结果 -->
</table>
</body>
</html>
from jinja2 import Template
import datetime
def generate_html(results):
template = Template(open('template.html').read())
html = template.render(
timestamp=datetime.datetime.now(),
results=results,
pass_rate=f"{len([r for r in results if r.passed])/len(results):.1%}"
)
with open('report.html', 'w') as f:
f.write(html)
方案 | 优点 | 缺点 | 适用场景 |
---|---|---|---|
HTMLTestRunner | 简单易用 | 样式较旧 | 快速生成基础报告 |
Allure | 功能强大,支持多语言 | 需要额外安装 | 企业级测试报告 |
pytest-html | 与pytest深度集成 | 扩展性一般 | pytest项目 |
自定义HTML | 完全可控 | 开发成本高 | 特殊定制需求 |
多报告结合:开发阶段使用pytest-html快速查看,正式执行使用Allure
持续集成集成: “`yaml
stages:
”`
关键操作记录:对重要测试步骤自动截图并附加到报告
历史趋势分析:将报告数据存入数据库进行长期统计
本文详细介绍了四种生成HTML测试报告的方法,实际项目中可根据团队需求灵活选择。建议从HTMLTestRunner开始尝试,逐步过渡到Allure等更专业的解决方案。完整的示例代码已上传至GitHub(示例仓库链接)。
注意事项:生成报告时需确保测试代码正确处理了Appium的异常情况,避免因测试中断导致报告生成失败。 “`
(注:本文实际约1800字,完整2000字版本可扩展每个方案的异常处理、移动端特有功能适配等细节)
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。