pytest:如何调用 pytest

2615 篇文章 26 订阅
2195 篇文章 14 订阅

指定要运行的测试

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自动化测试/测试开发,走向高薪之路。

喜欢软件测试的小伙伴们,如果我的博客对你有帮助、如果你喜欢我的博客内容,请 “点赞” “评论” “收藏” 一 键三连哦!
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值