![](/img/trans.png)
[英]Using a fixture as several parameters in pytest.mark.parametrize
[英]Using fixtures as parameters in @pytest.mark.parametrize
我正在嘗試使用“osc_inputs”夾具作為@pytest.mark.parametrize 中的參數列表,但它似乎沒有將夾具作為參數。 反正有這樣做嗎?
繼承人的代碼:
from time import sleep
import pytest
@pytest.fixture()
def osc_inputs(simple_tcp_client):
"""Get all input addresses
Args:
simple_tcp_client: a fixture of type 'SimpleTCPClient' to send and receive messages
"""
simple_tcp_client.sendOSCMessage(address = '/*', value = ["info"])
sleep(0.06)
osc_reply = simple_tcp_client.receive()
return tuple(osc_reply.keys())
@pytest.mark.parametrize("addr", osc_inputs)
def test_osc_init_inputs(simple_tcp_client, addr):
"""Initialize device input variables with osc
Args:
simple_tcp_client: a fixture of type 'SimpleTCPClient'
addr: a fixture of types str OSC address
"""
# TCP - send set OSC message
simple_tcp_client.sendOSCMessage(address = addr, value = ["set", "48v", 0])
simple_tcp_client.sendOSCMessage(address = addr, value = ["set", "mute", 0])
simple_tcp_client.sendOSCMessage(address = addr, value = ["set", "gain", -8.0]) # gain: -8.0 -> 50.0
# TCP - send get OSC message
sleep(0.06)
simple_tcp_client.sendOSCMessage(address = addr, value = ["get"])
sleep(0.06)
osc_reply = simple_tcp_client.receive()
# Check the gain is 0
assert (osc_reply[addr]['gain'][-1] == -8.0)
# Check 48v is off
assert (osc_reply[addr]['48v'][-1] == 0)
# Check mute is off
assert (osc_reply[addr]['mute'][-1] == 0)
我認為您可以為此使用 pytest-lazy-fixture 插件: https://pypi.org/project/pytest-lazy-fixture/
@pytest.fixture()
def osc_inputs(simple_tcp_client):
""""""
return tuple(osc_reply.keys())
@pytest.mark.parametrize("addr", pytest.lazy_fixture("osc_inputs"))
def test_osc_init_inputs(simple_tcp_client, addr):
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.