2

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:

  • The problem is with print, not with logging

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

  • Just eliminating part of test verbosity

Turn some print off in python unittest

Any idea/help?

Trebia Project.
  • 930
  • 2
  • 17
  • 36
  • I'm not sure exactly what you want to accomplish. You said you want to turn them off during testing, but you want to turn them on during testing, but you're not interested in logging, what you really want is print statements? Try and describe the exact behavior that you are having, and what you would like that behavior to be. – Paul Becotte Aug 09 '17 at 21:28
  • @PaulBecotte thanks for taking interest in my issue. Essentially I want to have a different behaviour in testing and in normal app operation. I will put some examples to make it more explicit – Trebia Project. Aug 09 '17 at 21:34

1 Answers1

1

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.

Paul Becotte
  • 9,767
  • 3
  • 34
  • 42
  • Well... you actually get the point. Changing the unittest standard module that I was using to nose got me some time to do... so I crosscheck everything and works quite well! Is EXACTLY what I was looking for, thanks!. One quick question just seen in the documentation that nose is in maintenance, no further development expected. Do you recommend nose2? – Trebia Project. Aug 10 '17 at 15:39
  • No, from the reading I saw I didn't see any value in upgrading. Have been using it for about 3 years now- maybe someday there will be a reason to upgrade, but it seems to work good for us. – Paul Becotte Aug 10 '17 at 18:01