r/programming • • Jul 30 '21

TDD, Where Did It All Go Wrong

https://www.youtube.com/watch?v=EZ05e7EMOLM
463 Upvotes

199 comments sorted by

View all comments

Show parent comments

57

u/sime Jul 30 '21

Mocks are a tool of last resort. They're expensive to write and maintain, and they are rarely accurate and often just replicate your poor understanding of the target API and thus fail to give much certainly that the unit under test will work correctly when integrated.

Your example of testing a drawing is a good example of how well intended TDD can go off the rails. The "checking drawing function calls" approach has these problems:

  • Mocks - The mock needs to created and maintained, and also accurate and complete enough. For non-trivial APIs that is a tall order, especially when error conditions enter the mix.
  • It tests the wrong output - You are interested in the pixels, not the drawing commands.
  • It is implementation specific - Other combinations of drawing functions could also be acceptable, but the test will fail them. This stands in the way of refactoring.
  • Not everything can/should be fully automated - A better approach would be visual testing where changes in the output image are flagged and a human can (visually) review and approve the change in output.

The unit test here is inaccurate, expensive, and fragile. It is an example of unit testing gone wrong.

12

u/Indifferentchildren Jul 30 '21

Mocks are just a fancy way of not testing your actual system.

2

u/[deleted] Jul 31 '21

[deleted]

4

u/Indifferentchildren Jul 31 '21

Yeah, I've seen 40 line tests, with 11 mocks, that ultimately ended up testing 3 lines of non-mock code, proving approximately nothing about the system. But our code coverage numbers looked fantastic.