简体   繁体   中英

Using fixtures as parameters in @pytest.mark.parametrize

I'm trying to use the 'osc_inputs' fixture as a list of params in @pytest.mark.parametrize but it doesn't seem to take a fixture as an argument. Is there anyway of doing this?

Heres the code:

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)
        

I think you can use pytest-lazy-fixture plugin for this: 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):

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM