【问题标题】:How can I get the parameter ID in a pytest fixture?如何在 pytest 夹具中获取参数 ID?
【发布时间】:2015-08-03 14:54:13
【问题描述】:

是否可以在 Pytest 夹具中获取参数 ID

import pytest

@pytest.fixture(
     params = ['a', 'b', 'c'],
     ids    = ['x', 'y', 'z'])
def foo(request):
   myParam = request.param
   myID    = "How do I get the current ID?"

【问题讨论】:

    标签: pytest fixtures


    【解决方案1】:

    一种方法是将 ID 作为参数传递

    import pytest
    
    params = ['a', 'b', 'c']
    ids    = ['x', 'y', 'z']
    @pytest.fixture(params=zip(params,ids), id=lambda x: x[1]):
    def foo(request):
        myParam = request.param[0]
        myID    = request.param[1]
    

    这很难看,但它有效。

    【讨论】:

    • 对@ajwood 的回答进行了一些调整(pytest==5.3.2): 1. 从@pytest.fixture(...) 行中删除冒号。 2. 将@pytest.fixture(...) 行中的id 更改为ids
    猜你喜欢
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-05
    • 2020-11-27
    • 1970-01-01
    相关资源
    最近更新 更多