这篇文章上次修改于 1591 天前,可能其部分内容已经发生变化,如有疑问可询问作者。
pytest fixture
可替代s/t 功能,比s/t更为灵活
方法前允许多个
autouse 参数为定义是否自动启用
-setup-show 回溯 fixture 的执行过程
作用域 scope参数(session>module>class>function)
pytest conftest.py 文件的用法
- conftest.py文件名是不能换的
- 放在项目下是全局的数据共享的地方(全局的配置和前期工作都可以写在这里)
- 一般来说,conftest.py是用来存放fixture 方法的
- conftest.py 文件就近生效(如果 在不同的文件夹下都有conftest.py),离测试文件最近的那个conftest.py 生效)
pytest 插件
- 内部插件 :pytest 内置 hook 函数
- 外部插件 :pip install 安装的插件
- 本地插件 :fixture 自定义插件,在conftest.py 文件中
#一些 pytest 插件
pip install pytest-ordering # 控制用例的执行顺序
pip install pytest-dependency # 控制用例的依赖关系
pip install pytest-xdist # 分布式并发执行测试用例
pip install pytest-rerunfailures # 失败重跑
pip install pytest-assume # 多重较验
pip install pytest-random-order # 用例随机执行
pip install pytest-html # 测试报告
pytest 改用例中文编码
在 pytest_collection_modifyitems 中遍历所有用例后将name改编码:
item.name = item.name.encode('utf-8').decode('unicode-escape')
item._nodeid = item.nodeid.encode('utf-8').decode('unicode-escape')
allure
- 安装 java 环境
- 下载 allure 并配置变量
- 安装 allure-pytest 包
allure使用
pytest --alluredir=./res
allure serve res
# 生成 html 版
allure generate