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

 

一、fixture 之 params参数 

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

     示例代码如下:

014、fixture 之 params参数(一)  , ( 字典不能单独作为fixture的传参,需要被嵌套在元组、列表中 )
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 值

014、fixture 之 params参数(一)  , ( 字典不能单独作为fixture的传参,需要被嵌套在元组、列表中 )
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、把字典嵌套在列表中传参 。

  示例代码如下:

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

  执行结果如下:

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

上一篇:.NET 对象的序列化和反序列化


下一篇:k8s pod启动后停止问题解决