Unit test log output with python

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".

Test method

testfixtures To test that the log is output, use a package called testfixtures.

pip install testfixtures

Example of use

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

application

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

Unit test log output with python
Output Python log to console with GAE
Primality test with Python
Primality test with python
python unit test template
Python log is not output with docker-compose up
Unit test flask with pytest
Test standard output with Pytest
Output log in JSON format with Python standard logging
Make apache log csv with python
Input / output with Python (Python learning memo ⑤)
python log
[Note] Hello world output with python
[python3] Implement debug log output function easily with logging and Click
[Python] Super easy test with assert statement
Stress Test with Locust written in Python
Test Python non-functional programs with GitLab CI
WebUI test with Python2.6 + Selenium 2.44.0 --profile setting
Output color characters to pretty with python
Generate Japanese test data with Python faker
Post Test 3 (Working with PosgreSQL in Python)
How to do portmanteau test with python
Integrating with setuptools / python setup.py test / pytest-runner
UnicodeEncodeError struggle with standard output of python3
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
python learning output
Twilio with Python
Integrate with Python
numpy unit test
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Python Integrity Test
Bingo with python
Zundokokiyoshi with python
Python unit tests
Excel with Python
Microcomputer with Python
Cast with python
Create test data like that with Python (Part 1)
[Python] What I did to do Unit Test
Read JSON with Python and output as CSV
I tried to output LLVM IR with Python
Log in to Yahoo Business with Selenium Python
Output python log to both console and file
Python unit test library Nose option introduction-19 types-
[Small story] Test image generation with Python / OpenCV
ARC037 Baum test politely with Python recursive function
Output test function docstring to report with pytest-html
[CentOS8] How to output Python standard output to systemd log
Serial communication with Python
Primality test by Python
Zip, unzip with python
Django 1.11 started with Python3.6
Socket communication with Python