pytest framework advanced self-study series | ids 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


The ids parameter is id, which cannot be used because it is the same as the keyword, so it is changed to ids. Usually, when ids is not written, different data is displayed directly each time, that is, the data itself. If the ids value is defined, this value is displayed. You can mark our test points by writing content in ids. Usually we test numbers, letters, boundary values, etc. during the test, so we can check whether the coverage is comprehensive by setting this parameter. For example, the first data is a number, the second data is Chinese, and the third data is a special character. In this way, you can see whether the test is complete or not when you see the results in the report.

ids is an executable object used to generate test ids, or a list/tuple indicating the test ids of all newly added test cases. These ids can be used with -k to select a specific use case to run, and they will also identify that specific use case when it fails. Running pytest --collect-only will show the generated id.

length of ids

If the test id is specified directly using a list/tuple, then its length is equal to the length of argvalues.

code show as below:

import pytest

@pytest.mark.parametrize('input, expected', [(1,2),(3,4)], ids=['first','second'])
def test_ids_with_ids(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_test.py
Testing started at 9:30 ...
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 2 items

test_test.py::test_ids_with_ids[first] PASSED                            [ 50%]
test_test.py::test_ids_with_ids[second] PASSED                           [100%]

============================== 2 passed in 0.01s ==============================

Process finished with exit code 0

The id of the input parameter is first, the value of the first time is 1, the value of the second time is 3, the id of the expected parameter is second, the value of the first time is 2, and the value of the second time is 4.

same ids

If the test ids are the same, pytest will automatically add indexes later, such as [num0] and [num1].

import pytest

@pytest.mark.parametrize('input, expected', [(1,2),(3,4)], ids=['num', 'num'])
def test_ids_with_ids(input, expected):
    pass

The result of the execution is 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_test.py
Testing started at 9:31 ...
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 2 items

test_test.py::test_ids_with_ids[num0] PASSED                             [ 50%]
test_test.py::test_ids_with_ids[num1] PASSED                             [100%]

============================== 2 passed in 0.01s ==============================

Process finished with exit code 0

Use Chinese in ids

Chinese can be used in the test ID, and the byte sequence is displayed by default.

import pytest

@pytest.mark.parametrize('input, expected', [(1,2),(3,4)], ids=['num', '中文'])
def test_ids_with_ids(input, expected):
    pass

The collected test IDs 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_test.py
Testing started at 9:31 ...
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 2 items

test_test.py::test_ids_with_ids[num] PASSED                              [ 50%]
test_test.py::test_ids_with_ids[\u4e2d\u6587] PASSED                     [100%]

============================== 2 passed in 0.01s ==============================

Process finished with exit code 0

As can be seen from the above results, "Chinese" is expected to be displayed, but \u4e2d\u6587 is actually displayed. How to solve this problem, the author checked the source code python.py.

To solve Chinese garbled characters, you can set the disable_test_id_escaping_and_forfeit_all_rights_to_community_support option to True in pytest.ini.

code show as below:

[pytest]
disable_test_id_escaping_and_forfeit_all_rights_to_community_support = True

The test ID collected again is 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_test.py
Testing started at 9:33 ...
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\src\chapter-4, inifile: pytest.ini
collecting ... collected 2 items

test_test.py::test_ids_with_ids[num] PASSED                              [ 50%]
test_test.py::test_ids_with_ids[中文] PASSED                             [100%]

============================== 2 passed in 0.01s ==============================

Process finished with exit code 0

Generate ids by function

import pytest

def idfn(val):
    return val + 1

@pytest.mark.parametrize('input, expected', [(1,2),(3,4)], ids=idfn)
def test_ids_with_ids(input, expected):
    pass

The execution result is displayed 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_test.py
Testing started at 9:34 ...
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\src\chapter-4, inifile: pytest.ini
collecting ... collected 2 items

test_test.py::test_ids_with_ids[2-3] PASSED                              [ 50%]
test_test.py::test_ids_with_ids[4-5] PASSED                              [100%]

============================== 2 passed in 0.01s ==============================

Process finished with exit code 0

It is not difficult to see from the above example that for a specific argvalues ​​parameter (1, 2), it is split into 1 and 2 and passed to idfn respectively, and the return values ​​are connected together by the - symbol as a The test id is returned instead of passing in (1, 2) as a whole.

ids coverage

It can also be seen from the above source code that if the id attribute has been specified through pytest.param, then the corresponding test id in ids will be overwritten.

code show as below:

import pytest

@pytest.mark.parametrize('input, expected', [(1,2),pytest.param(3,4,id='id_via_pytest_param')], ids=['first', 'second'])
def test_ids_with_ids(input, expected):
    pass

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_test.py
Testing started at 9:38 ...
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\src\chapter-4, inifile: pytest.ini
collecting ... collected 2 items

test_test.py::test_ids_with_ids[first] PASSED                            [ 50%]
test_test.py::test_ids_with_ids[id_via_pytest_param] PASSED              [100%]

============================== 2 passed in 0.01s ==============================

Process finished with exit code 0

The test id is id_via_pytest_param, not second.

The role of ids

The main function of ids is to further refine test cases, distinguish different test scenarios, and provide a new method for targeted execution of tests.

For example, for the following test cases, you can pass the -k 'Non-Windows' option to only execute scenarios related to Non-Windows.

code show as below:

import pytest

@pytest.mark.parametrize('input, expected',[
    pytest.param(1,2,id='Windows'),
    pytest.param(3,4,id='Windows'),
    pytest.param(5,6,id='Non-Windows')
])
def test_ids_with_ids(input, expected):
    pass
PS D:\SynologyDrive\CodeLearning\WIN\pytest-book\src\chapter-4> pytest -k 'Non-Windows' .\test_ids.py 
========================================================================================================= test session starts =========================================================================================================
platform win32 -- Python 3.7.7, pytest-5.4.1, py-1.11.0, pluggy-0.13.1
rootdir: D:\SynologyDrive\CodeLearning\WIN\pytest-book\src\chapter-4, inifile: pytest.ini
collected 3 items / 2 deselected / 1 selected                                                                                                                                                                                           

test_ids.py .                                                                                                                                                                                                                    [100%] 

=================================================================================================== 1 passed, 2 deselected in 0.01s =================================================================================================== 
PS D:\SynologyDrive\CodeLearning\WIN\pytest-book\src\chapter-4>

Guess you like

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