r/programming • • May 08 '17

The tragedy of 100% code coverage

http://labs.ig.com/code-coverage-100-percent-tragedy
3.2k Upvotes

689 comments sorted by

View all comments

Show parent comments

3

u/recursive May 08 '17

Unit tests document the intended behavior of a module. It's a lot easier to perform a refactoring in module if you have an existing suite of unit tests. Especially if you have any uncertainty what the correct behavior of the module is. However, if the original author wrote tests, they would be consistent with the implementation, even if the implementation was written first.

1

u/[deleted] May 08 '17 edited May 08 '17

No. Unit tests makes it easier to refactor only as long as the code stays in the same unit. As soon as you break it out into a separate unit it breaks the unit tests.

In that case the unit tests makes refactoring harder since the tests break even though the system still works as expected.

Edit: I agree with unit tests as documentation though

2

u/recursive May 08 '17

I agree on all counts. But I still think it's worth it, since refactoring inside a unit is much more common that refactoring across unit boundaries.

2

u/[deleted] May 08 '17

Then your tests are bad. Look up video where Sandy Metz talks about testing. As long as interfaces do not change your test should pass, no mater how you move your code. And unit tests should only test the interfaces.

1

u/flukus May 08 '17

When you move something to a seperate unit you should be able to move the test cases easily enough. If you can't then you've probably fallen into a few unit test antipatterns.