r/shittyprogramming • u/legendaryroots • Feb 25 '15
How to get more code coverage
So this just happened at a Fortune 100 company that I contracted for...
When pushing to production a new requirement came down that unit tests needed to have 80% code coverage. This was a big issue, because unit tests weren't even part of the requirement let alone this much code coverage for such as large application.
So I throw up the red flag and say we have to delay the launch because apparently the 80% coverage is a "unchangeable automated check". To which the senior developer responds with the following:
"Oh don't worry about it - just add a 5000 line private function that just writes to the debug log, and call it with the unit test."
Now that is thinking outside the box. A shitty box.
52
28
u/alienangel2 Feb 26 '15
When they see how quickly you took a legacy application to 80% coverage, they will be overjoyed, and then demand the threshold be raised to 100% instead, at which point this trick will sadly no longer work.
45
u/AqKBMbvf5eMnq7Qz6 Feb 26 '15
Easy, you can use reflection to overwrite the internal LOC count in your testing coverage framework fairly trivially. This is even easier if your language supports proper metaprogramming.
18
13
u/zman0900 Feb 26 '15
metaprogramming
Continually generate more fake functions with tests that fully cover them until the coverage reaches at least 99.99%?
15
7
6
u/supergnawer Feb 26 '15
Managers are all about "80/20" rule, that's where 80% likely came from. So they are likely to understand the difference between 80 and 100.
1
9
14
u/supergnawer Feb 26 '15
That was actually a great call. Large part of contracting for big companies is being aware of their internal politics and specifically knowing what/whom you can safely ignore. Apparently the guy knew that this requirement is stupid and nobody cares about it.
What's interesting here is the 5000-line function approach. I take it, it works like this: there's some big screen where you have the entirety of you code listed in very small print, and test coverage immediately highlights in red over it. In this case, 80% is not the limit, you can even achieve 99%.
0
u/ekolis Mar 04 '15
Unit tests can call private functions in whatever language you're using? Lucky...
-8
74
u/UnspeakableEvil Feb 25 '15
That's magnificent. One change though, I suggest you start each line with an if(true) check so that you're future-proofed for when branch coverage needs to be taken into consideration.