在@pytest.mark.parametrize中使用固定装置作为参数

问题描述 投票:0回答:1

我正在尝试使用

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)
python pytest fixtures parametrize
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):
© www.soinside.com 2019 - 2024. All rights reserved.