Trebia Project.
Trebia Project.

Reputation: 940

Change logging level during testing execution

I have an application where I introduced the standard logging library, I just set it up to WARNING.

When running unittesting I would like to avoid that those errors and warnings are appearing (just because I am making them intentionally!), but I would like to keep the verbose from unittesting.

Is there any way I can have the standard application with a logging level (WARNING) and during testing in a different one (none or CRITICAL?)

For example, I want my application in normal mode of operation to show the following:

=====
Application started
ERROR = input file is wrong
=====

However, when running my unittesting I do not want any of those outputs to appear, as I will actually make the app fail to check the correct error tracking, so it will be redundant to show the error messages and actually will complicate detecting the problems.

Looking to stackoverflow I found some similar problems, but not fixing my issue:

Is there a way to suppress printing that is done within a unit test?

Turn some print off in python unittest

Any idea/help?

Upvotes: 2

Views: 324

Answers (1)

Paul Becotte
Paul Becotte

Reputation: 9977

I'm still not 100% sure- I think what you want is to have log statements in your app that get suppressed during testing.

I would use Nosetests for this- it suppresses all stdout for passing tests and prints it for failing ones, which is just about perfect for your use case in my opinion.

A less good solution, just in case I don't understand you, is to define a test case class that all of your tests inherit from- it can have extra test methods or whatever (it should inherit from unittest.TestCase itself). The key though is that you can change the logging level to a higher/lower level in that file that only gets imported during testing, which will allow you to have special logging behavior during tests.

The behavior of nose though is the best- it still shows output on failing tests and captures print statements as well.

Upvotes: 1

Related Questions