Nope.
We write the test to fail first... because we need to test... the test itself! How can we depend on a test that isn't ever going to fail?
Test is failing because there's no code yet.
Write just enough code to make the test pass (hard code the result).
Now that the test is passing, we can refactor.
But... I actually like to take this opportunity to write the next test.
New test is failing because the hard-coded value for the first test doesn't work (by design).
NOW... we can refactor. Adding just enough code to make both tests work. Here is where we put in the actual work that makes the code function.
Every time we have a new piece of functionality, we write a test.
Our tests will be an "executable" document on how we (programmers) intend the code to work.
Even if we didn't get the correct logic to satisfy the requirements (which we found out in our Acceptance testing)... At least we know what we intended our code to do.
Now that the test is passing, you're half way there. Move on to Refactoring, and keep the test green each time a refactoring is completed.BUGS?Got a bug? Great! You're just missing a test then... DON'T JUST FIX IT! That's why bugs come back..TDD: Find a bug ->Write a test! The test expects the correct behavior... and fails of course! NOW fix the bug ... and the test passes. That bug will NEVER be back again because your test will always ensure that the code will do the right thing. This is the safety net that TDD, Continuous Integration, and test automation give us. We can now refactor mercilessly... Onward to Refactoring!
DON'T JUST FIX IT! That's why bugs come back..TDD: Find a bug ->Write a test!
The test expects the correct behavior... and fails of course! NOW fix the bug ... and the test passes.
That bug will NEVER be back again because your test will always ensure that the code will do the right thing.
This is the safety net that TDD, Continuous Integration, and test automation give us. We can now refactor mercilessly...
Onward to Refactoring!
Change the order of your tests... do they still all run green? Good tests are made to be independent. They set-up the system, test it, determine success, and then CLEAN UP. Especially if there is an error, CLEAN UP is essential.
Be a good test citizen. Just like when hiking - pack it in, pack it out - After the tests run, always make sure leave things as pristine as you found them.
Remember when you're so gung-ho about testing that you generate all those unit tests... those tests will have to be maintained along with the mainline code... Test what makes sense, but don't over-do it. Test maintenance costs time and money too.