Using with unittest
¶
logot
includes logot.unittest.LogotTestCase
for easy integration with unittest
.
The logot
attribute automatically captures logs
during tests and can be used to make log assertions:
from logot import logged
from logot.unittest import LogotTestCase
class MyAppTest(LogotTestCase):
def test_something(self) -> None:
do_something()
self.logot.assert_logged(logged.info("Something was done"))
Comparison to assertLogs()
¶
unittest
includes a assertLogs()
method that supports log capture and
testing. The above example can be rewritten using assertLogs()
as:
class MyAppTest(TestCase):
def test_something(self) -> None:
with self.assertLogs(level=logging.DEBUG) as cm:
do_something()
assert any(
record.levelno == logging.INFO and record.message == "Something was done"
for record in cm.records
)
logot
improves on assertLogs()
with:
Support for log message matching using
%
-style placeholders.Support for log pattern matching using log pattern operators.
Support for 3rd-party logging frameworks.
A cleaner, clearer syntax.
Configuring¶
Override logot
-prefixed attributes in your test case to configure the
logot
attribute:
class MyAppTest(LogotTestCase):
logot_level = "WARNING"
logot_name = "app"
logot_timeout = 10.0
See also
See logot.unittest.LogotTestCase
API reference.