Pytest----parametrize参数化对两组数据全排列组合测试
作者:redrose2100   类别:    日期:2022-12-02 07:26:23    阅读:1265 次   消耗积分:0 分

    Parametrize参数化同样支持对两组数据的全排列组合测试,这个功能对一些要求数据进行全排列覆盖测试的场景是非常有用的,具体用法如下,即使用两次参数化声明即可。

    1. import pytest
    2. @pytest.mark.parametrize("x", [0, 1])
    3. @pytest.mark.parametrize("y", [2, 3])
    4. def test_foo(x, y):
    5. print("x:",x)
    6. print("y:",y)
    7. assert x<y

    执行结果如下,这里可以看出,x和y分别进行了全排列组合,即(0,2),(0,3),(1,2),(1,3),执行结果也明确显示这里有四个测试用例。

    1. (demo-HCIhX0Hq) E:\demo>pytest -s
    2. =================== test session starts ===================
    3. platform win32 -- Python 3.7.9, pytest-7.2.0, pluggy-1.0.0
    4. rootdir: E:\demo
    5. plugins: assume-2.4.3, rerunfailures-10.2
    6. collected 4 items
    7. test_demo.py x: 0
    8. y: 2
    9. .x: 1
    10. y: 2
    11. .x: 0
    12. y: 3
    13. .x: 1
    14. y: 3
    15. .
    16. ==================== 4 passed in 0.02s ====================
    17. (demo-HCIhX0Hq) E:\demo>

    那么加入一组数据是单个类型,另一组数据是多个,比如两个类型的,这两组数据放在一起进行全排列组合,具体使用方法见如下代码。

    1. import pytest
    2. @pytest.mark.parametrize("x", [0, 1])
    3. @pytest.mark.parametrize("y,z", [(2,3), (3,4)])
    4. def test_foo(x, y,z):
    5. print("x:",x)
    6. print("y:",y)
    7. print("z:",z)
    8. assert x+y==z

    执行结果如下,可以使用方式基本是类似的。

    1. (demo-HCIhX0Hq) E:\demo>pytest -s
    2. =================== test session starts ===================
    3. platform win32 -- Python 3.7.9, pytest-7.2.0, pluggy-1.0.0
    4. rootdir: E:\demo
    5. plugins: assume-2.4.3, rerunfailures-10.2
    6. collected 4 items
    7. test_demo.py x: 0
    8. y: 2
    9. z: 3
    10. Fx: 1
    11. y: 2
    12. z: 3
    13. .x: 0
    14. y: 3
    15. z: 4
    16. Fx: 1
    17. y: 3
    18. z: 4
    19. .
    20. ======================== FAILURES =========================
    21. _____________________ test_foo[2-3-0] _____________________
    22. x = 0, y = 2, z = 3
    23. @pytest.mark.parametrize("x", [0, 1])
    24. @pytest.mark.parametrize("y,z", [(2,3), (3,4)])
    25. def test_foo(x, y,z):
    26. print("x:",x)
    27. print("y:",y)
    28. print("z:",z)
    29. > assert x+y==z
    30. E assert (0 + 2) == 3
    31. test_demo.py:9: AssertionError
    32. _____________________ test_foo[3-4-0] _____________________
    33. x = 0, y = 3, z = 4
    34. @pytest.mark.parametrize("x", [0, 1])
    35. @pytest.mark.parametrize("y,z", [(2,3), (3,4)])
    36. def test_foo(x, y,z):
    37. print("x:",x)
    38. print("y:",y)
    39. print("z:",z)
    40. > assert x+y==z
    41. E assert (0 + 3) == 4
    42. test_demo.py:9: AssertionError
    43. ================= short test summary info =================
    44. FAILED test_demo.py::test_foo[2-3-0] - assert (0 + 2) == 3
    45. FAILED test_demo.py::test_foo[3-4-0] - assert (0 + 3) == 4
    46. =============== 2 failed, 2 passed in 0.08s ===============
    47. (demo-HCIhX0Hq) E:\demo>

    同理假如有三组数据需要进行全排列组合,有怎么操作的,类似地,只需要再增加一个个参数化的声明即可,如下。

    1. import pytest
    2. @pytest.mark.parametrize("x", [0, 1])
    3. @pytest.mark.parametrize("y", [2, 3])
    4. @pytest.mark.parametrize("z", [4, 5])
    5. def test_foo(x, y,z):
    6. print("x:",x)
    7. print("y:",y)
    8. print("z:",z)
    9. assert x+y==z

    此时执行结果如下,可以发现,此时的组合个数为222=8个,因此总共有8个用例。

    1. (demo-HCIhX0Hq) E:\demo>pytest -s
    2. =================== test session starts ===================
    3. platform win32 -- Python 3.7.9, pytest-7.2.0, pluggy-1.0.0
    4. rootdir: E:\demo
    5. plugins: assume-2.4.3, rerunfailures-10.2
    6. collected 8 items
    7. test_demo.py x: 0
    8. y: 2
    9. z: 4
    10. Fx: 1
    11. y: 2
    12. z: 4
    13. Fx: 0
    14. y: 3
    15. z: 4
    16. Fx: 1
    17. y: 3
    18. z: 4
    19. .x: 0
    20. y: 2
    21. z: 5
    22. Fx: 1
    23. y: 2
    24. z: 5
    25. Fx: 0
    26. y: 3
    27. z: 5
    28. Fx: 1
    29. y: 3
    30. z: 5
    31. F
    32. ======================== FAILURES =========================
    33. _____________________ test_foo[4-2-0] _____________________
    34. x = 0, y = 2, z = 4
    35. @pytest.mark.parametrize("x", [0, 1])
    36. @pytest.mark.parametrize("y", [2, 3])
    37. @pytest.mark.parametrize("z", [4, 5])
    38. def test_foo(x, y,z):
    39. print("x:",x)
    40. print("y:",y)
    41. print("z:",z)
    42. > assert x+y==z
    43. E assert (0 + 2) == 4
    44. test_demo.py:10: AssertionError
    45. _____________________ test_foo[4-2-1] _____________________
    46. x = 1, y = 2, z = 4
    47. @pytest.mark.parametrize("x", [0, 1])
    48. @pytest.mark.parametrize("y", [2, 3])
    49. @pytest.mark.parametrize("z", [4, 5])
    50. def test_foo(x, y,z):
    51. print("x:",x)
    52. print("y:",y)
    53. print("z:",z)
    54. > assert x+y==z
    55. E assert (1 + 2) == 4
    56. test_demo.py:10: AssertionError
    57. _____________________ test_foo[4-3-0] _____________________
    58. x = 0, y = 3, z = 4
    59. @pytest.mark.parametrize("x", [0, 1])
    60. @pytest.mark.parametrize("y", [2, 3])
    61. @pytest.mark.parametrize("z", [4, 5])
    62. def test_foo(x, y,z):
    63. print("x:",x)
    64. print("y:",y)
    65. print("z:",z)
    66. > assert x+y==z
    67. E assert (0 + 3) == 4
    68. test_demo.py:10: AssertionError
    69. _____________________ test_foo[5-2-0] _____________________
    70. x = 0, y = 2, z = 5
    71. @pytest.mark.parametrize("x", [0, 1])
    72. @pytest.mark.parametrize("y", [2, 3])
    73. @pytest.mark.parametrize("z", [4, 5])
    74. def test_foo(x, y,z):
    75. print("x:",x)
    76. print("y:",y)
    77. print("z:",z)
    78. > assert x+y==z
    79. E assert (0 + 2) == 5
    80. test_demo.py:10: AssertionError
    81. _____________________ test_foo[5-2-1] _____________________
    82. x = 1, y = 2, z = 5
    83. @pytest.mark.parametrize("x", [0, 1])
    84. @pytest.mark.parametrize("y", [2, 3])
    85. @pytest.mark.parametrize("z", [4, 5])
    86. def test_foo(x, y,z):
    87. print("x:",x)
    88. print("y:",y)
    89. print("z:",z)
    90. > assert x+y==z
    91. E assert (1 + 2) == 5
    92. test_demo.py:10: AssertionError
    93. _____________________ test_foo[5-3-0] _____________________
    94. x = 0, y = 3, z = 5
    95. @pytest.mark.parametrize("x", [0, 1])
    96. @pytest.mark.parametrize("y", [2, 3])
    97. @pytest.mark.parametrize("z", [4, 5])
    98. def test_foo(x, y,z):
    99. print("x:",x)
    100. print("y:",y)
    101. print("z:",z)
    102. > assert x+y==z
    103. E assert (0 + 3) == 5
    104. test_demo.py:10: AssertionError
    105. _____________________ test_foo[5-3-1] _____________________
    106. x = 1, y = 3, z = 5
    107. @pytest.mark.parametrize("x", [0, 1])
    108. @pytest.mark.parametrize("y", [2, 3])
    109. @pytest.mark.parametrize("z", [4, 5])
    110. def test_foo(x, y,z):
    111. print("x:",x)
    112. print("y:",y)
    113. print("z:",z)
    114. > assert x+y==z
    115. E assert (1 + 3) == 5
    116. test_demo.py:10: AssertionError
    117. ================= short test summary info =================
    118. FAILED test_demo.py::test_foo[4-2-0] - assert (0 + 2) == 4
    119. FAILED test_demo.py::test_foo[4-2-1] - assert (1 + 2) == 4
    120. FAILED test_demo.py::test_foo[4-3-0] - assert (0 + 3) == 4
    121. FAILED test_demo.py::test_foo[5-2-0] - assert (0 + 2) == 5
    122. FAILED test_demo.py::test_foo[5-2-1] - assert (1 + 2) == 5
    123. FAILED test_demo.py::test_foo[5-3-0] - assert (0 + 3) == 5
    124. FAILED test_demo.py::test_foo[5-3-1] - assert (1 + 3) == 5
    125. =============== 7 failed, 1 passed in 0.13s ===============
    126. (demo-HCIhX0Hq) E:\demo>
    始终坚持开源开放共享精神,同时感谢您的充电鼓励和支持!
    版权所有,转载本站文章请注明出处:redrose2100, http://blog.redrose2100.com/article/461
    个人成就
    • 博客总数: 613 
    • 阅读总量: 745739 
    • 2022年 : 371 篇 
    • 2023年 : 211 篇 
    • 2024年 : 31 篇 
    • 2025年 : 0 篇 
    测试开发技术全栈公众号
    DevOps技术交流微信群