如何在 pytest 夹具中获取参数 ID?

ajw*_*ood 5 fixtures pytest

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

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?"
Run Code Online (Sandbox Code Playgroud)

ajw*_*ood 1

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

import pytest

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

这很丑陋,但它有效。