Is duplicated code more tolerable in unit tests?

前端 未结 11 2121
死守一世寂寞
死守一世寂寞 2020-11-30 18:28

I ruined several unit tests some time ago when I went through and refactored them to make them more DRY--the intent of each test was no longer clear. It seems there is a tr

11条回答
  •  日久生厌
    2020-11-30 19:02

    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.

提交回复
热议问题