Does mocking reduce code coverage?

Some time ago I had a discussion with my colleague. I was trying to explain why test doubles should be used to isolate unit under test.

He used an argument I couldn’t respond to:

Using mocks reduces code coverage of tests.

It’s true, but here’s the thing:

Code coverage does not bring value in itself.

Isolated tests brings value, because they can be used to pinpoint errors (and be used as code documentation and usage examples)

Fast tests are valuable – because slow tests are disincentive to use them constantly.

Code coverage is just a quantity indicator. But just from code coverage one can tell nothing about quality. And additionally code coverage – which you loose when using test doubles – is indirect code tests and is a code smell that you may have hidden dependencies.

Share Button

2 thoughts on “Does mocking reduce code coverage?

  1. “Code coverage does not bring value in itself.” I disagree. Code coverage actually crawls all of the code it covers, in effect running that code. If errors are going to be found it will be at that time. I can write a test that will pass even though the underlying code fails. Code coverage is actually what you should strive for in unit testing. The Asserts and tests themselves are more for the developer to test the results of a particular unit of code. Each test should be debugged and stepped through which often times leads to you seeing errors in your code that threw no exceptions but allowed the method to do things it shouldn’t. All IMO of course :).

  2. Thanks Stephen. My point was this: just having line covered doesn’t meen it’s actually tested.

    But you may be right. Just having particular line touched will give you information when this particular line explodes (exception or fatal). It will be not “tested” if behaves as we want and need.

Leave a Reply

Your email address will not be published. Required fields are marked *