python—如何将变量从pytest夹具传递到实际测试?

hl0ma9xz  于 2021-09-08  发布在  Java
关注(0)|答案(1)|浏览(318)

我有一个pytest,它依赖于设置/拆卸夹具来创建/删除运动流:

@pytest.fixture()
def clean_up_kinesis_test():
    stream_name = uuid.uuid4().hex
    api_utils.create_kinesis_stream('us-east-1', stream_name, 1)
    assert active
    yield
    api_utils.delete_kinesis_stream('us-east-1', stream_name)

@pytest.mark.usefixtures("clean_up_kinesis_test")
def test_func1():
    # Use the stream_name from the fixture to do further testing

@pytest.mark.usefixtures("clean_up_kinesis_test")
def test_func2():
    # Use the stream_name from the fixture to do further testing

是否有一种方法可以将流_名称从夹具传递到实际的test_func1和test_func2?
我不能使用全局变量,因为每个测试都需要有自己的流来进行测试。

pxyaymoc

pxyaymoc1#

从测试夹具中生成值,并将夹具作为参数传递到每个测试中。

import pytest
import uuid

@pytest.fixture()
def clean_up_kinesis_test():
    stream_name = uuid.uuid4().hex
    yield stream_name

def test_func1(clean_up_kinesis_test):
    print(clean_up_kinesis_test)

def test_func2(clean_up_kinesis_test):
    print(clean_up_kinesis_test)

相关问题