• 【pytest】 参数化@pytest.mark.parametrize


    1.创建   test_parametrize.py

    通过

    @pytest.mark.parametrize 方法设置参数
    1. import pytest
    2. import math
    3. #pytest参数化
    4. @pytest.mark.parametrize(
    5. "base,exponent,expected", # 参数变量名称
    6. # 每个元组都是一条测试用例测试数据
    7. [
    8. (2,2,4),
    9. (3,3,9),
    10. (1,9,1),
    11. (0,9,0)
    12. ],
    13. ids=['case1','case2','case3','case4'] # 默认为None 用来定义测试用例的名称
    14. )
    15. def test_pow(base,exponent,expected):
    16. print(base,exponent,expected)
    17. assert math.pow(base,exponent)==expected

    2.运行结果:pytest -s test_parametrize.py

    1. PS E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example> pytest -v test_parametrize.py
    2. ======================================================================== test session starts ========================================================================
    3. platform win32 -- Python 3.10.9, pytest-7.1.2, pluggy-1.0.0 -- D:\software\python3\anconda3\python.exe
    4. cachedir: .pytest_cache
    5. rootdir: E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example
    6. plugins: anyio-3.5.0
    7. collected 0 items
    8. ======================================================================= no tests ran in 0.03s =======================================================================
    9. ERROR: file or directory not found: test_parametrize.py
    10. PS E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example> pytest -s test_parametrize.py
    11. ======================================================================== test session starts ========================================================================
    12. platform win32 -- Python 3.10.9, pytest-7.1.2, pluggy-1.0.0
    13. rootdir: E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example
    14. plugins: anyio-3.5.0
    15. collected 0 items
    16. ======================================================================= no tests ran in 0.04s =======================================================================
    17. ERROR: file or directory not found: test_parametrize.py
    18. PS E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example> cd parametrize
    19. PS E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example\parametrize> pytest -s test_parametrize.py
    20. ======================================================================== test session starts ========================================================================
    21. platform win32 -- Python 3.10.9, pytest-7.1.2, pluggy-1.0.0
    22. rootdir: E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example\parametrize
    23. plugins: anyio-3.5.0
    24. collected 4 items
    25. test_parametrize.py .F..
    26. ============================================================================= FAILURES ==============================================================================
    27. __________________________________________________________________________ test_pow[case2] __________________________________________________________________________
    28. base = 3, exponent = 3, expected = 9
    29. @pytest.mark.parametrize(
    30. "base,exponent,expected", # 参数变量名称
    31. # 每个元组都是一条测试用例测试数据
    32. [
    33. (2,2,4),
    34. (3,3,9),
    35. (1,9,1),
    36. (0,9,0)
    37. ],
    38. ids=['case1','case2','case3','case4'] # 默认为None 用来定义测试用例的名称
    39. )
    40. def test_pow(base,exponent,expected):
    41. > assert math.pow(base,exponent)==expected
    42. E assert 27.0 == 9
    43. E + where 27.0 = in function pow>(3, 3)
    44. E + where in function pow> = math.pow
    45. test_parametrize.py:18: AssertionError
    46. ====================================================================== short test summary info ======================================================================
    47. FAILED test_parametrize.py::test_pow[case2] - assert 27.0 == 9
    48. ==================================================================== 1 failed, 3 passed in 1.66s ====================================================================
    49. PS E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example\parametrize> pytest -s test_parametrize.py
    50. ======================================================================== test session starts ========================================================================
    51. platform win32 -- Python 3.10.9, pytest-7.1.2, pluggy-1.0.0
    52. rootdir: E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example\parametrize
    53. plugins: anyio-3.5.0
    54. collected 4 items
    55. test_parametrize.py 2 2 4
    56. .3 3 9
    57. F1 9 1
    58. .0 9 0
    59. .
    60. ============================================================================= FAILURES ==============================================================================
    61. __________________________________________________________________________ test_pow[case2] __________________________________________________________________________
    62. base = 3, exponent = 3, expected = 9
    63. @pytest.mark.parametrize(
    64. "base,exponent,expected", # 参数变量名称
    65. # 每个元组都是一条测试用例测试数据
    66. [
    67. (2,2,4),
    68. (3,3,9),
    69. (1,9,1),
    70. (0,9,0)
    71. ],
    72. ids=['case1','case2','case3','case4'] # 默认为None 用来定义测试用例的名称
    73. )
    74. def test_pow(base,exponent,expected):
    75. print(base,exponent,expected)
    76. > assert math.pow(base,exponent)==expected
    77. E assert 27.0 == 9
    78. E + where 27.0 = in function pow>(3, 3)
    79. E + where in function pow> = math.pow
    80. test_parametrize.py:19: AssertionError
    81. ====================================================================== short test summary info ======================================================================
    82. FAILED test_parametrize.py::test_pow[case2] - assert 27.0 == 9
    83. ==================================================================== 1 failed, 3 passed in 0.44s ====================================================================
    84. PS E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example\parametrize>

      运行  pytest -v test_parametrize.py

    1. ======================================================================== test session starts ========================================================================
    2. platform win32 -- Python 3.10.9, pytest-7.1.2, pluggy-1.0.0 -- D:\software\python3\anconda3\python.exe
    3. cachedir: .pytest_cache
    4. rootdir: E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example\parametrize
    5. plugins: anyio-3.5.0
    6. collected 4 items
    7. test_parametrize.py::test_pow[case1] PASSED [ 25%]
    8. test_parametrize.py::test_pow[case2] FAILED [ 50%]
    9. test_parametrize.py::test_pow[case3] PASSED [ 75%]
    10. test_parametrize.py::test_pow[case4] PASSED [100%]
    11. ============================================================================= FAILURES ==============================================================================
    12. __________________________________________________________________________ test_pow[case2] __________________________________________________________________________
    13. base = 3, exponent = 3, expected = 9
    14. @pytest.mark.parametrize(
    15. "base,exponent,expected", # 参数变量名称
    16. # 每个元组都是一条测试用例测试数据
    17. [
    18. (2,2,4),
    19. (3,3,9),
    20. (1,9,1),
    21. (0,9,0)
    22. ],
    23. ids=['case1','case2','case3','case4'] # 默认为None 用来定义测试用例的名称
    24. )
    25. def test_pow(base,exponent,expected):
    26. print(base,exponent,expected)
    27. > assert math.pow(base,exponent)==expected
    28. E assert 27.0 == 9
    29. E + where 27.0 = in function pow>(3, 3)
    30. E + where in function pow> = math.pow
    31. test_parametrize.py:19: AssertionError
    32. ----------------------------------------------------------------------- Captured stdout call ------------------------------------------------------------------------
    33. 3 3 9
    34. ====================================================================== short test summary info ======================================================================
    35. FAILED test_parametrize.py::test_pow[case2] - assert 27.0 == 9
    36. ==================================================================== 1 failed, 3 passed in 1.79s ====================================================================
    37. PS E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example\parametrize>

    3.运行参数控制


    pytest -s test_parametrize.py     其中 -s 用于关闭捕捉 

    pytest -v test_parametrize.py   增加测试用例冗长

    pytest --help  查看帮助

    pytest  -q  test_parametrize.py 减少测试冗长

    pytest -x   test_parametrize.py 如果出现一条测试用例失败,就停止

    pytest  ./test_dir    运行测试目录 

    pytest      指定特定类或是方法

     pytest   ./fixture/test_fixtures_02.py::TestMultiply::test_numbers_5_6

    1. # 功能函数
    2. def multiply(a, b):
    3. return a * b
    4. class TestMultiply:
    5. # =====fixtures========
    6. @classmethod
    7. def setup_class(cls):
    8. print("setup_class=========>")
    9. @classmethod
    10. def teardown_class(cls):
    11. print("teardown_class=========>")
    12. def setup_method(self, method):
    13. print("setup_method----->>")
    14. def teardown_method(self, method):
    15. print("teardown_method-->>")
    16. def setup(self):
    17. print("setup----->")
    18. def teardown(self):
    19. print("teardown-->")
    20. # =====测试用例========
    21. def test_numbers_5_6(self):
    22. print('test_numbers_5_6')
    23. assert multiply(5, 6) == 30
    24. def test_strings_b_2(self):
    25. print('test_strings_b_2')
    26. assert multiply('b', 2) == 'bb'
    1. pytest ./fixture/test_fixtures_02.py::TestMultiply::test_numbers_5_6
    2. ======================================================================== test session starts ========================================================================
    3. platform win32 -- Python 3.10.9, pytest-7.1.2, pluggy-1.0.0
    4. rootdir: E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example
    5. plugins: anyio-3.5.0
    6. collected 1 item
    7. fixture\test_fixtures_02.py . [100%]

    通过 main 方法运行 

     

    1. import pytest
    2. if __name__=='__main__':
    3. #pytest.main(['-s','./fixture'])
    4. pytest.main(['-v', './fixture'])

     

    1. ============================== 6 passed in 0.06s ==============================
    2. ============================= test session starts =============================
    3. platform win32 -- Python 3.10.9, pytest-7.1.2, pluggy-1.0.0 -- D:\software\python3\anconda3\python.exe
    4. cachedir: .pytest_cache
    5. rootdir: E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example
    6. plugins: anyio-3.5.0
    7. collecting ... collected 6 items
    8. fixture/test_fixtures_01.py::test_mult_3_4 PASSED [ 16%]
    9. fixture/test_fixtures_01.py::test_mult_a_4 PASSED [ 33%]
    10. fixture/test_fixtures_011.py::test_multiply_3_4 PASSED [ 50%]
    11. fixture/test_fixtures_011.py::test_multiply_a_3 PASSED [ 66%]
    12. fixture/test_fixtures_02.py::TestMultiply::test_numbers_5_6 PASSED [ 83%]
    13. fixture/test_fixtures_02.py::TestMultiply::test_strings_b_2 PASSED [100%]
    14. ============================== 6 passed in 0.06s ==============================

  • 相关阅读:
    【开发环境】安装 Hadoop 运行环境 ( 下载 Hadoop | 解压 Hadoop | 设置 Hadoop 环境变量 | 配置 Hadoop 环境脚本 | 安装 winutils )
    osqp的原理ADMM(交替方向乘子法)理解
    Maven execution terminated abnormally (exit code 1) 创建Maven项目时报错解决方法
    设计模式 - 适配器模式
    乘积小于 K 的子数组
    CSS外边距重叠:原理、结果
    (LeetCode C++)有效的括号
    springboot+高校学生实习档案管理 毕业设计-附源码221508
    有没有不用加班的程序员 ?
    vue中tinymce的使用
  • 原文地址:https://blog.csdn.net/oDianZi1234567/article/details/132958505