【pytest】pytest的几种运行方式,尤其最后一种调试很方便

接口自动化测试中,我们在调试bug时候使用当个文件执行的话其实是很费时间的,如果没有上下测试方法依赖关系的话,其实是可以单个执行指定测试方法的,借此总结了几种执行测试用例的方法:

一、执行全部用例

pytest.main()

二、指定模块执行用例

pytest.main('[-vs],','py文件的路径')

例如:

 pytest.main('[-vs],','./testcase/test_demo1.py')  #只运行testcase 下的test_demo1.py 文件

三、指定目录执行用例

pytest.main('[-vs]'),'测试目录的路径')

例如: 

pytest.main('[-vs]'),'./testcase')    #只运行testcase 目录下的文件

四、指定测试类执行用例

pytest  py文件的路径::类名 

例如: 

pytest.main(["-vs"],'./interface_testcase/test_demo1.py::TestLogin')

五、指定测试方法执行用例

pytest py文件的路径::类名::方法名 

例如:

pytest.main(["-vs"],'./interface_testcase/test_demo1.py::TestLogin::test_func01')

六、 使用pytest.mark标记函数

@pytest.mark.标记名   

 执行测试的时候使用 pytest -m 标记名

pytest -m 标记名

参数详解:

-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)运行的规则:不管是主函数的模式运行该,命令行模式,都会区读取这个配置文件

常用参数

addopts 命令行的参数,用空格分隔

testpaths 测试用例的路径

markers 标记参数,赋值方式为 key:value

python_files 模块的命名规则 xx.py

python_classes 类名的命名规则 Xxx

 python_functions 方法的命名规则 **

required_plugins 插件的使用

xfail_strict = true 禁用xpass

猜你喜欢

转载自blog.csdn.net/weixin_43569834/article/details/131292420