书籍来源:房荔枝 梁丽丽《pytest框架与自动化测试应用》
一边学习一边整理老师的课程内容及实验笔记,并与大家分享,侵权即删,谢谢支持!
附上汇总贴:pytest框架进阶自学系列 | 汇总_热爱编程的通信人的博客-CSDN博客
parametrize方法中的第一个参数argnames是一个用逗号分隔的字符串,或者一个列表/元组,表明指定的参数名。argnames通常是与被标记测试方法入参的参数名对应的,但实际上有一些限制,它只能是被标记测试方法入参的子集。
argnames与测试方法中的参数关系
- 测试方法未声明,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
- 测试方法参数声明的范围小于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。