Skip to main content

Simple but powerful assertion and verification of logged lines.

Project description

Log Assertion

Python package

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 do I use it?

The same functionality is exposed in two very different ways, one that fits better the pytest semantics, the other one more suitable for classic unit tests.

For pytest

All you need to do is to declare logs in your test arguments, it works just like any other fixture.

Then you just check (using assert, as usual with pytest) if a specific line is in the logs for a specific level.

Example:

def test_bleh(logs)
    (...)
    assert "The meaning of life is 42" in logs.debug

Actually, the line you write is a regular expression, so you can totally do (in case you're not exactly sure which the meaning of life is):

    assert "The meaning of life is \d+" in logs.debug

The indicated string is searched to be inside the log lines, it doesn't need to be exact whole line. If you want that, just indicate it as with any regular expression:

    assert "^The meaning of life is \d+$" in logs.debug

In a similar way you can also express the desire to check if it's at the beginning or at the end of the log lines.

NOTE: the message checked is the final one, after the logging system replaced all the indicated parameters in the indicated string.

If you want to verify that a text was logged, no matter at which level, just do:

    assert "The meaning of life is 42" in logs.any_level

To verify that some text was NOT logged, just juse the Python's syntax! For example:

    assert "A problem happened" not in logs.error

But I don't like regexes, I want the exact string

Then you just import Exact from logassert and wrap the string with that.

For example, in this case the .. means exactly two dots, no regex semantics at all:

    assert Exact("The meaning of life is ..") in logs.any_level

Anyway, I liked old behaviour of searching multiple strings

Then you may want to import Multiple from logassert and wrap the different strings you had in each call for the classic behaviour.

For example:

    assert Multiple("life", "meaning", "42") in logs.any_level

Examples

After logging...

    person = "madam"
    item = "wallet"
    logger.debug("Excuse me %s, you dropped your %s", person, item)

...the following test will just pass:

    assert "Excuse me .*?, you dropped your wallet" in logs.debug

However, the following will fail (different text!)...

    assert "Excuse me .*?, you lost your wallet" in logs.debug

...producing this message in your tests:

assert for regex 'Excuse me .*?, you lost your wallet' check in DEBUG, failed; logged lines:
      DEBUG     'Excuse me madam, you dropped your wallet'

This one will also fail (different level!)...

    assert "Excuse me .*?, you dropped your wallet" in logs.info

...producing this message in your tests:

assert for regex 'Excuse me .*?, you dropped your wallet' check in INFO, failed; logged lines:
       DEBUG     'Excuse me madam, you dropped your wallet'

A more complex example, with several log lines, and a specific assertion:

    logger.info("Starting system")
    places = ['/tmp/', '~/temp']
    logger.debug("Checking for config XYZ in all these places %s", places)
    logger.warning("bad config XYZ")

    assert "bad config XYZ" in logs.debug

See how the test failure message is super helpful:

assert for regex 'bad config XYZ' check in DEBUG, failed; logged lines:
       INFO      'Starting system'
       DEBUG     "Checking for config XYZ in all these places ['/tmp/', '~/temp']"
       WARNING   'bad config XYZ'

What about repeated verifications?

Sometimes it's needed to verify that something if logged only once (e.g. welcoming messages). In this cases it's super useful to use the reset method.

See the following test sequence:

def test_welcoming message(logs):
    logger.info("foo")  # first log! it should trigger the welcoming message
    assert "Welcome" in logs.info

    logs.reset()
    logger.info("foo")  # second log! it should NOT trigger the welcoming message
    assert "Welcome" not in logs.info

For classic TestCases

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. This is the default behaviour for backwards compatibility.

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-5.tar.gz (11.6 kB view details)

Uploaded Source

Built Distribution

logassert-5-py3-none-any.whl (11.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: logassert-5.tar.gz
  • Upload date:
  • Size: 11.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.2

File hashes

Hashes for logassert-5.tar.gz
Algorithm Hash digest
SHA256 bd7e85af02101d207c1e4718d31fe12237e4fbe410912492ed370bc61b92b2b8
MD5 5293a69e35529f781b4f0d01ff7bbc0e
BLAKE2b-256 4f7a6bd9cef881dac9cc93b25f1886900eaa50a74a33d38b5d45edd1b928957c

See more details on using hashes here.

File details

Details for the file logassert-5-py3-none-any.whl.

File metadata

  • Download URL: logassert-5-py3-none-any.whl
  • Upload date:
  • Size: 11.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.8.2

File hashes

Hashes for logassert-5-py3-none-any.whl
Algorithm Hash digest
SHA256 b7d047882e48260a227c90d6f15366fd93d13fa927c3f0bf0537ca9d1bce1c9a
MD5 b786e91ac30924059e7219d928389fe8
BLAKE2b-256 14b14ab0b774981e984ee5d06410629849dad16c65af42a7fef591f20cc1bfd2

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