Is duplicated code more tolerable in unit tests?

Readability is more important for tests. If a test fails, you want the problem to be obvious. The developer shouldn’t have to wade through a lot of heavily factored test code to determine exactly what failed. You don’t want your test code to become so complex that you need to write unit-test-tests.

However, eliminating duplication is usually a good thing, as long as it doesn’t obscure anything, and eliminating the duplication in your tests may lead to a better API. Just make sure you don’t go past the point of diminishing returns.

Leave a Comment