首页 文章

访问autouse fixture而不必将其添加到method参数

提问于
浏览
5

我在conftest.py中有一个会话范围的夹具

@pytest.fixture(scope="session",autouse=True)
def log(request):
    testlog = LogUtil(testconf.LOG_NAME).get()
    return testlog

当mytest.py中定义了测试方法时,这将加载并按预期工作,如下所示:

def test_hello_world(self, log):
        log.info("hello world test")

有没有办法使用夹具(因为它启用了autouse)而无需在测试方法中添加额外的“log”参数?

def test_hello_world(self):
        log.info("hello world test") #log is not found

@pytest.mark.usefixtures("log")
def test_hello_world2(self):
        log.info("hello world test") #log is not found

def test_hello_world3(self,log):
        log.info("hello world test") #log object is accessible here

错误 - NameError:未定义名称“log”

1 回答

  • 1

    当您只想使用autouse灯具进行设置/拆卸时,通常会使用它们 .

    如果需要访问fixture返回的对象,则需要将其指定为参数,如第一个示例所示 .

相关问题