Concerns about Red-Green-Refactor

Green, Red, Green, GreenI have never worked in an environment that practices Test Driven Development (TDD), but the little that I have read suggests that there are three phases: Red – The Test is written, and checked that it will fail properly; Green – The test code is rewritten so that is passes ‘by any means possible’; and finally Refactor – the code under test is refactored to produce the desired end-results for real.

I understand the point of testing that your test can fail, but I don’t understand why its a good idea to make it look like your test is passing with anything other than real code? At ‘Red’ I can hope no-one will mistake my failing test for something that is working or finished – at ‘Green’ however, I have a test that appears to be functioning and could easily be mistaken for a test that was complete for code that was complete! The process has three steps; Write Test, Write basic Production Code, and Refactor…. but the measurement system has just two outputs; Red and Green. The output does not tell an interested third party where the process got to – or to put it another way, there is no correlation between the process and the output. Now, if a test could somehow be informed where you were at in the development process you could have Red – Chartreuse – Green – with the middle-colour (indicating yellowish-green) also marking the middle step of the process and a kind-of test pass (but not the real test pass). But to do this, you would have to maintain meta-data in your code about the process you were following when coding, which would itself become another point of failure, perhaps?

For example, in Automated Builds I mentioned that I had noticed confusion between causes of test failures (was it a bad test, or a bad code-change), and one might additionally imagine complaints that a test was at Chartreuse (because it had been incorrectly marked as being at the central phase when in fact it really ought to have gone back to the ‘Red’ step because changes to functionality should have resulted in tests being rewritten. I can’t help but feel I am being a little ridiculous here, but…?

In fact, thinking about it, I have seen tests that indicate that the TDD process is in fact Green, Red, Green, Green – because if you write a test with no Assert the test will pass (in my experience with NUnit at least). Therefore, you can have a test that appears to work (it’s green) and yet nothing has been tested. maybe it is melodramatic to say it, but that’s scary and very wrong.

Let me be clear, I am not against starting new coding with writing unit tests, and I certainly believe that designing code from the ground-up to be testable is a good thing, but I just can’t understand this desire to ‘hack’ code to achieve a unit-test success in the steps to tested and working code.

Leave a Reply

Your email address will not be published. Required fields are marked *