Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8142353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:47:33+00:00 2026-06-06T12:47:33+00:00

How could you write a unittest in Python that would test that the output

  • 0

How could you write a unittest in Python that would test that the output from a logger is indeed in the format you expect (i.e. set through a call to logging.basicConfig())? I’m thinking along the lines of a custom StreamHandler and use of the ‘re’ library but it doesn’t look like LogRecord passed to StreamHandler.emit() can give me the string that will be output.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T12:47:35+00:00Added an answer on June 6, 2026 at 12:47 pm

    From the documentation (http://packages.python.org/testfixtures/logging.html):

    To help with this, TestFixtures allows you to easily capture the output of calls to Python’s logging framework and make sure they were as expected. There are three different techniques, depending on the type of test you are writing.

    1. The context manager
    2. The decorator
    3. The manual usage

    The examples are included in the documentation. The shortened version is below.

    The context manager

    >>> import logging
    >>> from testfixtures import LogCapture
    >>> with LogCapture() as l:
    ...     logger = logging.getLogger()
    ...     logger.info('a message')
    ...     logger.error('an error')
    

    And after that you can check the logs for equality:

    >>> l.check(
    ...     ('root', 'INFO', 'a message'),
    ...     ('root', 'ERROR', 'another error'),
    ...     )
    Traceback (most recent call last):
     ...
    AssertionError: Sequence not as expected:
    
    same:
    (('root', 'INFO', 'a message'),)
    
    first:
    (('root', 'ERROR', 'another error'),)
    
    second:
    (('root', 'ERROR', 'an error'),)
    

    The decorator

    Similar to the previous, but applied to specific function:

    from testfixtures import log_capture
    
    @log_capture()
    def test_function(l):
        logger = logging.getLogger()
        logger.info('a message')
        logger.error('an error')
    
        l.check(
            ('root', 'INFO', 'a message'),
            ('root', 'ERROR', 'an error'),
            )
    

    Manual usage

    >>> from testfixtures import LogCapture
    >>> l = LogCapture()
    

    After which you can also "check" the logs:

    >>> l.check(('root', 'INFO', 'a message'))
    <...>
    

    EDIT: To access specific logs and analyze them in a custom way, you can just iterate through l.records (where l is just LogCapture‘s instance) and access some properties of each of them (eg. msg contains message sent to logger, levelname contains codename of the level, and there are plenty other properties).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wish I could write LINQ to Entity queries that called other functions: from
Is it useful to write a standardised TDD [Test] method that would expose common
How could I write code to get all the action names from a controller
I'm trying to write a unit test for a class that generates distinct strings.
I want to write a unit test to ensure that the view I am
I'm trying to write a unit test for a module that will give me
I'm using Python's built-in unittest module and I want to write a few tests
You want to write unittest -cases for a function like that: def test_me(a): for
Sure I could write this myself, but before I go reinventing the wheel is
Is there a piece of code I could write to be notified when a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.