pytest fixture-使用标记将数据传递给fixture
使用请求对象,fixture还可以访问应用到测试函数的标记。这对于将数据从测试传递到fixture非常有用:
import pytest
@pytest.fixture
def fixt(request):
marker = request.node.get_closest_marker("fixt_data")
if marker is None:
# Handle missing marker in some way...
data = None
else:
data = marker.args[0]
# Do something with the data
return data
@pytest.mark.fixt_data(42)
def test_fixt(fixt):
assert fixt == 42