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='已成年')
相关推荐
<花开花落>6 天前
将Python第三方库转换为真正的 pytest 插件
pytest
春风又。8 天前
接口自动化——初识pytest
python·测试工具·自动化·pytest
南部余额9 天前
playwright解决重复登录问题,通过pytest夹具自动读取storage_state用户状态信息
前端·爬虫·python·ui·pytest·pylawright
小码哥说测试11 天前
接口自动化进阶 —— Pytest全局配置pytest.ini文件详解!
自动化测试·软件测试·测试工具·自动化·pytest·测试工程师
天才测试猿12 天前
Python常用自动化测试框架—Pytest
自动化测试·软件测试·python·功能测试·测试工具·测试用例·pytest
Rhys..12 天前
2、pytest核心功能(进阶用法)
开发语言·python·pytest
一只天蝎的晋升之路14 天前
Python中常用的测试框架-pytest和unittest
开发语言·python·pytest
明月与玄武14 天前
pytest-xdist 进行高效并行自动化测试
pytest·pytest-xdist·进行高效并行自动化测试
测试笔记(自看)14 天前
Python+Requests+Pytest+YAML+Allure接口自动化框架
python·自动化·pytest·allure
活跃家族14 天前
Jsonpath使用
python·pytest