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

文章详细介绍了pytest框架中parametrize装饰器的使用,包括参数化测试方法的参数限制,如参数名必须是测试方法参数的子集,不能与有默认值的参数冲突。同时,展示了parametrize如何与fixture结合使用,以及参数化参数如何覆盖同名fixture的返回值。
摘要由CSDN通过智能技术生成

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

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

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


parametrize方法中的第一个参数argnames是一个用逗号分隔的字符串,或者一个列表/元组,表明指定的参数名。argnames通常是与被标记测试方法入参的参数名对应的,但实际上有一些限制,它只能是被标记测试方法入参的子集。

argnames与测试方法中的参数关系

  1. 测试方法未声明,mark.parametrize中声明

test_sample1中并没有声明expected参数,如果在标记中强行声明,则会得到如下错误。

代码如下:

import pytest

@pytest.mark.parametrize('input, expected', [(1,2)])
def test_sample1(input):
    assert input + 1 == 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_test.py
Testing started at 14:17 ...
Launching pytest with arguments D:/SynologyDrive/CodeLearning/WIN/pytest-book/src/chapter-4/test_test.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
collecting ... 
src/chapter-4/test_test.py:None (src/chapter-4/test_test.py)
In test_sample1: function uses no argument 'expected'




collected 0 items / 1 error

=================================== ERRORS ====================================
_________________ ERROR collecting src/chapter-4/test_test.py _________________
In test_sample1: function uses no argument 'expected'
=========================== short test summary info ===========================
ERROR test_test.py
!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
============================== 1 error in 0.08s ===============================

Process finished with exit code 2
  1. 测试方法参数声明的范围小于mark.parametrize中声明的范围

不能是被标记测试方法入参中定义了默认值的参数。

代码如下:

import pytest

@pytest.mark.parametrize('input, expected', [(1,2)])
def test_sample2(input, expected=2):
    assert input + 1 == expected

虽然test_sample2声明了expected参数,但同时也为其赋予了一个默认值,如果非要在标记中强行声明,则会得到如下错误:

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_test.py
Testing started at 14:17 ...
Launching pytest with arguments D:/SynologyDrive/CodeLearning/WIN/pytest-book/src/chapter-4/test_test.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
collecting ... 
src/chapter-4/test_test.py:None (src/chapter-4/test_test.py)
In test_sample2: function already takes an argument 'expected' with a default value




collected 0 items / 1 error

=================================== ERRORS ====================================
_________________ ERROR collecting src/chapter-4/test_test.py _________________
In test_sample2: function already takes an argument 'expected' with a default value
=========================== short test summary info ===========================
ERROR test_test.py
!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
============================== 1 error in 0.05s ===============================

Process finished with exit code 2

argnames调用覆盖同名的fixture

通常在使用fixture和参数parametrize时,可以一个参数使用参数化,另一个参数使用fixture和参数化,而同时使用fixture和参数化时,参数化的参数值会覆盖原来fixture返回的值。

代码如下:

import pytest

@pytest.fixture()
def expected():
    return 2

@pytest.fixture()
def input():
    return 0

@pytest.mark.parametrize('input', [(1)])
def test_sample(input, expected):
    assert input + 1 == expected
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_test.py
Testing started at 14:18 ...
Launching pytest with arguments D:/SynologyDrive/CodeLearning/WIN/pytest-book/src/chapter-4/test_test.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
collecting ... collected 1 item

test_test.py::test_sample[1] PASSED                                      [100%]

============================== 1 passed in 0.01s ==============================

Process finished with exit code 0

可以看到expected参数未使用参数化传入数据,而是直接调用fixture中的返回值2,input同时使用参数化和fixture,参数化中参数值1覆盖了原来fixture的返回值0,因此执行结果断言应该是成功的。

参数化的参数可以不是fixture的,因此可以通过参数值传入。

代码如下:

import pytest

@pytest.fixture()
def expected():
    return 1

@pytest.mark.parametrize('input, expected', [(1, 2)])
def test_sample(input, expected):
    assert input + 1 == expected
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_test.py
Testing started at 14:20 ...
Launching pytest with arguments D:/SynologyDrive/CodeLearning/WIN/pytest-book/src/chapter-4/test_test.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
collecting ... collected 1 item

test_test.py::test_sample[1-2] PASSED                                    [100%]

============================== 1 passed in 0.01s ==============================

Process finished with exit code 0

test_sample标记的input参数的值是由后面的(1,2)传入的,expected参数(参数值为2)覆盖了同名的fixture expected(返回值1),所以这条用例是可以测试成功的。

注意:可参考第3.12节内容在用例参数中覆写fixture。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值