Skip to main content

Log Assertion.

Project description

What?

A simple log assertion mechanism for Python unittests.

Why?

As is vox populi, you must also test the logging calls in your programs.

With logassert this is now very easy.

Awesome! How to use it?

All you need to do is to call this module’s setup() passing the test case instance, and the logger you want to supervise.

Like:

class MyTestCase(unittest.TestCase):
    """Example."""

    def setUp(self):
        logassert.setup(self, 'mylogger')

In the example, mylogger is the name of the logging to supervise. If different subsystems of your code log in other loggers, this tester won’t notice.

Then, to use it, just call the assertLogged method and it’s family, passing all the strings you want to find.

Example:

def test_blah(self):
    (...)
    self.assertLoggedDebug('secret', 'life', '42')

That line will check that “secret”, “life” and “42” are all logged in the same logging call, in DEBUG level.

So, if you logged this, the test will pass:

logger.debug("The secret of life, the universe and everything is %d", 42)

Note that the message checked is the one with all parameters replaced.

But if you logged any of the following, the test will fail (the first because it misses one of the string, the second because it has the wrong log level):

logger.debug("The secret of life, the universe and everything is lost")
logger.info("The secret of life, the universe and everything is 42")

What can I test?

You’ll have at disposition several assertion methods:

  • self.assertLogged: will check that the strings were logged, no matter at which level

  • self.assertLoggedLEVEL (being LEVEL one of Error, Warning, Info, or Debug): will check that the strings were logged at that specific level.

  • self.assertNotLogged: will check that the strings were NOT logged, no matter at which level

  • self.assertNotLoggedLEVEL (being LEVEL one of Error, Warning, Info, or Debug): will check that the strings were NOT logged at that specific level.

Nice! But…

If you need help, or have any question, or found any issue, please open a ticket here.

Thanks in advance for your time.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

logassert-3.tar.gz (5.8 kB view details)

Uploaded Source

File details

Details for the file logassert-3.tar.gz.

File metadata

  • Download URL: logassert-3.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for logassert-3.tar.gz
Algorithm Hash digest
SHA256 25dbce5052db84f3fe3505d2d0ae73ee061d78103a3fd08a20c5ebb995e8194f
MD5 472bd035cb41bc7e136f3eb0203941ff
BLAKE2b-256 a72205c817fb21f071fe834268a61a238864c1b9a593016e3e37ccb240310e12

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page