如何在 pytest conftest.py 中导入之前设置配置?
how to set config before import in pytest conftest.py?
我有myprj
个项目,文件如下。
$ tree myprj/
myprj/
├── prj
│ ├── __init__.py
│ ├── config.py
│ ├── my_logger.py
│ ├── my_module.py
│ └── tests
│ ├── __init__.py
│ ├── conftest.py
│ └── unit
│ ├── __init__.py
│ └── test_my_module.py
├── setup.py
└── tox.ini
config.py
LOG_FILE='/path/exists/on/production/prj.log'
my_logger.py
from prj import config
import logging
class MyLogger():
def __init__(self):
self.setup_logger()
def setup_logger(self):
logFilename = config.LOG_FILE
file_hdlr = logging.FileHandler(logFilename)
my_module.py
from prj import my_logger
myLogger = my_logger.MyLogger()
def my_method():
return 1
test_my_module.py
from prj import my_module
def test_my_method():
assert 1 == my_module.my_method()
setup.py
from setuptools import setup
setup(name="Tox Testing")
tox.ini
[tox]
envlist = py35
[testenv]
deps=pytest
commands=py.test
当我 运行 tox
时,日志文件失败 path not exists
。
GLOB sdist-make: /private/tmp/myprj/setup.py
py35 inst-nodeps: /private/tmp/myprj/.tox/dist/Tox Testing-0.0.0.zip
py35 installed: attrs==17.4.0,pluggy==0.6.0,py==1.5.2,pytest==3.3.2,six==1.11.0,Tox-Testing==0.0.0
py35 runtests: PYTHONHASHSEED='2231398989'
py35 runtests: commands[0] | py.test
========================================================================================================= test session starts =========================================================================================================
platform darwin -- Python 3.5.2, pytest-3.3.2, py-1.5.2, pluggy-0.6.0
rootdir: /private/tmp/myprj, inifile:
collected 0 items / 1 errors
=============================================================================================================== ERRORS ================================================================================================================
__________________________________________________________________________________________ ERROR collecting prj/tests/unit/test_my_module.py __________________________________________________________________________________________
prj/tests/unit/test_my_module.py:1: in <module>
from prj import my_module
prj/my_module.py:3: in <module>
myLogger = my_logger.MyLogger()
prj/my_logger.py:6: in __init__
self.setup_logger()
prj/my_logger.py:11: in setup_logger
file_hdlr = logging.FileHandler(logFilename)
/Users/nile2691/.pyenv/versions/3.5.2/lib/python3.5/logging/__init__.py:1008: in __init__
StreamHandler.__init__(self, self._open())
/Users/nile2691/.pyenv/versions/3.5.2/lib/python3.5/logging/__init__.py:1037: in _open
return open(self.baseFilename, self.mode, encoding=self.encoding)
E FileNotFoundError: [Errno 2] No such file or directory: '/path/exists/on/production/prj.log'
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
======================================================================================================= 1 error in 0.36 seconds =======================================================================================================
ERROR: InvocationError: '/private/tmp/myprj/.tox/py35/bin/py.test'
_______________________________________________________________________________________________________________ summary _______________________________________________________________________________________________________________
ERROR: py35: commands failed
我明白了,日志文件路径在我的本地系统中不存在,它给出了错误。
我尝试使用 conftest.py
fixture 在 config.py
中设置 LOG_FILE
变量。
conftest.py
import pytest
from prj import config
@pytest.fixture(scope="session", autouse=True)
def set_config():
config.LOG_FILE = '/tmp/prj.log'
但还是 return 同样的错误。如果我能够在开始测试执行之前调用某些东西,比如 py.test
的第一个执行步骤,那么我可以设置 LOG_FILE
并且它不会引发错误。
在 my_module.py
的全局范围内,您创建一个实例 my_logger.MyLogger()
,并在 MyLogger.__init__
中设置日志记录处理程序。
您的问题是在导入时设置日志处理程序的反模式。不要那样做。
对于应用程序,应在运行时配置日志处理程序(由 main()
或其他人完成)。对于库,根本不应配置日志记录处理程序(possibly adding a NullHandler
除外)- 由库的用户决定他们希望如何配置处理程序。
请注意,pytest 不需要已经配置日志记录处理程序来测试记录的记录和对日志事件进行断言 - caplog
fixture 将插入自己的处理程序以捕获日志记录事件, 因此它甚至可以用于未配置的日志系统。
我有myprj
个项目,文件如下。
$ tree myprj/
myprj/
├── prj
│ ├── __init__.py
│ ├── config.py
│ ├── my_logger.py
│ ├── my_module.py
│ └── tests
│ ├── __init__.py
│ ├── conftest.py
│ └── unit
│ ├── __init__.py
│ └── test_my_module.py
├── setup.py
└── tox.ini
config.py
LOG_FILE='/path/exists/on/production/prj.log'
my_logger.py
from prj import config
import logging
class MyLogger():
def __init__(self):
self.setup_logger()
def setup_logger(self):
logFilename = config.LOG_FILE
file_hdlr = logging.FileHandler(logFilename)
my_module.py
from prj import my_logger
myLogger = my_logger.MyLogger()
def my_method():
return 1
test_my_module.py
from prj import my_module
def test_my_method():
assert 1 == my_module.my_method()
setup.py
from setuptools import setup
setup(name="Tox Testing")
tox.ini
[tox]
envlist = py35
[testenv]
deps=pytest
commands=py.test
当我 运行 tox
时,日志文件失败 path not exists
。
GLOB sdist-make: /private/tmp/myprj/setup.py
py35 inst-nodeps: /private/tmp/myprj/.tox/dist/Tox Testing-0.0.0.zip
py35 installed: attrs==17.4.0,pluggy==0.6.0,py==1.5.2,pytest==3.3.2,six==1.11.0,Tox-Testing==0.0.0
py35 runtests: PYTHONHASHSEED='2231398989'
py35 runtests: commands[0] | py.test
========================================================================================================= test session starts =========================================================================================================
platform darwin -- Python 3.5.2, pytest-3.3.2, py-1.5.2, pluggy-0.6.0
rootdir: /private/tmp/myprj, inifile:
collected 0 items / 1 errors
=============================================================================================================== ERRORS ================================================================================================================
__________________________________________________________________________________________ ERROR collecting prj/tests/unit/test_my_module.py __________________________________________________________________________________________
prj/tests/unit/test_my_module.py:1: in <module>
from prj import my_module
prj/my_module.py:3: in <module>
myLogger = my_logger.MyLogger()
prj/my_logger.py:6: in __init__
self.setup_logger()
prj/my_logger.py:11: in setup_logger
file_hdlr = logging.FileHandler(logFilename)
/Users/nile2691/.pyenv/versions/3.5.2/lib/python3.5/logging/__init__.py:1008: in __init__
StreamHandler.__init__(self, self._open())
/Users/nile2691/.pyenv/versions/3.5.2/lib/python3.5/logging/__init__.py:1037: in _open
return open(self.baseFilename, self.mode, encoding=self.encoding)
E FileNotFoundError: [Errno 2] No such file or directory: '/path/exists/on/production/prj.log'
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
======================================================================================================= 1 error in 0.36 seconds =======================================================================================================
ERROR: InvocationError: '/private/tmp/myprj/.tox/py35/bin/py.test'
_______________________________________________________________________________________________________________ summary _______________________________________________________________________________________________________________
ERROR: py35: commands failed
我明白了,日志文件路径在我的本地系统中不存在,它给出了错误。
我尝试使用 conftest.py
fixture 在 config.py
中设置 LOG_FILE
变量。
conftest.py
import pytest
from prj import config
@pytest.fixture(scope="session", autouse=True)
def set_config():
config.LOG_FILE = '/tmp/prj.log'
但还是 return 同样的错误。如果我能够在开始测试执行之前调用某些东西,比如 py.test
的第一个执行步骤,那么我可以设置 LOG_FILE
并且它不会引发错误。
在 my_module.py
的全局范围内,您创建一个实例 my_logger.MyLogger()
,并在 MyLogger.__init__
中设置日志记录处理程序。
您的问题是在导入时设置日志处理程序的反模式。不要那样做。
对于应用程序,应在运行时配置日志处理程序(由 main()
或其他人完成)。对于库,根本不应配置日志记录处理程序(possibly adding a NullHandler
除外)- 由库的用户决定他们希望如何配置处理程序。
请注意,pytest 不需要已经配置日志记录处理程序来测试记录的记录和对日志事件进行断言 - caplog
fixture 将插入自己的处理程序以捕获日志记录事件, 因此它甚至可以用于未配置的日志系统。