Using with pytest
¶
logot
includes a pytest
plugin.
The logot
fixture automatically captures logs during tests and can be used to make log
assertions:
from logot import Logot, logged
def test_something(logot: Logot) -> None:
do_something()
logot.assert_logged(logged.info("Something was done"))
Comparison to caplog
¶
pytest
includes a caplog
fixture that supports log capture and testing. The above example can be rewritten
using caplog
as:
def test_something(caplog: pytest.LogCaptureFixture) -> None:
do_something()
assert any(
record.levelno == logging.INFO and record.message == "Something was done"
for record in caplog.records
)
logot
improves on caplog
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.
Installing¶
Ensure logot
is installed alongside a compatible pytest
version by adding the pytest
extra:
pip install 'logot[pytest]'
See also
See Installing package extras usage guide.
Configuring¶
Use the following CLI and configuration options to configure the
pytest
plugin:
--logot-level
,logot_level
The
level
used for automatic log capturing.Defaults to
logot.Logot.DEFAULT_LEVEL
.--logot-name
,logot_name
The
name
used for automatic log capturing.Defaults to
logot.Logot.DEFAULT_NAME
.--logot-capturer
,logot_capturer
The default
capturer
for thelogot
fixture.Defaults to
logot.Logot.DEFAULT_CAPTURER
.--logot-timeout
,logot_timeout
The default
timeout
(in seconds) for thelogot
fixture.Defaults to
logot.Logot.DEFAULT_TIMEOUT
.--logot-async-waiter
,logot_async_waiter
The default
async_waiter
for thelogot
fixture.Defaults to
logot.Logot.DEFAULT_ASYNC_WAITER
.
Note
When both CLI and configuration options are given, the CLI option takes precidence.
Available fixtures¶
The following fixtures are available in the pytest
plugin:
logot:
logot.Logot
An initialized
logot.Logot
instance with log capturing enabled.logot_level:
str
|int
The
level
used for automatic log capturing.logot_name:
str
|None
The
name
used for automatic log capturing.logot_capturer:
Callable
[[],Capturer
]The default
capturer
for thelogot
fixture.logot_timeout:
float
The default
timeout
(in seconds) for thelogot
fixture.logot_async_waiter:
Callable
[[],AsyncWaiter
]The default
async_waiter
for thelogot
fixture.