pytest 使用(一)

pytest

b站课程链接

使用pytest,默认的测试用例的规则以及基础应用

1.模块名必须以test_开头或者_test结尾

2.测试类必须以Test开头,并且不能有init方法。

3.测试方法必须以test开头

pytest测试用例的运行方式

1.主函数模式

(1)运行所有:pytest.main()

(2)指定模块:pytest.main(['-vs','test_login.py'])

(3)指定目录:pytest.main(['-vs','./interface_testcase'])

(4)通过nodeid指定用例运行:nodeid由模块名,分隔符,类名,方法名,函数名组

成。
pytest.main(['-vs','./interface_testcase/test_interface.py::test_04_func']
pytest.main(['- vs','./interface_testcase/test_interface.py::TestInterface::test_03_zhiliao'])

2.命令行模式

(1)运行所有:pytest

(2)指定模块:pytest -vs test_login.py

(3)指定目录:pytest -vs ./interface_testcase

(4)指定目录:pytest -vs ./interface_testcase/test_interface.py::test_04_func

参数详解:

-s:表示输出调试信息,包括print打印的信息

-v:显示更详细的信息

-vs:这两个参数一起用

-n:支持多线程或者分布式运行测试用例。

如:pytest -vs ./testcase/test_login.py -n 2

--reruns NUM:失败用例重跑

-x:表示只要要一个用例报错,那么测试停止。

--maxfail=2 出现两个用例失败就停止。

-k:根据测试用例的部分字符串指定测试用例。

如:pytest -vs ./testcase -k "ao"

--html ./report/report.html:生成html的测试报告。

3.通过读取pytest.ini全局配置文件运行。

pytest.ini这个文件它是pytest单元测试框架的核心配置文件。

1.位置:一般放在项目的根目录

2.编码:必须是ANSI,可以使用notpad++修改编码格式。

3.作用:改变pytest默认的行为。

4.运行的规则;不管是主函数的模式运行,命令行模式运行,都会去读取这个配置文件。

bash 复制代码
[pytest]
addopts = -vs #命令行的参数,用空格分隔
testpaths = ./testcase #测试用例的路径
python_files = test_*.py #模块名的规则
python_classes = Test* #类名的规则
python_functions = test #方法名的规则
markers =
smoke:冒烟用例
usermanage:用户管理模块
productmanage:商品管理模块

pytest执行测试用例的顺序是怎样的呢?

unittest:ascII的大小来绝对的执行的顺序

pytest:默认从上到下

改变默认的执行顺序:使用mark标记。

@pytest.mark.run(order=3)

如何分组执行(冒烟,分模块执行,分接口和web执行)

smoke:冒烟用例,分布在各个模块里面

pytest -m "smoke"

pytest -m "smoke or usermanage or productmanage"

pytest跳过测试用例

(1)无条件跳过

bash 复制代码
@pytest.mark.skip(reason="xxx")

(2)有条件跳过

bash 复制代码
@pytest.mark.skipif(age>=18,reason='已成年')
相关推荐
姚青&1 天前
Pytest 测试用例编写
测试用例·pytest
Warren981 天前
Pytest Fixture 作用域与接口测试 Token 污染问题实战解析
功能测试·面试·单元测试·集成测试·pytest·postman·模块测试
serve the people2 天前
python环境搭建 (七) pytest、pytest-asyncio、pytest-cov 试生态的核心组合
开发语言·python·pytest
真智AI3 天前
用 LLM 辅助生成可跑的 Python 单元测试:pytest + coverage 覆盖率报告(含运行指令与排坑)
python·单元测试·pytest
Warren983 天前
Allure 常用装饰器:实战用法 + 最佳实践(接口自动化)
运维·服务器·git·python·单元测试·自动化·pytest
Warren984 天前
Pytest Fixture 到底该用 return 还是 yield?
数据库·oracle·面试·职场和发展·单元测试·pytest·pyqt
Warren985 天前
Pytest Fixture 作用域详解:Function、Class、Module、Session 怎么选
面试·职场和发展·单元测试·pytest·pip·模块测试·jira
我的xiaodoujiao6 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 44--将自动化测试结果自动推送至钉钉工作群聊
前端·python·测试工具·ui·pytest
我的xiaodoujiao6 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 45--生成项目需要的requirements.txt依赖文件
python·学习·测试工具·pytest
月明长歌6 天前
全栈测试修炼指南:从接口策略到 Python+Pytest+Allure 企业级架构
python·架构·pytest