pytest参考资料:
运行测试:
pytest -v
将运行当前目录和子目录中所有格式为 test_*.py 或 *_test.py 的文件。
pytest <文件名> -v
运行指定文件
pytest -k <子集名> -v
这将执行所有名称中含有指定子集名(如一个py中包含的多个类或函数就是子集名) 的测试
pytest -m <标记名> -v
将执行标记为 **others ** 测试,在函数上方标记,语法:@pytest.mark.<自定义的名称,可以是功能分类,如login>
pytest <文件名> -v --maxfail 1
在n次测试失败后立即停止测试套件的执行
将执行结果生成到xml文件中
pytest <文件名> -v --junitxml="result.xml"
生成测试报告
安装插件:pip install pytest-html
运行测试并指定测试报告名称:pytest --html=report.html
运行测试并指定测试报告目录:pytest --html=./report/report.html
设置报告标题:pytest --html=./report/report.html --title="My Test Report"
设置报告头部信息:pytest --html=./report/report.html --header="<h1>My Test Report</h1>"
设置报告底部信息:pytest --html=./report/report.html --footer="<p>Generated by pytest-html</p>"
还可以通过其他配置项来设置样式、截图、饼图和条形图等内容。此外,pytest-html 还支持多语言、多浏览器显示和多种结果类型输出等功能,极大地提高了测试报告生成效率和质量。
并行运行测试
前提条件,先安装pytest-xdis
pytest -n 3 (-n <num> 通过使用多个工作者来运行测试)
标记(在函数上方标记)
@pytest.fixture 将在所有函数之前进行
@pytest.mark.xfail 测试失败或通过,pytest都不会打印测试细节
@pytest.mark.skip 该测试将不被执行
@pytest.mark.aaa 自定义标记
产生测试报告(新建一个py文件,单独写运行脚本和生成报告的代码,便于管理):
from BeautifulReport.BeautifulReport import BeautifulReport
import unittest
import datetime
# 获取当前时间
now = datetime.datetime.now()
# 对当前时间格式化
format_now = now.strftime('%Y-%m-%d-%H%M%S')
'''运行aaa开头的py脚本文件,目前是包含了境内外官网的'''
suite = unittest.defaultTestLoader.discover(start_dir=r'D:\auto\web_auto',pattern='aaa*.py')
bf = BeautifulReport(suite)
bf.report(description='aaa境内外官网ui自动化测试报告', filename=f'{format_now}aaa_website_report.html',
report_dir=r'D:\auto\web_auto\report', theme='theme_cyan')
allure生成测试报告参考资料:
最全的Pytest+Allure使用教程,建议收藏 - 知乎
下载allure:
Releases · allure-framework/allure2 · GitHub
安装python依赖:
pip install allure-pytest -i http://pypi.douban.com/simple --trusted-host pypi.douban.com