当前位置:网站首页>pytest:如何调用 pytest
pytest:如何调用 pytest
2022-08-03 00:39:00 【爱吃 香菜】
指定要运行的测试
pytest 支持多种从命令行运行和选择测试的方法。
模块中运行测试( .py文件 )
pytest sample_class_test.py
在目录中运行测试
pytest cases
通过关键字(测试类名、测试方法名 或 测试函数名)运行测试
pytest -k "TestDemo" 或 pytest -k "test_sample_one"
按节点运行测试
命令行中指定测试函数运行
pytest sample_demo_test.py::test_sample_one
按节点运行测试–命令行中指定测试方法运行
pytest sample_demo_test.py::TestDemo::test_sample_one
通过标记运行测试
这里要引用装饰标记。就像这样:
@pytest.mark.test
def test_sample_two(self):
x = "hello"
assert hasattr(x, "check")
命令行运行时要指定标签名。就像这样:
pytest -m test
从包运行测试
如果你的文件夹中必须未包含__init__.py文件,运行就会抛出这样的异常:
ERROR: module or package not found: cases.sample_class_test.py (missing __init__.py?)
因为是一个文件夹,并非指定的包(package),所以才提示缺少了__init__.py文件。补上文件之后再运行,就像下面这样:
PS E:\git_code\python-code\pytestProject> pytest -q
Test session starts (platform: win32, Python 3.10.1, pytest 7.1.2, pytest-sugar 0.9.5)
rootdir: E:\git_code\python-code\pytestProject
plugins: allure-pytest-2.9.45, anyio-3.4.0, html-3.1.1, metadata-2.0.1, sugar-0.9.5
cases/sample_class_test.py ** 33% ███▍
cases/sample_demo_test.py ** 67% ██████▋
cases/sample_needsfiles_test.py * 83% ████████▍
cases/sample_test.py *
获取版本、选项名称、环境变量的帮助
获取pytest当前版本
pytest --version
获取pytest内置装置
pytest --fixtures
获取pytest的帮助信息
pytest -h 或 pytest --help
测试执行持续时间
获得超过 0.1 秒的最慢 10 个测试持续时间的列表:
pytest --durations=10 --durations-min=0.1
在你的控制台显示的运行结果中会包含这样一段代码。就像这样:
(10 durations < 0.1s hidden. Use -vv to show these durations.)
调用pytest的其他方式
可以从命令行通过python解释器调用测试:
python -m pytest -v -q
这就是利用python直接调用命令运行脚本,调用时会把目录添加到python中的sys.path中。
从python代码中调用pytest
pytest您可以直接从 python 代码调用:
import pytest
def main():
pytest.main(
[
"-q", "./cases",
'--html=./report/report.html',
'--alluredir=./report/allure/allure-report', "--clean-alluredir"
]
)
if __name__ == '__main__':
main()
pytest.main()就是从命令行调用pytest一样,你可以在里面传入选项和参数,选项必须放在列表中。-q是简化输出信息,./cases是测试用例名录,–html是生成html报告,–alluredir是生成allure的json文件,–clean-alluredir是清除allure以前生成的一些脏数据
pytest.main()可以指定pytest插件:
import sys
import pytest
class MyHello:
def pytest_addoption(self, parser):
parser.addoption("--host", action="store", default="test", choices=["test", "pre", "pro"])
def main():
pytest.main(
[
"-q", "./cases",
'--html=./report/report.html',
'--alluredir=./report/allure/allure-report', "--clean-alluredir",
],
plugins=[MyHello()]
)
if __name__ == '__main__':
sys.exit(main())
MyHello是我们写的pytest插件类,插件名称必须是pytest开头,不然会识别不到的,parser.addoption是自定义添加一些自己需要的命令。然后在pytest.main()中指定插件类名称,会自动寻找我们定义的插件并运行起来,sys.exit是退出python程序。
对于插件的编写,通常是写在一个固定名称为conftest.py文件中,如有引用插件名称,在程序运行时pytest会自动寻找插件,这种就会方便很多。当然,你可以编写功能强大的公用插件供其他人使用。
今天先聊到这里吧,以上总结或许能帮助到你,或许帮助不到你,但还是希望能帮助到你。
现在我邀请你进入我们的软件测试学习交流群:【746506216
】,备注“入群”, 大家可以一起探讨交流软件测试,共同学习软件测试技术、面试等软件测试方方面面,还会有免费直播课,收获更多测试技巧,我们一起进阶Python自动化测试/测试开发,走向高薪之路。
喜欢软件测试的小伙伴们,如果我的博客对你有帮助、如果你喜欢我的博客内容,请 “点赞” “评论” “收藏” 一 键三连哦!
边栏推荐
- 一个接口并发问题的模拟与复现
- 10-security登录
- minio 单机版安装
- 【软考 系统架构设计师】软件架构设计① 软件架构的概念
- 阿里云增强版实人认证--银行卡要素核验
- 有奖提问|《新程序员》专访“Apache之父”Brian Behlendorf
- 麒麟信安邀您抢先看 | openEuler 志高远,开源汇智创未来-开放原子全球开源峰会欧拉分论坛最详细议程出炉
- npm运行项目dependencies were not found: core-js/modules/es6.array.fill
- js垃圾回收机制
- 如何快速对接淘宝开放平台API接口(淘宝店铺订单明文接口,淘宝店铺商品上传接口,淘宝店铺订单交易接口)
猜你喜欢
【图像分类】2021-EfficientNetV2 CVPR
开源聚力,共创未来 | 麒麟信安祝贺openKylin首个体验版正式发布!
JSP第一篇 -----JSP九大内置对象(隐式对象)和四大域对象
聊聊 Nacos
全栈---CORS
torchvision.datasets.ImageFolder使用详解
SAP ABAP OData 服务如何支持修改(Update)操作试读版
【问题征集】向 iPod 之父、iPhone 联合设计者、Google Nest 创始人 Tony Fadell 提问啦
【深度学习】基于tensorflow的小型物体识别训练(数据集:CIFAR-10)
智能合约安全-可重入攻击(SW107-Reentrancy)
随机推荐
matlab常微分方程在传染病建模中的应用
VS Code 这么牛,再次印证了一句名言
做快乐的事情
一个接口并发问题的模拟与复现
Greenplum数据库故障分析——can not listen port
作业8.2 线程同步互斥机制——互斥锁
微信小程序--》条件与列表渲染以及WXSS模板样式
Vite教程 安装
Flink / Scala - 使用 CountWindow 实现按条数触发窗口
鲲鹏devkit开发套件
Day017 封装
PAT甲级 1051 Pop Sequence
聊聊 Nacos
XSS攻击
流程控制for和while循环语句
npm运行项目dependencies were not found: core-js/modules/es6.array.fill
【飞控开发高级教程2】疯壳·开源编队无人机-遥控整机代码走读、编译与烧写
线性DP
开源聚力,共创未来 | 麒麟信安祝贺openKylin首个体验版正式发布!
基于rt-thread studio的STM32裸机开发——LED