我们的想法是为类的每个"端口"参数化重新创建"连接器"固定装置。然而,目前它只为整个测试类创建一次fixture,而不是在每个param迭代中创建。如何使fixture仅适用于类的每个参数化。
conftest.py-
@pytest.fixture(scope="class")
def connector():
# RUN ON CREATE ...
# connect to TCP
yield tcp_client
# RUN ON DESTROY ...
# disconnect and destroy
test_device.py-
@pytest.mark.parametrize('ports', [0, 2], scope="class")
class TestDevice:
device_info = load_device_info('device_model')['device']
def test_power_on(self, switcher, ports):
# switch power on for the port the device is on
@pytest.mark.parametrize('inputs', device_info['inputs'])
def test_init_inputs(self, inputs, connector, ports):
# send some messages over TCP network and set the inputs for the device
# destroy and recreate the simple_tcp_client fixture after each iteration of the class parametrization when it reaches this function ...
@pytest.mark.parametrize("samplerate", [samplerate for samplerate in SampleRate])
def test_run_device_tests(self, samplerate, ports):
# run tests on each device with different samplerates ...
我也在conftest中尝试过,但遇到了同样的问题:
@pytest.fixture(params=[0, 2], scope='class')
def ports(request):
return request.param
我想要的结构:
TestClass [port: '1', 2]
├─ power_on [port: 1]
├─ init_inputs [input: 1, 2], [new connector]
│ ├─ set inputs[1]
│ ├─ set inputs[2]
├─ run_test [samplerate [44100, 96000 ...]
│ ├─ run with [44100]
│ ├─ run with [96000]
TestClass [port: 1, '2']
├─ power_on [port: 2]
├─ init_inputs [input: 1, 2], [new connector]
│ ├─ set inputs[1]
│ ├─ set inputs[2]
├─ run_test [samplerate [44100, 96000 ...]
│ ├─ run with [44100]
│ ├─ run with [96000]
目前运行方式:
TestClass [port: '1', 2]
├─ power_on [port: 1]
├─ init_inputs [input: 1, 2], [new connector]
│ ├─ set inputs[1]
│ ├─ set inputs[2]
├─ run_test [samplerate [44100, 96000 ...]
│ ├─ run with [44100]
│ ├─ run with [96000]
TestClass [port: 1, '2']
├─ power_on [port: 2]
├─ init_inputs [input: 1, 2], [same connector]
│ ├─ set inputs[1]
│ ├─ set inputs[2]
├─ run_test [samplerate [44100, 96000 ...]
│ ├─ run with [44100]
│ ├─ run with [96000]
我是pytest的新手,所以也许我没有正确使用夹具和参数化?
您是否尝试过不将class
作用域用于connector
固定装置?
@pytest.fixture
def connector():
# RUN ON CREATE ...
# connect to TCP
yield tcp_client
# RUN ON DESTROY ...
# disconnect and destroy