无需将其添加到方法参数即可访问自动使用装置 [英] Access autouse fixture without having to add it to the method argument

查看:18
本文介绍了无需将其添加到方法参数即可访问自动使用装置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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")

有没有一种方法可以在不向测试方法添加额外"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

错误-名称错误:未定义名称‘log’

推荐答案

如果您只想将自动鼠标装置用于安装/拆卸,则通常使用它们。

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

这篇关于无需将其添加到方法参数即可访问自动使用装置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆