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

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

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

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


ids参数就是id,因为与关键字雷同所以不能用,因此改成ids。通常不写ids时每次不同数据直接显示,也就是数据本身,如果定义ids值,则显示的就是这个值。大家可以通过在ids中写内容来标记我们的测试要点。通常我们在测试时分测试数字、字母、边界值等,因此我们可以通过对这个参数的设置检查是不是覆盖全面。例如第1个数据是数字,第2个数据是中文,第3个数据是特殊字符。这样在报告中看到结果就知道是否测试完整。

ids是一个可执行对象,用于生成测试id,或者一个列表/元组,指明所有新增用例的测试id。这些id可用于-k选择要运行的特定用例,当某个用例失败时,它们还将识别该特定用例。运行pytest--collect-only将显示生成的id。

ids的长度

如果使用列表/元组直接指明测试id,那么它的长度等于argvalues的长度。

代码如下:

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

input参数的id是first,第1次的值是1,第2次的值是3,expected参数的id是second,第1次的值是2,第2次的值是4。

ids相同

如果测试id相同,pytest则会在后面自动添加索引,例如[num0]和[num1]。

import pytest

@pytest.mark.parametrize('input, expected', [(1,2),(3,4)], ids=['num', 'num'])
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: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

ids中使用中文

测试ID中可以使用中文,默认显示的是字节序列。

import pytest

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

收集到的测试ID如下:

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

从上面的结果可以看出,期望显示“中文”,但实际上显示的是\u4e2d\u6587。如何解决此问题,笔者查了一下源码python.py。

解决中文乱码,可以在pytest.ini中将disable_test_id_escaping_and_forfeit_all_rights_to_community_support选项设置为True。

代码如下:

[pytest]
disable_test_id_escaping_and_forfeit_all_rights_to_community_support = True

再次收集到的测试ID如下:

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

通过函数生成ids

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

执行结果显示如下:

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

通过上面的例子不难看出,对于一个具体的argvalues参数(1,2)来讲,它被拆分为1和2分别传递给idfn,并将返回值通过-符号连接在一起,以此作为一个测试id返回,而不是将(1,2)作为一个整体传入。

ids的覆盖

从上面的源码还可以看出,假设已经通过pytest.param指定了id属性,那么将会覆盖ids中对应的测试id。

代码如下:

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

执行结果如下:

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

测试id是id_via_pytest_param,而不是second。

ids的作用

ids最主要的作用就是更进一步细化测试用例,区分不同的测试场景,为有针对性的执行测试提供了一种新方法。

例如,对于以下测试用例,可以通过-k 'Non-Windows'选项,只执行和Non-Windows相关的场景。

代码如下:

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>

猜你喜欢

转载自blog.csdn.net/guolianggsta/article/details/131695722
ids