Many developers have found that the best time to test their code is when they are writing it. It's a horrible feeling when you spend all day writing a module, only to have it fail and not know why. By creating unit tests in the middle of development, the developer receives quick feedback regarding whether part of their code succeeds or fails. Now, let's look at some ways developers write tests as they code.
Test-Driven Development (TDD) is the process of writing unit tests and end-to-end tests, then writing code to pass those tests. Whenever code is refactored, it runs through the same tests to ensure results come out as expected.
Developers using TDD tend to code using the following steps:
Write a test based on requirements.
Run all tests and see that the new test fails. This eliminates unnecessary tests and tests the overall testing framework.
Write some code to pass the test.
Run the tests again.
Refactor as necessary to clean up ugly code, names,...