如何将变量从 pytest fixture 转移到实际测试?
How to carry over variable from pytest fixture to an actual test?
我有一个 pytest 依赖 setup/teardown 到 create/delete 运动流的夹具:
@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
有什么方法可以将 stream_name 从夹具传递到实际的 test_func1 和 test_func2?
我不能使用全局变量,因为每个测试都需要有自己的流来进行测试。
生成测试夹具的值并将夹具作为参数传递到每个测试中。
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)
我有一个 pytest 依赖 setup/teardown 到 create/delete 运动流的夹具:
@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
有什么方法可以将 stream_name 从夹具传递到实际的 test_func1 和 test_func2?
我不能使用全局变量,因为每个测试都需要有自己的流来进行测试。
生成测试夹具的值并将夹具作为参数传递到每个测试中。
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)