Live data from Hacker News

Test coverage only matters if it's at 100%

dein.fr

31–40 of 53 posts

Re: Test coverage only matters if it's at 100%

#31
Edgy, but wrong.

Is there a difference between 79.9% and 80%? Maybe not. My team is increasing test coverage for a legacy code base. 60% to 80% DOES matter. And it’s not just the fact that the tests flags regression, but the very process of increasing test coverage uncovers bugs / dead code.

Re: Test coverage only matters if it's at 100%

#32

The irony to me is that the examples he provides for things not to cover are exactly things I think should be covered. Short-circuit logic is ripe for bugs and the code that causes the short-circuit should definitely be tested!

Yes, and exceptions. Tests must tickle all the exception paths. An exception that contains a dumb typo crashes the system instead of propagating up to get handled. Just touching the exception path roots those out.

  raise ValueError(' '.join('meaningful message with:', parameter)
Is a stupid mistake that I still make all too often inside an exception handler.

Re: Test coverage only matters if it's at 100%

#33
post #26

Test coverage is a very dangerous metric. IMO it will only be useful the day that we find a way to not take into account those tests that are testing implementation details. Every time that I see a high number being enforced, the end result is always a disaster: lots of poor quality tests that are only there in order to bump up that metric. Those tests are poison: they don't aim to test correctness, they make refacto…

Bingo, this *1000.

Your team ends up spending half their time writing painful depressing tests that are bad afterthoughts. Generally with TONS of mocks and little real meaning.

Re: Test coverage only matters if it's at 100%

#35
Not my experience at all... Tests need to be intelligently written for the project at hand, which sometimes means testing something to freeze the API, writing tests for small functions that are easier to test in a suite than anywhere else, and especially catching a problem where your program keeps failing, like testing with an older version of a library or runtime, or even testing something that you know could be fragile.

Re: Test coverage only matters if it's at 100%

#37
- Don't write unit tests for testing code, use them to guide your design.

While that statement is itself is somewhat questionable, it's a little better and IMO reflects the purpose of unit tests better. Usually, if I'm finding it hard to write or setup a test, that's a signal that something's off. Also, UTs are absolutely lovely when you're refactoring code - as long as code tests invariants and publicly visible behavior instead of implementation. So even if the part you're refactoring has say 60%, you'd still be in a much more confident of making changes.

Code coverage under anything other than unit tests can be very misleading - for ex I've seen places where system & integration tests are run on instrumented code (and little to no unit tests) and since a huge swathe of code will be hit (mostly happy path), there's a false sense of code quality and safety which is probably more dangerous than no tests.

Overall, "When a measure becomes a target, it ceases to be a good measure." applies most often to test coverage.

Re: Test coverage only matters if it's at 100%

#38
post #18

As someone who worked somewhere where 100% code coverage was practically required, while it does point out flaws in your testing where you may have "missed a spot." It still doesn't prove that you're testing everything. You never know if a library function you are calling beneath your code has a different path it can also follow. > I believe the only level of test coverage that's worth it is 100% of the lines you wan…

100% line coverage in particular means nothing, and it’s not 100% branch coverage.

    if myTest:
      myVar := “foo”
    print(myVar)
You can achieve 100% line coverage by testing only the True case, yet have your code fail in production.

Re: Test coverage only matters if it's at 100%

#39
post #18

As someone who worked somewhere where 100% code coverage was practically required, while it does point out flaws in your testing where you may have "missed a spot." It still doesn't prove that you're testing everything. You never know if a library function you are calling beneath your code has a different path it can also follow. > I believe the only level of test coverage that's worth it is 100% of the lines you wan…

I would go farther and claim that 100% test coverage is worse than 90% test coverage. You're probably testing irrelevant stuff - which just means that you have more test code to maintain, and also likely means that your tests are bad (i.e. not testing business logic, but testing irrelevant implementation details). Last, but not least, 100% test coverage means that your code is bad/ you're not programming defensively (how exactly did you cover all those error-handling bits for cases of unexpected exceptions/ "stuff that theoretically should never happen, but in practice it might, so let's have some sensible treatment/ logging/ etc."? )

Re: Test coverage only matters if it's at 100%

#40

This is totally ridiculous. Test coverage clearly matters even if it's not 100%, as anyone who has ever worked on an older codebase knows. As long as you're not causing coverage regressions, it is perfectly acceptable to leave untested code that "should" be tested, but hasn't been changed in years. Test it when you change it; don't test for the sake of achieving a coverage benchmark.

And implying that there is 100% coverage just because the instruction register had all valid values is silly. When writing test one should worry about the other registers too ...

Thank you for stating this so succinctly!

This concern has been nagging me in the back of my mind, but I hadn't convinced myself that code-coverage was insufficient (in practice) until you framed it this way.

Now all kinds of supporting examples come to mind:

- divide-by-0 errors

- floating-point exceptions

- numeric underflow / overflow (both silent and signaled)

The alternative code paths exercised by these situations are generally outside the purview of code-coverage tools, because they reside in the silicon / firmware, process' default signal handlers, etc.

Post reply on HN