Asserting log traces in functional or unittests Python

Sometimes you need to assert log traces while writing tests, mostly functional tests (end to end) after you have setup several components by means of mocks. When executing nosetests it sets up automatically the logging and uses any handler you provide on your own.
Let’s say you have got a module in your tests folder including the following code:

then, in a test case you could end up writing something like:

Overall, it shows how easy it is to assert log traces in-memory while executing unittests. In a similar way you could patch the sys STDOUT with a mock object providing a write method and assert what is being written by means of print statements.

Advertisements

About jordilin

I'm a Senior Software Engineer with an MSc in Telecommunications Engineering. I've got a range of interests including Linux, a very powerful and limitless operating system, travelling, gastronomy and technology.
This entry was posted in Linux, programming, Python and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s