首页 > 其他 > 详细

014、fixture 之 params参数(一) , ( 字典不能单独作为fixture的传参,需要被嵌套在元组、列表中 )

时间:2021-08-06 17:53:15      阅读:32      评论:0      收藏:0      [点我收藏+]

 

一、fixture 之 params参数 

     1、字典不能单独作为fixture的传参,需要被嵌套在元组、列表中 。 如果字典作为传参,只打印字典的key 。

     示例代码如下:

技术分享图片
import pytest


# fixture 参数化
# 字典不能单独作为fixture的传参,需要被嵌套在元组、列表中。

@pytest.fixture(params=data)
def user_register(request):
    user_info = request.param
    print(f\n==========={user_info})
    result = success
    return user_info, result


def test_ee(user_register):
    print(f\n***********{user_register})
View Code

    执行结果如下,只打印了字典的 key 值

技术分享图片
D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day11\venv\Scripts\python.exe "C:\SkyWorkSpace\WorkTools\PyCharm\PyCharm_Community_Edition_202003\PyCharm Community Edition 2020.3\plugins\python-ce\helpers\pycharm\_jb_pytest_runner.py" --path D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day11/ee/test_ee.py
Testing started at 12:28 ...
Launching pytest with arguments D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day11/ee/test_ee.py in D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day11\ee

============================= test session starts =============================
platform win32 -- Python 3.8.6, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day11\venv\Scripts\python.exe
cachedir: .pytest_cache
rootdir: D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day11\ee
collecting ... collected 2 items

test_ee.py::test_ee[name] 
===========name
PASSED                                         [ 50%]
***********(name, success)

test_ee.py::test_ee[pwd] 
===========pwd
PASSED                                          [100%]
***********(pwd, success)


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

Process finished with exit code 0
View Code

 

  2、把字典嵌套在列表中传参 。

  示例代码如下:

技术分享图片
import pytest

# fixture 参数化
# 原来,字典不能单独作为fixture的传参,需要被嵌套在元组、列表中。
# data = [‘sky‘, ‘123‘]
# data = {‘name‘: ‘Tony‘, ‘pwd‘: ‘456‘}
# data = (‘sky‘, ‘123‘)

# 把字典嵌套在列表中传参
data = [
    {name: Tony, pwd: 456},
    {name: jack, pwd: 789}
]

# data = (
#     {‘name‘: ‘Tony‘, ‘pwd‘: ‘456‘},
#     {‘name‘: ‘jack‘, ‘pwd‘: ‘789‘}
# )


@pytest.fixture(params=data)
def user_register(request):
    user_info = request.param
    print(f\n==========={user_info})
    result = success
    return user_info, result


def test_ee(user_register):
    print(f\n***********{user_register})
View Code

  执行结果如下:

技术分享图片
D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day11\venv\Scripts\python.exe "C:\SkyWorkSpace\WorkTools\PyCharm\PyCharm_Community_Edition_202003\PyCharm Community Edition 2020.3\plugins\python-ce\helpers\pycharm\_jb_pytest_runner.py" --path D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day11/ee/test_ee.py
Testing started at 12:32 ...
Launching pytest with arguments D:/SkyWorkSpace/WorkSpace/Pytest/Temp/day11/ee/test_ee.py in D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day11\ee

============================= test session starts =============================
platform win32 -- Python 3.8.6, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day11\venv\Scripts\python.exe
cachedir: .pytest_cache
rootdir: D:\SkyWorkSpace\WorkSpace\Pytest\Temp\day11\ee
collecting ... collected 2 items

test_ee.py::test_ee[user_register0] 
==========={name: Tony, pwd: 456}
PASSED                               [ 50%]
***********({name: Tony, pwd: 456}, success)

test_ee.py::test_ee[user_register1] 
==========={name: jack, pwd: 789}
PASSED                               [100%]
***********({name: jack, pwd: 789}, success)


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

Process finished with exit code 0
View Code

 

  

 

014、fixture 之 params参数(一) , ( 字典不能单独作为fixture的传参,需要被嵌套在元组、列表中 )

原文:https://www.cnblogs.com/qq-2780619724/p/15108010.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!