Is duplicated code more tolerable in unit tests?

前端 未结 11 2115
死守一世寂寞
死守一世寂寞 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 18:57

    I feel that test code requires a similar level of engineering that would normally be applied to production code. There can certainly be arguments made in favor of readability and I would agree that's important.

    In my experience, however, I find that well-factored tests are easier to read and understand. If there's 5 tests that each look the same except for one variable that's changed and the assertion at the end, it can be very difficult to find what that single differing item is. Similarly, if it is factored so that only the variable that's changing is visible and the assertion, then it's easy to figure out what the test is doing immediately.

    Finding the right level of abstraction when testing can be difficult and I feel it is worth doing.

提交回复
热议问题