我希望能够修改显示的测试ID字符串以使其更具可读性。我的用例是一个测试,该测试向灯具提供可接受的设备列表,然后将其与可用设备进行匹配。
import pytest
import random
AVAILABLE_FISH = ["TUNA", "SALMON", "TROUT", "ANCHOVIES", "LIONFISH", "PUFFERFISH"]
@pytest.fixture(scope="function")
def cooked_fish(acceptable_fish):
"""
Takes a list of acceptable fish and matches them with what is available in the kitchen.
"""
matches = [f for f in acceptable_fish if f in AVAILABLE_FISH]
assert len(matches) > 0, "We couldn't find the right fish for you. May we suggest the lobster?"
return random.choice(matches)
@pytest.mark.parametrize("acceptable_fish", [["CATFISH", "ANCHOVIES"]])
def test_feed_me(cooked_fish):
assert is_tasty(cooked_fish), "This fish is NOT tasty"
理想情况下将显示为:
test_feed_me[ANCHOVIES]
但显示为:
test_feed_me[acceptable_fishes0]
我知道可以在ids
的{{1}} arg和钩子@pytest.mark.parametrize
中设置参数id。但是,这两个选项都不起作用,因为它们在选择夹具选择了合适的设备之前就将ID设置为 ,因此对打印任何有用的信息都不了解。
我认为解决方案是在pytest_make_parametrize_id
固定装置内以某种方式设置ID。我戳了cooked_fish
pytest句柄,但没有看到可以轻易覆盖的任何东西。有什么想法吗?