深入理解 pytest.main():Python 测试框架的核心功能解析

前言

笔者平常运行pytest用例时,通常使用命令行方式,像这样

pytest -v pxl/test_dir/test_demo.py::TestDemo::test_my_var,执行某一条case,但每次命令行敲也挺麻烦的。那如何在python代码中调用pytest呢?带着疑问一起看。

解答疑问

main()函数中调用pytest.main()就可以啦。

我们深入探讨一下 pytest.main 的用法和一些常见的应用场景。

什么是pytest.main

pytest.main 是 Pytest 框架中一个非常实用的函数,它用于从命令行运行测试集或者以编程方式运行测试。通过调用 pytest.main() 函数,我们可以在代码中启动测试过程,并获取测试结果或者进行进一步的操作。

如何使用 pytest.main 运行测试用例?

我们看一个简单示例:

test_demo.py

import pytest
​
def test_add():
    assert 1 + 1 == 2
​
if __name__ == "__main__":
    pytest.main([__file__])

在这个示例中,我们定义了一个简单的测试用例 test_add,用于检查加法运算的正确性。然后,我们通过 pytest.main([__file__]) 来执行当前文件中的测试用例。

要运行这个测试用例,只需在命令行中执行以下命令:

python test_demo.py

Pytest 将会自动发现并执行 test_add 测试用例,并生成测试结果报告。

源码定义

def main(
    args: Optional[Union[List[str], "os.PathLike[str]"]] = None,
    plugins: Optional[Sequence[Union[str, _PluggyPlugin]]] = None,
) -> Union[int, ExitCode]:
    """Perform an in-process test run.
​
    :param args: List of command line arguments.
    :param plugins: List of plugin objects to be auto-registered during initialization.
​
    :returns: An exit code.
    """

可以看到有两个参数,args 传一个list对象,list 里面是多个命令行的参数;plugins 传一个list对象,list 里面是初始化的时候需注册的插件

常用参数

  • -m <标记>--mark=<标记>:只运行被标记为 <标记> 的测试用例。
  • -k <关键字表达式>--keyword=<关键字表达式>:只运行名称中包含 <关键字表达式> 的测试用例。
  • -s--capture=no:禁止捕获标准输出和标准错误,将其直接打印到控制台。
  • -v--verbose:以详细模式运行测试,显示更多的信息,如每个测试用例的名称、结果和错误信息。
  • --collect-only:只收集测试用例但不执行它们,可以用于查看测试集中的所有可用测试。
  • --ignore:忽略某个测试模块
  • -q: 简单输出模式, 不输出环境信息
  • -x: 出现一条测试用例失败就退出测试。在调试阶段非常有用,当测试用例失败时,应该先调试通过,而不是继续执行测试用例。

这些参数可以单独使用,也可以组合使用。例如,可以同时使用 -m-k 参数来指定运行特定标记和关键字的测试用例。像这样

pytest.main(["-s", "-v", "-m", "p0"])

举例说明

-m
import pytest
​
@pytest.mark.p0
def test_addition():
    assert 2 + 2 == 4
​
@pytest.mark.p1
def test_subtraction():
    assert 5 - 3 == 2
​
if __name__ == "__main__":
    pytest.main(["-m", "p0"])

在这个示例中,我们使用 @pytest.mark 装饰器为两个测试用例标记了不同的标记。然后,我们在 pytest.main 函数调用时传递了 -m p0 参数,表示只运行被标记为 p0 的测试用例。这样就可以只运行 test_addition 测试用例。

-k
import pytest
​
def test_addition():
    assert 2 + 2 == 4
​
def test_subtraction():
    assert 5 - 3 == 2
​
def test_multiplication():
    assert 2 * 3 == 6
​
if __name__ == "__main__":
    pytest.main(["-k", "addition"])

在这个示例中,我们定义了三个测试用例,分别测试加法、减法和乘法运算的正确性。然后,我们在 pytest.main 函数调用时传递了 -k addition 参数,表示只运行名称中包含 "addition" 关键字的测试用例。这样就可以只运行 test_addition 测试用例。

--ignore

test_demo.py

import pytest
​
def test_addition():
    assert 2 + 2 == 4
​
def test_subtraction():
    assert 5 - 3 == 2
​
if __name__ == "__main__":
    pytest.main(["--ignore=test_contract_controller.py"])

在这个示例中,我们定义了两个测试用例 test_additiontest_subtraction。然后,我们在 pytest.main 函数调用时传递了 --ignore=test_contract_controller.py 参数,表示忽略名为 test_contract_controller.py 的测试模块,不运行其中的测试用例。

--durations
import pytest
import time
​
def test_slow_function():
    time.sleep(2)
    assert True
​
def test_fast_function():
    assert True
​
if __name__ == "__main__":
    pytest.main(["--durations=5"])

在这个示例中,我们定义了两个测试用例,test_slow_functiontest_fast_function,分别模拟了一个耗时较长和一个耗时较短的测试。然后,我们在 pytest.main 函数调用时传递了 --durations=5 参数,表示输出最慢的 5 个测试用例的执行时间。这样可以帮助我们识别出执行时间较长的测试用例,进行性能优化或者重点关注。

--collect-only
import pytest
​
def test_addition():
    assert 2 + 2 == 4
​
def test_subtraction():
    assert 5 - 3 == 2
​
if __name__ == "__main__":
    pytest.main(["--collect-only"])

我们在 pytest.main 函数调用时传递了 --collect-only 参数,这样只会收集用例,而不会执行。

运行指定用例

运行指定目录下的case
pytest.main(["--collect-only", "/panda-test/peilian/test_dir/"])
运行指定模块下的case
pytest.main(["--collect-only", "/panda-test/peilian/test_dir/test_demo.py"])
运行指定的case
pytest.main(["--collect-only", "/panda-test/peilian/test_dir/test_demo.py::test_01"])

plugins参数

我们自定义的插件放到 conftest.py 会被pytest查找到,如果不是写到conftest.py 的插件内容,可以通过 plugins 参数指定加载。我们看个案例:

import pytest
​
​
class MyPlugin(object):
    def pytest_sessionstart(self):
        print("*** test run start ***")
​
​
def test_division():
    assert 10 / 2 == 5
​
​
if __name__ == "__main__":
    pytest.main(["/panda-test/peilian/test_dir/test_demo.py::test_division"], plugins=[MyPlugin()])
​

这里又涉及到一个新的钩子函数pytest_sessionstart,先不用管,之后写一篇文章来介绍。我们运行case,会发现在执行最开始会输出*** test run start ***。说明已经加载插件成功了。

plugins参数的作用就是指定需加载的插件,也可以指定多个。

pytest.main 是一个非常有用的函数,它允许我们以编程的方式执行测试用例。通过灵活运用 pytest.main,我们可以更好地集成测试流程和自动化工具,提高测试效率和质量。

最后:下面是配套学习资料,对于做【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!【100%无套路免费领取】

软件测试面试小程序

被百万人刷爆的软件测试题库!!!谁用谁知道!!!全网最全面试刷题小程序,手机就可以刷题,地铁上公交上,卷起来!

涵盖以下这些面试题板块:

1、软件测试基础理论 ,2、web,app,接口功能测试 ,3、网络 ,4、数据库 ,5、linux

6、web,app,接口自动化 ,7、性能测试 ,8、编程基础,9、hr面试题 ,10、开放性测试题,11、安全测试,12、计算机基础

  全套资料获取方式:点击下方小卡片自行领取即可

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值