pytest运行用例的常见方式及参数

标题pytest运行用例方式及参数

用例结构目录

"""

在最外层目录下执行所有的用例

参数说明:

-s:显示用例的打印信息

-v:显示用例执行的详细信息

--alluredir:指定allure报告的路径

--clean-alluredir:清除allure报告的路径

-n:指定并发的进程数

-x:出现一条用例失败就停止执行

--maxfail:指定最大失败用例数

--reruns:指定用例失败重跑次数

--reruns-delay:指定用例失败重跑的间隔时间

--m:指定用例的标记表达式

-k:(keywords)指定用例的关键字表达式,可以使用and、or、not进行组合.例如用例包含了success,但是不包含failed的用例 '-k','success'

"""

import pytest

# 运行所有用例

pytest.main('-s', '-v')

# 运行指定用例:运行登录用例

pytest.main('-s', '-v', './test_login.py')

print("运行指定模块用例完成***")

# 运行制定的用例模块下的某个用例,使用两个冒号作为分隔符

pytest.main('-s', '-v', './test_login.py::TestLogin::test_login_01')

print("运行指定某个用例完成***")

# 分布式运行 依赖pytest-xdist插件

time_start = time.time()

pytest.main('-s', '-v', './test_login.py')

time_end = time.time()

print("分布式运行总耗时:", time_end - time_start)

print("运行分布式用例前完成***")

# 分布式运行'-n','2' -n 表示使用分布式,后面的数字表示并发的进程数;注意多线程的时候,是按顺序分配。两个线程则是,1.3.5 :2.4.6

time_start = time.time()

pytest.main('-s', '-v', '-n', '2', './test_login.py')

time_end = time.time()

print("分布式运行总耗时:", time_end - time_start)

print("运行分布式用例完成***")

# 失败用例重跑, -reruns 2 表示失败重跑2次 依赖pytest-rerunfailures插件

pytest.main('-s', '-v', '--reruns', '2', './test_login.py')

print("运行失败重跑用例完成***")

# -x 表示出现一条用例失败就停止执行,执行失败了就停止运行

pytest.main('-s', '-v', '-x', './test_login.py')

print("运行用例失败就停止执行完成***")

**************************************************************************************************************

上面是all run 文件运行的命令。也可以在终端里面输入对应的命令执行测试

第三种执行方式:pytest.ini 文件,将需要执行的用例信息配置到pytest.ini文件中,main()中存在其他参数时,还是会先读取配置,这种优先级最高!!!

注意:文件的编码方式需要使用ANSI编码格式

"""

pytest.ini 文件内容如下:

pytest

参数,多个参数使用空格分开

addopts = -v -s --html ./report/report.html

用例路径

testpaths = testcase

用例文件

python_files = test*.py

用例类

python_classes = Test*

用例方法

python_functions = test*

分组执行方式:pytest.ini 文件

markers =

smoke:冒烟测试

login:登录测试

register:注册测试

执行方式:pytest.main('-v','-m','smoke')

执行方式:pytest.main('-v','-m','login')

这里可以修改默认的方式,例如文件是AAA*.py

"""

# 分组执行用例:冒烟,标记smoke ,要配合用例文件中的pytest.mark.smoke使用

pytest.main('-v', '-m', 'smoke')

# 分组执行用例:登录,标记login.smoke,多个分组执行用or 链接

pytest.main('-v', '-m', 'smoke or login')

# 无条件跳过测试用例:pytest.mark.skip(reason="跳过原因") --这种跳过方式,跳过后,用例不执行,但是会显示跳过的结果

pytest.main('-m', 'smoke or login')

有条件跳过测试用例:pytest.mark.skipif(condition=True, reason="跳过原因") --这种跳过方式,跳过后,用例不执行

pytest.main('-v', '-m', 'smoke or login')

相关推荐
2601_962078191 小时前
Appium+Python+pytest自动化测试框架详解
自动化测试·python·appium·pytest·移动应用
2601_962284502 天前
安卓APP UI自动化测试:Python + UiAutomator2 + pytest + pytest-html
python·pytest·uiautomator2·ui自动化测试·安卓app
2601_962300812 天前
基于pytest的接口自动化测试:使用requests模块的实战项目与接口关联方法详解
接口自动化测试·测试用例·pytest·接口关联·requests模块
2601_962300812 天前
Python + Requests + Pytest + Excel + Allure:接口自动化测试项目实战
python·excel·pytest·allure·requests
11路没有终点3 天前
Pytest 安装、版本与第一个测试用例
测试用例·pytest
11路没有终点4 天前
pytest 参数化与 Mark 标记
pytest
2601_962296284 天前
五种Python自动化测试框架汇总,附学习方法
自动化测试·pytest·unittest·robotframework·python框架
11路没有终点5 天前
pytest Fixture 进阶:conftest 与依赖注入
pytest
2601_962387828 天前
web自动化测试实战教程【selenium/unittest/pytest】【共193课
selenium·pytest·devops·web自动化测试·unittest
weixin_440730508 天前
使用pytest中方法控制执行步骤(test_begin.py、test_end.py,@pytest.mark.run(order=1))
开发语言·python·pytest