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'])

相关推荐
not coder20 小时前
Pytest Fixture 详解
数据库·pytest
not coder1 天前
pytest 常见问题解答 (FAQ)
开发语言·python·pytest
程序员的世界你不懂1 天前
(1)pytest简介和环境准备
pytest
not coder1 天前
Pytest Fixture 是什么?
数据库·oracle·pytest
Tester_孙大壮1 天前
pytest中的元类思想与实战应用
pytest
<花开花落>1 天前
pytest 中 fixture 与类继承交互导致的问题
python·pytest
Julyyyyyyyyyyy1 天前
【软件测试】测试框架(unittest/pytest)
测试工具·pycharm·自动化·pytest
not coder2 天前
pytest 常用命令参数
pytest
not coder6 天前
Pytest 是什么
数据库·pytest
程序猿阿伟9 天前
《深入剖析:Python自动化测试框架之unittest与pytest》
开发语言·python·pytest