-1

This answer https://stackoverflow.com/a/34920727 deals with testing log outputs

But what if I want to test that messages did not trigger, how do I do that. I am using unittest. For example:

    def test_HandlerEnabled(self):
        with self.assertLogs(level='DEBUG') as fake_log:
            uut=myHandler.Setup(tag="foo", enabled=True, out_stream='log')
            uut.Debug("testing")
        self.assertEqual(fake_log.output, ["DEBUG:root:foo: testing"])

    def test_HandlerDisabled(self):
        with self.assertLogs(level='DEBUG') as fake_log:
            uut=myHandler.Setup(tag="foo", enabled=False, out_stream='log')
            uut.Debug("testing")

        self.AssertThatNoLogsTriggeredOnRoot(fake_log.output) # <<<<<<<<<< here

I hereby, confirm that I have attempted to find an answer to this question myself.

4
  • I would suggest using the caplog fixture from pytest. Commented Sep 7, 2023 at 13:01
  • @zvone even if I were to use caplog from pytest, what method allows me to check that nothing triggered on the logs? Commented Sep 7, 2023 at 13:08
  • Adapt some of the code in plumberjack.blogspot.com/2010/09/unit-testing-and-logging.html Commented Sep 7, 2023 at 20:00
  • Check this post Commented Sep 8, 2023 at 7:58

1 Answer 1

0

In my case, I had to upgrade to python 3.11 so I could use assertNoLogs:

def test_HandlerDisabled(self):
    with self.assertLogs(level='DEBUG') as fake_log:
        uut=myHandler.Setup(tag="foo", enabled=False, out_stream='log')
        uut.Debug("testing")
        self.assertNoLogs('root', level = 'DEBUG') #<<<<<< use this

For previous versions, as @frankfalse suggests, implement your own method. Example here: https://stackoverflow.com/a/61381576/9962617

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.