在 Python Django 中运行单元测试时如何禁用日志记录?
我正在使用基于简单单元测试的测试运行器来测试我的 Django 应用程序。
我的应用程序本身配置为在 settings.py 中使用基本记录器,使用:
logging.basicConfig(level=logging.DEBUG)
在我的应用程序代码中使用:
logger = logging.getLogger(__name__)
logger.setLevel(getattr(settings, 'LOG_LEVEL', logging.DEBUG))
但是,在运行单元测试时,我想禁用日志记录,以便它不会扰乱我的测试结果输出。有没有一种简单的方法可以以全局方式关闭日志记录,以便应用程序特定的记录器在我运行测试时不会将内容写入控制台?
I am using a simple unit test based test runner to test my Django application.
My application itself is configured to use a basic logger in settings.py using:
logging.basicConfig(level=logging.DEBUG)
And in my application code using:
logger = logging.getLogger(__name__)
logger.setLevel(getattr(settings, 'LOG_LEVEL', logging.DEBUG))
However, when running unittests, I'd like to disable logging so that it doesn't clutter my test result output. Is there a simple way to turn off logging in a global way, so that the application specific loggers aren't writing stuff out to the console when I run tests?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(21)
将禁用严重程度低于或等于
CRITICAL
的所有日志记录调用。可以使用以下命令重新启用日志记录will disable all logging calls with levels less severe than or equal to
CRITICAL
. Logging can be re-enabled with由于您使用的是 Django,因此您可以将这些行添加到您的 settings.py 中:
这样您就不必在测试中的每个
setUp()
中添加该行。您还可以通过这种方式根据您的测试需求进行一些方便的更改。
还有另一种“更好”或“更干净”的方法可以为您的测试添加细节,那就是创建您自己的测试运行程序。
只需创建一个如下所示的类:
现在添加到您的 settings.py 文件中:
这可以让您进行一项非常方便的修改,而其他方法则无法做到这一点,即让 Django 只测试您想要的应用程序。您可以通过更改
test_labels
并将此行添加到测试运行器来做到这一点:Since you are in Django, you could add these lines to your settings.py:
That way you don't have to add that line in every
setUp()
on your tests.You could also do a couple of handy changes for your test needs this way.
There is another "nicer" or "cleaner" way to add specifics to your tests and that is making your own test runner.
Just create a class like this:
And now add to your settings.py file:
This lets you do one really handy modification that the other approach doesn't, which is to make Django just tests the applications that you want. You can do that by changing the
test_labels
adding this line to the test runner:其他答案通过全局设置日志记录基础设施以忽略任何内容来防止“将内容写入控制台”。这可行,但我发现这种方法太生硬了。我的方法是执行配置更改,该更改仅执行防止日志在控制台上泄露所需的操作。因此,我将 自定义日志记录过滤器 添加到我的
设置中.py
:我配置 Django记录以使用过滤器:
最终结果:当我测试时,没有任何内容发送到控制台,但其他一切都保持不变。
为什么要这样做?
我设计的代码包含仅在特定情况下触发的日志记录指令,并且在出现问题时应该输出诊断所需的准确数据。因此,我测试他们做了他们应该做的事情,因此完全禁用日志记录对我来说是不可行的。我不想在软件投入生产后发现我认为会记录的内容没有被记录。
此外,一些测试运行器(例如Nose)会在测试期间捕获日志,并将日志的相关部分与测试失败一起输出。它对于找出测试失败的原因很有用。如果日志记录完全关闭,则无法捕获任何内容。
The other answers prevent "writing stuff out to the console" by globally setting the logging infrastructure to ignore anything. This works but I find it too blunt an approach. My approach is to perform a configuration change which does only what's needed to prevent logs to get out on the console. So I add a custom logging filter to my
settings.py
:And I configure the Django logging to use the filter:
End result: when I'm testing, nothing goes to the console, but everything else stays the same.
Why Do This?
I design code that contains logging instructions that are triggered only in specific circumstances and that should output the exact data I need for diagnosis if things go wrong. Therefore I test that they do what they are supposed to do and thus completely disabling logging is not viable for me. I don't want to find once the software is in production that what I thought would be logged is not logged.
Moreover, some test runners (Nose, for instance) will capture logs during testing and output the relevant part of the log together with a test failure. It is useful in figuring out why a test failed. If logging is completely turned off, then there's nothing that can be captured.
我喜欢 Hassek 的自定义测试运行器想法。需要注意的是,
DjangoTestSuiteRunner
不再是 Django 1.6+ 中的默认测试运行器,它已被DiscoverRunner
取代。对于默认行为,测试运行程序应该更像:I like Hassek's custom test runner idea. It should be noted that
DjangoTestSuiteRunner
is no longer the default test runner in Django 1.6+, it has been replaced by theDiscoverRunner
. For default behaviour, the test runner should be more like:我发现,对于
unittest
或类似框架中的测试,安全禁用单元测试中不需要的日志记录的最有效方法是在setUp
/<特定测试用例的 code>tearDown 方法。这可以让我们专门针对应禁用日志的位置。您还可以在您正在测试的类的记录器上明确执行此操作。I've found that for tests within
unittest
or similar a framework, the most effective way to safely disable unwanted logging in unit tests is to enable/disable in thesetUp
/tearDown
methods of a particular test case. This lets one target specifically where logs should be disabled. You could also do this explicitly on the logger of the class you're testing.我正在使用一个简单的方法装饰器来仅在特定的测试方法中禁用日志记录。
然后我按照以下示例使用它:
I am using a simple method decorator to disable logging only in a particular test method.
And then I use it as in the following example:
有一些漂亮且干净的方法可以使用
unittest.mock.patch
方法暂停测试中的日志记录。foo.py:
测试.py:
并且 python3 -m unittest 测试将不会产生任何日志输出。
There is some pretty and clean method to suspend logging in tests with
unittest.mock.patch
method.foo.py:
tests.py:
And
python3 -m unittest tests
will produce no logging output.有时您需要日志,有时则不需要。我的
settings.py
中有此代码,因此,如果您使用
--no-logs
选项运行测试,您将仅获得关键
日志:如果您想加快持续集成流程的测试速度,这将非常有帮助。
Sometimes you want the logs and sometimes not. I have this code in my
settings.py
So if you run your test with the
--no-logs
options you'll get only thecritical
logs:It's very helpful if you want speedup the tests on your continuous integration flow.
如果我希望暂时抑制特定的记录器,我已经编写了一个我发现有用的小上下文管理器:
然后您可以像这样使用它:
这具有重新启用记录器(或设置回其状态)的优点之前的状态)一旦
with
完成。In cases where I wish to temporarily suppress a specific logger, I've written a little context manager that I've found useful:
You then use it like:
This has the advantage that the logger is re-enabled (or set back to its prior state) once the
with
completes.禁用整个模块的日志记录:
Disable logging for the entire module:
如果您使用的是 pytest:
由于 pytest 捕获日志消息并且仅在失败的测试中显示它们,因此您通常不想禁用任何日志记录。相反,使用单独的
settings.py
文件进行测试(例如,test_settings.py
),并向其中添加:这告诉 Django 完全跳过配置日志记录。
LOGGING
设置将被忽略,并且可以从设置中删除。使用这种方法,您不会获得通过的测试的任何日志记录,并且会获得失败的测试的所有可用日志记录。
测试将使用 pytest 设置的日志记录运行。可以根据您的喜好在
pytest
设置中对其进行配置(例如,tox.ini
)。要包含调试级别日志消息,请使用log_level = DEBUG
(或相应的命令行参数)。If you're using
pytest
:Since pytest captures log messages and only displays them for failed tests, you typically don't want to disable any logging. Instead, use a separate
settings.py
file for tests (e.g.,test_settings.py
), and add to it:This tells Django to skip configuring the logging altogether. The
LOGGING
setting will be ignored and can be removed from the settings.With this approach, you don't get any logging for passed tests, and you get all available logging for failed tests.
The tests will run using the logging that was set up by
pytest
. It can be configured to your liking in thepytest
settings (e.g.,tox.ini
). To include debug level log messages, uselog_level = DEBUG
(or the corresponding command line argument).您可以将其放在单元测试
__init__.py
文件的顶级目录中。这将在单元测试套件中全局禁用日志记录。You can put this in the top level directory for unit tests
__init__.py
file. This will disable logging globally in the unit test suite.我的一些测试包含有关日志输出的断言,因此更改日志级别会破坏它们。相反,我更改了 Django
LOGGING
设置以使用Some of my tests contain assertions about log output, so changing the log level breaks them. Instead, I changed my Django
LOGGING
settings to use a NullHandler when running tests:如果您不希望在单元测试的 setUp() 和 TeaDown() 中重复打开/关闭它(看不到原因),您可以每个类执行一次:
If you don't want it repeatedly turn it on/off in setUp() and tearDown() for unittest (don't see the reason for that), you could just do it once per class:
bh
对我来说效果很好 - 在 conftest.py 中:
b.h.
for me works whell - in conftest.py:
就我而言,我有一个专门为测试目的创建的设置文件
settings/test.py
,如下所示:我将一个环境变量
DJANGO_SETTINGS_MODULE=settings.test
添加到/etc/environment
。In my case I have a settings file
settings/test.py
created specifically for testing purposes, here's what it looks like:I put an environment variable
DJANGO_SETTINGS_MODULE=settings.test
to/etc/environment
.如果您有不同的初始化程序模块用于测试、开发和生产,那么您可以禁用任何内容或在初始化程序中重定向它。我有 local.py、test.py 和 production.py,它们都继承自 common.y
common.py 执行所有主要配置,包括此代码片段:
然后在 test.py 中我有这个:
这用 FileHandler 替换了控制台处理程序,并且意味着仍然可以进行日志记录,但我不必接触生产代码库。
If you have different initaliser modules for test, dev and production then you can disable anything or redirect it in the initialser. I have local.py, test.py and production.py that all inherit from common.y
common.py does all the main config including this snippet :
Then in test.py I have this:
This replaces the console handler with a FileHandler and means still get logging but I do not have to touch the production code base.
如果您使用 pytest,您可以安装超级有用的 pytest-mock 插件并定义一个自动使用的、会话范围内的固定装置,可以由环境变量触发:
In case you're using pytest, you can install the super useful pytest-mock plugin and define an auto-used, session-scoped fixture that can be triggered by an env var:
我们使用structlog,禁用它有点复杂:
We use structlog, which is a bit more complicated to disable:
如果您在 2021 年或以后想知道这个问题,您可能会问错问题
在现代版本的 Django* 上,使用开箱即用的配置,测试不应在屏幕上生成任何日志记录。因此,如果您问这个问题,真正的答案可能是“某些内容配置错误”。这是因为(默认情况下):
DEBUG=False
运行因此,如果您使用的记录器与您在
LOGGING['loggers']
中定义的记录器匹配并由处理>“console”
处理程序,测试不应在屏幕上生成任何日志记录。如果您在测试中看到某些内容,则
settings.LOGGING
中定义的内容不匹配,DEBUG=True
运行测试(需要覆盖)“require_debug_true”
。*现代版本含义:2.1 及更高版本,即不是古代的。
You're likely asking the wrong question if you're wondering about this in 2021 or later
On modern versions of Django*, with the out of the box configuration, tests should not produce any logging on screen. Thus, if you're aksing this question the real answer may be "something is misconfigured". This is because (by default):
DEBUG=False
Thus, if the loggers you use match those you have defined in
LOGGING['loggers']
and being handled by the"console"
handler, tests should not produce any logging on screen.If you see something in tests anyway, you either
settings.LOGGING
DEBUG=True
(which requires an override)"require_debug_true"
from your console handler's filters.*Modern versions meaning: 2.1 and up, i.e. not ancient.