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.
How to Write Unit Tests for Logging
11–20 of 38 posts
Re: How to Write Unit Tests for Logging
#12It's what I describe in "Returning a message list" in this post: https://henrikwarne.com/2020/07/23/good-logging/
Re: How to Write Unit Tests for Logging
#13In 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.
(I'm probably using incorrect terminology here, so happy if someone corrects me)
Re: How to Write Unit Tests for Logging
#14There 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…
Re: How to Write Unit Tests for Logging
#15It 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.
If your code is mostly passing data around and managing side effects a unit test is more like a parasite. It will couple to your code and fail noisily when refactoring but not provide any actual useful feedback.
Re: How to Write Unit Tests for Logging
#16I would say it's not great practice, but, it can remove a lot of contortions to be able to make assertions about the execution path taken when doing a unit test.
If I have a tricky thing to test. I ask, if I added the required operational monitoring (e.g. queue sizes)? Would I suddenly be able to test this easier? Would it make the test more self explanatory. If the answer is yes I might add the monitoring and then do the unit testing. If the answer is no I have to refactor the implementation for testability.
conclusion: exposing logs and especially monitoring counters to unit tests can actually super charge your testing and leave you net positive
Re: How to Write Unit Tests for Logging
#17Let's write unit tests for unit tests!
Tests for tests exist. One such technique is called "mutation testing" [1].
I don't know of any job that actually uses them, but they make sense to me: the tests need checks too, otherwise how do you know they are adding value? It's too easy to write worthless tests, see them pass, and get a false sense of security. Techniques such as mutation testing check how sensitive your tests are to actual bugs (or program changes): do they detect them? If not, they aren't useful.
Re: How to Write Unit Tests for Logging
#18Let'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
#19It 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.
You need to test that you're using the contracts you've made, and integration tests verify that everything works as expected when both sides are complying with the contract.
I'm saying that if I've abstracted out the filesystem into an interface the distinction of asserting against whether a method was called on that interface and asserting a side-effect is somewhat trivial. I've made it so that side-effects must be handled in integration tests.
Re: How to Write Unit Tests for Logging
#20There 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…
> Logging usually is so easy that it is an all- or nothing thing.
By all means, have the integration test that verifies that logs are produced at all (on each mechanism, if you support more than one). And it's a bonus if that test makes the deployment (or installation if that's your thing) fail.