如何在pytest夹具中获取来电者姓名?
How to get caller name inside pytest fixture?
假设我们有:
@pytest.fixture()
def setup():
print('All set up!')
return True
def foo(setup):
print('I am using a fixture to set things up')
setup_done=setup
我正在寻找一种从安装夹具中了解调用函数名称(在本例中为 foo)的方法。
到目前为止我已经尝试过:
import inspect
@pytest.fixture()
def setup():
daddy_function_name = inspect.stack()[1][3]
print(daddy_function_name)
print('All set up!')
return True
但是打印出来的是:call_fixture_func
如何通过打印 daddy_function_name
得到 foo
?
您可以在自己的夹具中使用the built-in request
fixture:
The request
fixture is a special fixture providing information of the requesting test function.
Underlying collection node (depends on current request scope).
import pytest
@pytest.fixture()
def setup(request):
return request.node.name
def test_foo(setup):
assert setup == "test_foo"
假设我们有:
@pytest.fixture()
def setup():
print('All set up!')
return True
def foo(setup):
print('I am using a fixture to set things up')
setup_done=setup
我正在寻找一种从安装夹具中了解调用函数名称(在本例中为 foo)的方法。
到目前为止我已经尝试过:
import inspect
@pytest.fixture()
def setup():
daddy_function_name = inspect.stack()[1][3]
print(daddy_function_name)
print('All set up!')
return True
但是打印出来的是:call_fixture_func
如何通过打印 daddy_function_name
得到 foo
?
您可以在自己的夹具中使用the built-in request
fixture:
The
request
fixture is a special fixture providing information of the requesting test function.
Underlying collection node (depends on current request scope).
import pytest
@pytest.fixture()
def setup(request):
return request.node.name
def test_foo(setup):
assert setup == "test_foo"