By Liam Mayfair

2019-01-11 11:05:49 8 Comments

I was peer reviewing some code today which had loads of assertions to check that specific logs were being produced at various points during the execution of the application and this got me wondering whether it is worth doing this.

To me it seems like a bit of an anti-pattern as it's breaking the encapsulation of the underlying methods and components, since it's expecting specific log strings to be present at various points or when certain functions are called.

By expecting these logs to be created instead of just asserting the outputs of the application, the tests become brittle and prone to break from small refactors like shuffling said logs around or simply by rewording them.

In my opinion logs should not be under test, but perhaps there are valid reasons for doing so which I don't know. What do you folks think?


@Dominique 2019-01-11 12:09:19

I understand this is an opinionated question, hence no answer can completely be correct, but let me give you my insight:

Yes: it's a very good idea to put application logs under test

The part, which I find most interesting, is the one where you launch a whole series of tests, and you create a parsing tool for the application logs. This might reveal formatting issues, which are generally not detected while performing tests one by one.

E.g., your tests log look as follows:

input params : 0, 1, 2 . Result : Ok
input params : 0, 2, 2 . Result : Ok
input params : 0, 3, 2 . RESULT IS OK
input params : 0, 4, 2 . Result : Ok

Here you immediately see that something's wrong with the third test. Such issues generally are not seen while doing single testing, disregarding the logs.

Related Questions

Sponsored Content

31 Answered Questions

[SOLVED] Unit Testing C Code

1 Answered Questions

Jest API Testing

27 Answered Questions

[SOLVED] Should I test private methods or only public ones?

19 Answered Questions

[SOLVED] What is Unit test, Integration Test, Smoke test, Regression Test?

7 Answered Questions

[SOLVED] Writing unit tests in Python: How do I start?

8 Answered Questions

13 Answered Questions

[SOLVED] Unit tests vs Functional tests

0 Answered Questions

7 Answered Questions

[SOLVED] Best way to test a Delphi application

4 Answered Questions

Sponsored Content