pytest framework advanced self-study series | indirect parameter

Book source: Fang Lizhi Liang Lili "pytest framework and automated testing application"

Organize the teacher's course content and experimental notes while studying, and share them with everyone. Any infringement will be deleted. Thank you for your support!

Attach a summary post: pytest framework advanced self-study series | summary


indirect is a subset of argnames or a boolean value. Redirect the actual parameter of the specified parameter to the fixture with the same name as the parameter through request.param to meet more complex scenarios. The default indirect is False, use the data after mark.parametrize. When indirect is True, the data in the fixture is used.

code show as below:

import pytest

@pytest.fixture()
def max(request):
    return request.param - 1

@pytest.fixture()
def min(request):
    return request.param + 1

@pytest.mark.parametrize('min, max', [(1,2),(3,4)])
def test_indirect(min, max):
    assert min <= max
    
@pytest.mark.parametrize('min, max', [(1,2),(3,4)], indirect=True)
def test_indirect_indirect(min, max):
    assert min >= max
    
@pytest.mark.parametrize('min, max',[(1,2),(3,4)], indirect=['max'])
def test_indirect_part_indirect(min, max):
    assert min == max
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_indirect.py
Testing started at 9:23 ...
Launching pytest with arguments D:/SynologyDrive/CodeLearning/WIN/pytest-book/src/chapter-4/test_indirect.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 6 items

test_indirect.py::test_indirect[1-2] PASSED                              [ 16%]
test_indirect.py::test_indirect[3-4] PASSED                              [ 33%]
test_indirect.py::test_indirect_indirect[1-2] PASSED                     [ 50%]
test_indirect.py::test_indirect_indirect[3-4] PASSED                     [ 66%]
test_indirect.py::test_indirect_part_indirect[1-2] PASSED                [ 83%]
test_indirect.py::test_indirect_part_indirect[3-4] PASSED                [100%]

============================== 6 passed in 0.03s ==============================

Process finished with exit code 0

indirect=True, the actual parameters corresponding to min and max are redirected to the fixture with the same name, and min and max use the fixture data.

indirect=['max'], only redirect the actual parameter corresponding to max to the fixture, min uses the following data, and max uses the fixture data.

In fact, this is a way of indirect parameterization. When indirect=True, it is allowed to use the fixture that receives the value to parameterize the test before passing the value to the test.

Guess you like

Origin blog.csdn.net/guolianggsta/article/details/131695062