I accidentally reached 100% coverage. Before I had 90%+ and felt pretty confident. The coverage reports gave me useful insights about my own codebase and I understood deeply what's going on and where.
The last statement was a bit more difficult to test and I was pretty confident that it works. So I wrote the unit test only to learn how to test this kind of stuff… and I found out that there's a bug.
What does it mean to have 100% test coverage
I always thought that it's about testing all lines of code. However, decent coverage reporter complains about missed else paths, so it actually also includes code which isn't there.
More accurate definition might be that all branches of code are executed during test run. Eg: for every if condition, there has to be at lest two unit tests (one for if clause, one for else clause).
The good thing is that you don't need a unit tests for 100% coverage. When you have a complex integration test which covers all cases, you might get most of your code tested.
Is it worth it?
The most common counter argument is that the metric itself is useless, because some tests are more important that others. I agree with that, some parts of code are crucial and should be tested more than others (these parts of codebase I usually have at least 100% covered).
The point is that the number itself isn't important. Test coverage is great because it helps me to understand how the code is executed. It allows me discover edge cases I'm not aware of.
The goal isn't to have 100% coverage, but rather understand 100% of the code and be aware what's going on there. Full code coverage is just a side-effect of such approach.
Link to original post.
Top comments (6)
Mutation testing is a cool way to find the edge cases that can make trouble even if getting 100% branch coverage, since 100% branch coverage doesn't mean you covered every possibility.
Totally! Mutation tests are much more important than having 100% coverage. It lets you know if the tests are correct or not.
Never heard of it. Definitely looks promising. I'll give it a try, thanks!
Coverage also helps to keep track of what areas are covered at all, but as you've said, it's not pure science, and 100% might hide some flaws.
Also, it doesn't tell us much about integration between the pieces.
I'd be interested in what sort of technologies you're working with (whatchya' stack?).
Mostly just a boring JS stack: Jest test runner with React. When working on backend API, then Python/Django with py.test test runner.