Everyone talks about refactoring code. What I don't see enough discussion on is refactoring <i>tests</i>.<p>It's a constant battle at work, and I tend to be firm with: "If the test is working, no matter how horrible the (test) code, leave it be."[1]<p>For regular code, we rely on tests to validate our refactors. With test code, we don't have that support.<p>If you have a bunch of tests that could do with a refactor, and your motivation is that you need to write more tests, then set up whatever abstractions you need and write the new tests, but <i>don't</i> touch the existing tests! If you really want, you can overtest: Write new tests with your refactored code that test the same thing as the old ones. It's OK to test the same thing multiple times (when testing is cheap).<p>About a third of the time a coworker looks at my tests and refactors them, he makes an error - the most common one being not testing what the original test meant to test. See, the reason he wanted to refactor was that the original test was hard to follow. And <i>because</i> of that, he failed to interpret it correctly and failed to properly reproduce the test in his refactored code.<p>I then have to code review all his refactors. I have to spend time to figure out what my difficult-to-understand test did, and confirm he didn't introduce bugs. It's very, very tiring. And for what?<p>And he's not a newbie. He's as good at SW as I am. Plenty of experience. If <i>he</i> gets it wrong, I expect most people will get it wrong. With a higher error rate.<p>This is the one case where I say: "Feel free to overcomment." If you took a long time understanding a given test, write out what you learned so that the next time you read it, you know what it does. I'll be happy to code review <i>that</i>.<p>[1] Unless you are the original author of the tests, and they are still fresh in your mind. In that case, refactor all you want.