我在conftest.py中有一个会话范围的夹具
@pytest.fixture(scope="session",autouse=True)
def log(request):
testlog = LogUtil(testconf.LOG_NAME).get()
return testlog
Run Code Online (Sandbox Code Playgroud)
当mytest.py中定义了测试方法时,这将加载并按预期工作,如下所示:
def test_hello_world(self, log):
log.info("hello world test")
Run Code Online (Sandbox Code Playgroud)
有没有办法使用夹具(因为它启用了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
Run Code Online (Sandbox Code Playgroud)
错误 - NameError:未定义名称"log"
当您只想将自动夹具用于设置/拆卸时,您通常会使用它们。
如果您需要访问夹具返回的对象,则需要像在第一个示例中一样将其指定为参数。
您的日志功能不是固定装置。像任何其他库例程一样对待它。例如:将其添加到公共模块并根据需要导入。
common.py
import logging
import os
logger = logging.getLogger()
project_id = os.getenv('PROJECT_ID', 'maris-baccus')
Run Code Online (Sandbox Code Playgroud)
test_stuff.py
from common import logger, project_id
def test_this():
logger.info(f'This is the {project_id}')
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
645 次 |
最近记录: |