当前位置:网站首页>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自动化测试/测试开发,走向高薪之路。
喜欢软件测试的小伙伴们,如果我的博客对你有帮助、如果你喜欢我的博客内容,请 “点赞” “评论” “收藏” 一 键三连哦!
边栏推荐
- 【mysql知识点整理】--- order by 、group by 出现Using filesort原因详解
- 买了一瓶饮料
- 如何正确地配置入口文件?
- 风电场运营实践 | 麒麟信安助力国华投资山东公司集控中心实现安全智慧化运营
- 【MySQL —— 数据库约束】
- 一个接口并发问题的模拟与复现
- php提示Array to string conversion
- 1686. 石子游戏 VI
- .NET深入解析LINQ框架(四:IQueryable、IQueryProvider接口详解)
- Auto.js special positioning control method cannot perform blocking operations on the ui thread, please use setTimeout instead
猜你喜欢
随机推荐
科捷智能冲刺科创板:年营收12.8亿 顺丰与日日顺是股东
npm运行项目dependencies were not found: core-js/modules/es6.array.fill
Greenplum database failure analysis, can not listen to the port
[NCTF2019]SQLi-1||SQL注入
记一次sql优化Using temporary; Using filesort
一个人的精力
Greenplum数据库故障分析——can not listen port
暴力递归到动态规划 06 (剑指 Offer II 095. 最长公共子序列)
聊聊 Nacos
13-security其他.md
v-if、v-else、v-elseif v-show v-for
Day017 封装
esp32和ros2基础篇草稿-micro-ros-
【遥控器开发基础教程4】疯壳·开源编队无人机-SPI(OLED)
20、商品微服务-web层实现
北路智控上市首日破发:公司市值59亿 募资15.6亿
爆款视频怎么做?这里或许有答案
麒麟信安邀您抢先看 | openEuler 志高远,开源汇智创未来-开放原子全球开源峰会欧拉分论坛最详细议程出炉
向往的生活
random.nextint()详解