当前位置:   article > 正文

【pytest】parametrize获取参数的几种常用形式

【pytest】parametrize获取参数的几种常用形式

一、直接在标签上传参:

     一个参数多个值   

@ pytest.mark.parametrize("参数", (参数值1, 参数值2, 参数值3))

示例:

  1. import pytest
  2. # 单个参数的情况
  3. @pytest.mark.parametrize("a", (1, 2, 3, 4))
  4. def test_add(a):
  5. print("\na的值")

结果:

  1. ============================= test session starts =============================
  2. collecting ... collected 4 items
  3. test3.py::test_add[1] PASSED [ 25%]
  4. a的值
  5. test3.py::test_add[2] PASSED [ 50%]
  6. a的值
  7. test3.py::test_add[3] PASSED [ 75%]
  8. a的值
  9. test3.py::test_add[4] PASSED [100%]
  10. a的值
  11. ============================== 4 passed in 0.10s ==============================
  12. Process finished with exit code 0

 多个参数多个值的情况

  1. # 单个参数的情况
  2. # 格式:@pytest.mark.parametrize("参数",参数值)
  3. @pytest.mark.parametrize("a", (1, 2, 3, 4))
  4. def test_add(a):
  5. print("\na的值")
  6. # 多个参数多个值的情况
  7. # 格式:@pytest.mark.parametrize("参数a, 参数b",([a1, b1],[a2,b2],[a3, b3]))
  8. @pytest.mark.parametrize("b,c,d", ([1, 2, 3], [4, 5, 6], [7, 8, 0]))
  9. def test_add2(b, c, d):
  10. print('\nb,c,d的值分别为:', f'{b}, {c}, {d}')

二、数据结构

列表(list)形式

  1. # 单个参数的情况
  2. data1 = [1, 2, 3, 4]
  3. @pytest.mark.parametrize("a", data1)
  4. def test_add(a):
  5. print('\na的值:', a)
  6. # 多个参数多个值得情况
  7. data2 = [
  8. (1, 2, 3),
  9. (4, 5, 6),
  10. (0, 8, 9)
  11. ]
  12. @pytest.mark.parametrize("a,b,c", data2)
  13. def test_add2(a, b, c):
  14. print('\na,b,c的值分别为:', f'{a},{b},{c}')

输出

  1. ============================= test session starts =============================
  2. collecting ... collected 7 items
  3. parametrize02_list.py::test_add[1] PASSED [ 14%]
  4. a的值: 1
  5. parametrize02_list.py::test_add[2] PASSED [ 28%]
  6. a的值: 2
  7. parametrize02_list.py::test_add[3] PASSED [ 42%]
  8. a的值: 3
  9. parametrize02_list.py::test_add[4] PASSED [ 57%]
  10. a的值: 4
  11. parametrize02_list.py::test_add2[1-2-3] PASSED [ 71%]
  12. a,b,c的值分别为: 1,2,3
  13. parametrize02_list.py::test_add2[4-5-6] PASSED [ 85%]
  14. a,b,c的值分别为: 4,5,6
  15. parametrize02_list.py::test_add2[0-8-9] PASSED [100%]
  16. a,b,c的值分别为: 0,8,9
  17. ============================== 7 passed in 0.05s ==============================
  18. Process finished with exit code 0

字典(dictionary)形式

  1. import pytest
  2. userinfo = [
  3. {"username": "小李", "password": "123456"},
  4. {"username": "大白", "password": "894561"}
  5. ]
  6. @pytest.mark.parametrize('info', userinfo)
  7. def test_add(info):
  8. print(info)

输出:

  1. ============================= test session starts =============================
  2. collecting ... collected 2 items
  3. parametrize03_dic.py::test_add[info0] PASSED [ 50%]{'username': '小李', 'password': '123456'}
  4. parametrize03_dic.py::test_add[info1] PASSED [100%]{'username': '大白', 'password': '894561'}

元组(tuple)形式

  1. # 元组形式:
  2. import pytest
  3. data1 = [(1, 2, 3), (4, 5, 6)]
  4. @pytest.mark.parametrize("a,b,c", data1)
  5. def test_case1(a, b, c):
  6. print(a, b, c)

输出

  1. ============================= test session starts =============================
  2. collecting ... collected 2 items
  3. parametrize04_tuple.py::test_case1[1-2-3] PASSED [ 50%]1 2 3
  4. parametrize04_tuple.py::test_case1[4-5-6] PASSED [100%]4 5 6
  5. ============================== 2 passed in 0.03s ==============================
  6. Process finished with exit code 0

三、json形式

四、文件(txt、csv、Excel 文件)

五、数据库:读取数据库文件要导入pymysql库

六、配置conftest.py文件

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/737264
推荐阅读
相关标签
  

闽ICP备14008679号