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 8617695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:55:25+00:00 2026-06-12T05:55:25+00:00

Need py.test to log assert errors in log file from python logging module. The

  • 0

Need py.test to log assert errors in log file from python logging module.
The test has python logging module set up and all logs goes there as expected.
I used assert statements through out the test. But when encounter assertion errors,
those messages are not logged in the python logging output but in command console.

Is there a way to get py.test to log the assertion errors in the test’s logging output?

Right now the errors are in command console but it would be great if these assertion errors are also logged as part of python logging output so all the log messages are captured in one place. Also, for long running test, I cannot see the errors until the entire test finish which could be a long time to wait. It would be great if I can see the assertion error immediately so I may decide to take action.

  • 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-12T05:55:27+00:00Added an answer on June 12, 2026 at 5:55 am

    You can achieve this by using the pytest_runtest_call hook in a conftest.py file:

    import logging
    
    def pytest_runtest_call(__multicall__):
        try:
            __multicall__.execute()
        except KeyboardInterrupt:
            raise
        except:
            logging.exception('pytest_runtest_call caught exception:')
            raise
    

    The pytest_runtest_call hook is responsible for actually running the test functions, but is not responsible for catching exceptions and reporting them. This means it is the ideal place to catch an exception and hand it to logging.

    Instead of actually changing the way a test function is called this uses __multicall__ to simply call the hook which would have been called if this hook was not there.

    Note that the exception logged by hook will be much longer then the exception which would be reported by py.test normally. This is because logging does not truncate the stack to be just the test function, you could add this yourself if it is needed.

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

Sidebar

Related Questions

For a NUnit test I need to reference a .mdf file from a .config
I need to zip a file in the folder c:\projects\test\test.log I need to zip
I need to test Perl Application ( File operation , Data base operation..etc )
I need to test a controller, of action index (generated by the grails generate-all
I need to test a database user to see if that user has privileges
I need to test if a user can write to a folder before actually
I need to test my application. Its a VoIP but each time I need
I need to test the bandwidth I have on my USB RNDIS connection. I
I need a test for a variable which would evaluate to true in two
I need to test, if an instance is exactly of a given type. But

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.