Live data from Hacker News

How to Write Unit Tests for Logging

principal-it.eu

1–10 of 38 posts

Re: How to Write Unit Tests for Logging

#2
I’ve written a sink for the Serilog structured logging library specifically with this in mind: testing logging from code.

When doing TDD there is no reason to skip over logging (although that does seem to happen, everyone likes print debugging ;-) it’s just that sometimes it’s a bit clunky to which is why I wrote this tool. It supplies a secondary package that has FluentAssertions extensions to make testing even easier.

Code/packages can be found here: https://github.com/sandermvanvliet/SerilogSinksInMemory

Edit: formatting

Re: How to Write Unit Tests for Logging

#5
There are two types of logging:

a) Logs which break the program semantics when missing. That means, the program does not work "as expected" anymore for any directly or indirectly impacted enduser.

b) Logs that have no semantic impact when missing. No one will notice that they are missing, unless there is any other problem. These logs are solely meant to better understand the program operations.

Logs of type A should be tested like everything else. But logs of type B - I don't think I would test them.

While it is not nice to have a failure in production and figuring out only at that time that the logging doesn't work, I think that is even worse to break tests because of logging changes. Logging usually is so easy that it is an all- or nothing thing. I'm not saying that it does not have advantages to test for these kind of logs, but from my experience it is not worth the costs in 99% of the cases.

Re: How to Write Unit Tests for Logging

#7
post #3

Let's write unit tests for unit tests!

To be fair, I worked at a company that had CI for running unit tests so nothing could be merged into remote master until the build was green.

The build asserted that the number of failing unit tests was 0.

It turns out that "breaking the unit test library completely such that 0 tests run instead of all thousands of them" did not break the build.

When someone noticed a week later, that was not fun to clean up.

Re: How to Write Unit Tests for Logging

#8
It seems that the boundaries of what a unit test should cover have never been clearly defined. In my opinion, unit testing should only cover logic that does not have side effects (like writing to disk or communicating over a network). If you want to test external side effects, you should write functional or integration tests for them.

Re: How to Write Unit Tests for Logging

#9
post #3

Let's write unit tests for unit tests!

so, on that topic:

- most test frameworks use unittests to test themselves.

- tests need to have value. the dev/team/organization needs to come up with the sensible measure of value that guides poeople what to test and how to test it.

Re: How to Write Unit Tests for Logging

#10
In Go, I test my log output easily: my logger instance’s output is set to a buffer, call the function under test, assert log contents. If another system depends on it, it gets tested. Our acceptance level tests operate largely on logs.
Post reply on HN