Always. The development cycle should be Test, Code, Refactor, as one atomic block of work. Sarah Mei explains why this is vitally important to creating good code in her talk "Work, Play & Code", I highly recommend checking it out.<p><a href="http://confreaks.com/videos/2333-mwrc2013-work-play-code" rel="nofollow">http://confreaks.com/videos/2333-mwrc2013-work-play-code</a><p>Also, if you don't have a test suite to prove your refactoring didn't change functionality, IMO you aren't refactoring, you are simply rewriting. This is the point many people miss about TDD. TDD is first and foremost about letting you refactor your code; doing TDD right gives you the best environment for writing great code. On the other hand, doing TDD wrong makes it more likely your entire code base will be very difficult to maintain.