Hello sekitaka.
I usually output logs casually, but sometimes I want to test that the logs are output. This article will show you how to test that "logs are being output".
testfixtures To test that the log is output, use a package called testfixtures.
pip install testfixtures
The following example tests that a function called put_log outputs a log called foo
at the INFO level.
# coding=utf-8
from testfixtures import LogCapture
import logging
from unittest import TestCase
import unittest
logger = logging.getLogger()
def put_log(message):
logger.info(message)
class Test(TestCase):
def test_put_log(self):
with LogCapture() as l:
put_log("foo")
l.check(
("root","INFO","foo")
)
if __name__ == '__main__':
unittest.main
The argument of LogCapture ()
allows you to filter the name of the logger, the log level to capture, and so on.
Also, by setting log_str = str (l)
, you can get the full text of the log in the following format as a character string, so it may be good to test with assertions such as regular expression matching.
root INFO
foo
But I'm always worried about how far to test
Recommended Posts