Pytest测试用例的执行方法

如下是目录结构:
在这里插入图片描述

1、主函数模式

(1)运行所有 pytest.main()
(2)指定模块 pytest.main(’[-vs],’,’./testcase/test_day1.py’)
(3)指定目录pytest.main(’[-vs]’),’./testcase’)
(4)通过nodeid指定用例运行:nodeid由模块名,分隔符,类名,方法名,函数名组成
#pytest.main(["-vs"],’./interface_testcase/test_day3.py::test_demo11’)
#pytest.main(["-vs"],’./interface_testcase/test_day3.py::TestLogin::test_01_qianghong1’)

2、命令行模式

(1)运行所有:pytest
(2)指定模块 pytest -vs ./testcase/test_day1.py
(3)指定目录 pytest -vs ./testcase
(4)通过nodeid指定用例运行:nodeid由模块名,分隔符,类名,方法名,函数名组成
pytest -vs ./interface_testcase/test_day3.py::test_demo11
pytest -vs ./interface_testcase/test_day3.py::TestLogin::test_01_qianghong1

参数详解:
-s:表示输出调试信息,包括print打印的信息
-v显示更详细的信息
-vs一起使用
-n支持多线程或者分布式运行测试用例
#如 #pytest.main([’-vs’,’./testcase/test_day1.py’,’-n=2’])
# pytest -vs ./testcase/test_day1.py -n 2
#reruns==number 表示失败用例重跑
#pytest -vs ./testcase/test_day2.py --reruns 2
#pytest.main([’–vs’,’./testcase/test_day2.py’,‘reruns=2’]) #失败得的用例重跑两次
#-x表示只要一个用例报错,那么测试停止运行
#–maxfail=2 出现两个失败用例停止
#-k 根据测试用例的部分字符串指定测试用例 pytest -vs test_day2 -k “yang”

3、通过读取pytest ini配置文件运行 (最主要运用的方式)

#pytest.ini是pytest单元测试框架中的核心配置文件
(1)位置:一般是放在项目的根目录
(2)编码:必须是ANSI,可以使用notepad++来修改编码格式
(3)作用:改变pytest的默认行为
(4)运行的规则:不管是主函数的模式运行该,命令行模式,都会区读取这个配置文件

[pytest]
addopts=-vs    #命令行参数 用空格分离
testpaths=./testcase #测试用例的路径 ./testcase/test_day2.py #
python_files=test_*.py  #模块名的规则
python_classes=Test*    #类名的规则
python_functions=test   #方法名的规则

#实际项目配置中,尽量删除注释,确保不必要的问题出现

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值