[英]Package __init__ for a test
I have the following structure in a project: 我在项目中有以下结构:
app/
__init__.py
main.py
tests/
__init__.py
test_a.py
test_b.py
I would like to initialize logging for tests under the tests
package. 我想在
tests
包下初始化测试的日志记录。 I placed the initialization code in app/tests/__init__.py
under the assumption that it is run before any test is run, however I've discovered this is not the case. 我将初始化代码放在
app/tests/__init__.py
,假设它在任何测试运行之前运行,但我发现情况并非如此。
How can I make this initialization code run in the following scenarios? 如何在以下场景中运行此初始化代码?
python test_a.py
from the command prompt python test_a.py
test_a.py
as a unit-test under eclipse\\aptana\\pycharm test_a.py
作为单元测试时 You can use logger config files for normal run and tests, which is the most secure way to have different configurations. 您可以将记录器配置文件用于正常运行和测试,这是使用不同配置的最安全方式。
The 'workaround' way is to re-create loggers. “解决方法”方式是重新创建记录器。 You've probably just added new loggers in __init__.py?
您可能刚刚在__init__.py中添加了新的记录器? Once configured, logging keeps it config and only allows extending.
配置完成后,日志记录会保持配置,并且只允许扩展。 However you can try re-creating the whole logger, as described here: Python logging before you run logging.basicConfig?
但是,您可以尝试重新创建整个记录器,如下所述: 运行logging.basicConfig之前的Python日志记录? eg:
例如:
root = logging.getLogger()
if root.handlers:
for handler in root.handlers:
root.removeHandler(handler)
# now create your test config
logging.basicConfig(format='%(asctime)s %(message)s',level=logging.DEBUG)
Add this code to your test.py file, or add it into __init__.py but you might probably need: 将此代码添加到test.py文件中,或将其添加到__init__.py中,但您可能需要:
from tests import *
to make it execute in the tests. 使它在测试中执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.