pytest框架进阶自学系列 | scope参数

书籍来源:房荔枝 梁丽丽《pytest框架与自动化测试应用》

一边学习一边整理老师的课程内容及实验笔记,并与大家分享,侵权即删,谢谢支持!

附上汇总贴:pytest框架进阶自学系列 | 汇总_热爱编程的通信人的博客-CSDN博客


scope参数声明argnames中参数的作用域,并通过对应的argvalues实例划分测试用例,进而影响测试用例的收集顺序。

module级别

如果我们显式地指明scope参数,例如,将参数作用域声明为模块级别,这样设置后测试方法会进行一起统筹,也就是执行的顺序是先执行所有测试方法的第一组数据,再整体执行第二组数据,直到执行完成。

代码如下:

import pytest

@pytest.mark.parametrize('test_input, expected', [(1,2),(3,4)], scope='module')
def test_scope1(test_input, expected):
    pass

@pytest.mark.parametrize('test_input, expected', [(1,2),(3,4)], scope='module')
def test_scope2(test_input, expected):
    pass

执行结果如下:

D:\SynologyDrive\CodeLearning\WIN\pytest-book\venv\Scripts\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2022.1.3\plugins\python-ce\helpers\pycharm\_jb_pytest_runner.py" --path D:/SynologyDrive/CodeLearning/WIN/pytest-book/src/chapter-4/test_scope.py
Testing started at 9:58 ...
Launching pytest with arguments D:/SynologyDrive/CodeLearning/WIN/pytest-book/src/chapter-4/test_scope.py in D:\SynologyDrive\CodeLearning\WIN\pytest-book\src\chapter-4

============================= test session starts =============================
platform win32 -- Python 3.7.7, pytest-5.4.1, py-1.11.0, pluggy-0.13.1 -- D:\SynologyDrive\CodeLearning\WIN\pytest-book\venv\Scripts\python.exe
cachedir: .pytest_cache
rootdir: D:\SynologyDrive\CodeLearning\WIN\pytest-book\src\chapter-4, inifile: pytest.ini
collecting ... collected 4 items

test_scope.py::test_scope1[1-2] PASSED                                   [ 25%]
test_scope.py::test_scope2[1-2] PASSED                                   [ 50%]
test_scope.py::test_scope1[3-4] PASSED                                   [ 75%]
test_scope.py::test_scope2[3-4] PASSED                                   [100%]

============================== 4 passed in 0.01s ==============================

Process finished with exit code 0

当未将scope设置为module时,默认的收集顺序是按测试方法的先后执行的。也就是先执行第一个测试方法中的所有数据,再执行第二测试方法中的所有数据。

import pytest

@pytest.mark.parametrize('test_input, expected', [(1,2),(3,4)])
def test_scope1(test_input, expected):
    pass

@pytest.mark.parametrize('test_input, expected', [(1,2),(3,4)])
def test_scope2(test_input, expected):
    pass
D:\SynologyDrive\CodeLearning\WIN\pytest-book\venv\Scripts\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2022.1.3\plugins\python-ce\helpers\pycharm\_jb_pytest_runner.py" --path D:/SynologyDrive/CodeLearning/WIN/pytest-book/src/chapter-4/test_scope.py
Testing started at 9:58 ...
Launching pytest with arguments D:/SynologyDrive/CodeLearning/WIN/pytest-book/src/chapter-4/test_scope.py in D:\SynologyDrive\CodeLearning\WIN\pytest-book\src\chapter-4

============================= test session starts =============================
platform win32 -- Python 3.7.7, pytest-5.4.1, py-1.11.0, pluggy-0.13.1 -- D:\SynologyDrive\CodeLearning\WIN\pytest-book\venv\Scripts\python.exe
cachedir: .pytest_cache
rootdir: D:\SynologyDrive\CodeLearning\WIN\pytest-book\src\chapter-4, inifile: pytest.ini
collecting ... collected 4 items

test_scope.py::test_scope1[1-2] PASSED                                   [ 25%]
test_scope.py::test_scope1[3-4] PASSED                                   [ 50%]
test_scope.py::test_scope2[1-2] PASSED                                   [ 75%]
test_scope.py::test_scope2[3-4] PASSED                                   [100%]

============================== 4 passed in 0.01s ==============================

Process finished with exit code 0

未指定scope

在scope未指定的情况下(或者scope=None),当indirect被设置为True或者包含所有的argnames参数时,作用域为所有fixture作用域的最小范围,否则,其永远为function。

代码如下:

import pytest

@pytest.fixture(scope='module')
def test_input(request):
    pass

@pytest.fixture(scope='module')
def expected(request):
    pass

@pytest.mark.parametrize('test_input, expected', [(1,2),(3,4)], indirect=True)
def test_scope1(test_input, expected):
    pass

@pytest.mark.parametrize('test_input, expected', [(1,2),(3,4)], indirect=True)
def test_scope2(test_input, expected):
    pass

test_input和expected的作用域都是module,所以参数的作用域也是module,用例的收集顺序和4.8.1节相同。

代码如下:

D:\SynologyDrive\CodeLearning\WIN\pytest-book\venv\Scripts\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2022.1.3\plugins\python-ce\helpers\pycharm\_jb_pytest_runner.py" --path D:/SynologyDrive/CodeLearning/WIN/pytest-book/src/chapter-4/test_scope.py
Testing started at 10:01 ...
Launching pytest with arguments D:/SynologyDrive/CodeLearning/WIN/pytest-book/src/chapter-4/test_scope.py in D:\SynologyDrive\CodeLearning\WIN\pytest-book\src\chapter-4

============================= test session starts =============================
platform win32 -- Python 3.7.7, pytest-5.4.1, py-1.11.0, pluggy-0.13.1 -- D:\SynologyDrive\CodeLearning\WIN\pytest-book\venv\Scripts\python.exe
cachedir: .pytest_cache
rootdir: D:\SynologyDrive\CodeLearning\WIN\pytest-book\src\chapter-4, inifile: pytest.ini
collecting ... collected 4 items

test_scope.py::test_scope1[1-2] PASSED                                   [ 25%]
test_scope.py::test_scope2[1-2] PASSED                                   [ 50%]
test_scope.py::test_scope1[3-4] PASSED                                   [ 75%]
test_scope.py::test_scope2[3-4] PASSED                                   [100%]

============================== 4 passed in 0.01s ==============================

Process finished with exit code 0

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值