pytest framework advanced self-study series | argvalues parameters

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


The parameter value argvalues ​​in parameterization is an iterable object, which indicates the assignment of argnames parameters. The specific situations are as follows: if argnames contains multiple parameters, then the iterative return elements of argvalues ​​must be measurable values, that is, support len( ) method, and the length is equal to the number of parameters declared by argnames, so it can be a tuple/list/collection, etc., indicating the actual parameters of all input parameters.

code show as below:

import pytest

@pytest.mark.parametrize('input, expected', [(1,2),[2,3],set([3,4])])
def test_sample4(input, expected):
    print(expected)
    assert input + 1 == expected

The execution results are as follows:

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_mark_param_sub.py
Testing started at 14:25 ...
Launching pytest with arguments D:/SynologyDrive/CodeLearning/WIN/pytest-book/src/chapter-4/test_mark_param_sub.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 3 items

test_mark_param_sub.py::test_sample4[1-2] PASSED                         [ 33%]2

test_mark_param_sub.py::test_sample4[2-3] PASSED                         [ 66%]3

test_mark_param_sub.py::test_sample4[3-4] PASSED                         [100%]4


============================== 3 passed in 0.01s ==============================

Process finished with exit code 0

argvalues ​​come from the Excel file

argvalues ​​is an iterable object, so it can be used in more complex scenarios, which is especially widely used in practical applications. The company generally saves the test data in an Excel table, or in a csv file, or in a database. The data can be read into a list first, so that it can be called directly in the parameterized parameter value. For example: read actual parameters from an Excel file.

code show as below:

import pytest

def read_excel():
    for dev in ['dev1', 'dev2', 'dev3']:
        yield dev
        
@pytest.mark.parametrize('dev', read_excel())
def test_sample(dev):
    assert dev
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:27 ...
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 3 items

test_test.py::test_sample[dev1] PASSED                                   [ 33%]
test_test.py::test_sample[dev2] PASSED                                   [ 66%]
test_test.py::test_sample[dev3] PASSED                                   [100%]

============================== 3 passed in 0.01s ==============================

Process finished with exit code 0

There are many ways to realize this scenario, and you can also directly load the data in the Excel file in a fixture, but their performance in the test report will be different.

Use pytest.param to assign values ​​to argvalues

In combining the pytest.param method to mark skip and xfail, you can use pytest.param to assign values ​​to the argvalues ​​parameter, so that the execution can be more detailed.

code show as below:

import pytest

@pytest.mark.parametrize(
    ('n', 'expected'),
    [(4,2),
     pytest.param(6, 3, marks=pytest.mark.xfail(), id='XPASS')])
def test_params(n, expected):
    assert n / 2 == expected

The execution result is shown in Figure 4-1.

Modify the calculator example above, if the specific data execution is divided into different results, you can use pytest.param to achieve.

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_mark_param_sub.py
Testing started at 14:31 ...
Launching pytest with arguments D:/SynologyDrive/CodeLearning/WIN/pytest-book/src/chapter-4/test_mark_param_sub.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 2 items

test_mark_param_sub.py::test_params[4-2] PASSED                          [ 50%]
test_mark_param_sub.py::test_params[XPASS] XPASS                         [100%]

======================== 1 passed, 1 xpassed in 0.01s =========================

Process finished with exit code 0

code show as below:

import pytest

@pytest.mark.parametrize("test_input, expected", [("3+5", 8),
                                                  ("2+5", 7),
                                                  pytest.param("6*9", 42, marks=pytest.mark.xfail)])
def test_eval(test_input, expected):
    assert eval(test_input) == expected

The execution results are as follows:

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_mark_param_sub.py
Testing started at 14:33 ...
Launching pytest with arguments D:/SynologyDrive/CodeLearning/WIN/pytest-book/src/chapter-4/test_mark_param_sub.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 3 items

test_mark_param_sub.py::test_eval[3+5-8] PASSED                          [ 33%]
test_mark_param_sub.py::test_eval[2+5-7] PASSED                          [ 66%]
test_mark_param_sub.py::test_eval[6*9-42] XFAIL                          [100%]
test_input = '6*9', expected = 42

    @pytest.mark.parametrize("test_input, expected", [("3+5", 8),
                                                      ("2+5", 7),
                                                      pytest.param("6*9", 42, marks=pytest.mark.xfail)])
    def test_eval(test_input, expected):
>       assert eval(test_input) == expected
E       assert 54 == 42
E         +54
E         -42

test_mark_param_sub.py:7: AssertionError


======================== 2 passed, 1 xfailed in 0.03s =========================

Process finished with exit code 0

The principle is explored below, and actual users can take a look.

Regardless of whether the argvalues ​​are measurable objects (lists, tuples, etc.) or specific values, they will be encapsulated into a ParameterSet object in the source code, which is a named tuple (namedtuple), including values, marks, id 3 element, the code is as follows:

What happens if you pass a ParameterSet object directly? Find the answer in the source code below.

The source code is as follows:

It can be seen that if a ParameterSet object is passed directly, then it returns itself (return parameterset), so the two writing methods in the following example are equivalent.

After checking here, you may have guessed that the function of pytest.param is to encapsulate a ParameterSet object. Let's go to the source code to verify it!

The source code is as follows:

As expected above, now you should have a better understanding of how to pass parameters to argvalues.

Guess you like

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