如何在 pytest fixture 中获取参数ID?

8
在 Pytest fixture 中,是否有可能获取参数 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?"
1个回答

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]

这很丑,但它能用。


1
对 @ajwood 的回答 (pytest==5.3.2) 进行一些调整:1. 从 @pytest.fixture(...) 行中删除冒号。2. 将 id 改为 ids,从 @pytest.fixture(...) 行中。 - datalifenyc

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接