Unit Testing Is Cool
February 25th, 2010.Filed under Programming.
The Agile and TDD ( Test Driven Development ) have come upon us, and have encouraged many programmers to write automated unit tests. More are joining their ranks every day. I`m one of them, and these are my notes from my own experience and other sources like the Internet and books.
Basically, everyone knows that TDD asks us to write unit tests first, before we write production code. But we should follow these three laws:
First Law: You cannot write production code until you have written a failing unit test.
Second Law: You cannot write more of a unit test than is needed or required but without being abundant to fail.
Third Law: You cannot write more production code than is needed to pass the currently failing test.
You have to keep your tests clean. It is unit testing that keeps your code flexible, maintainable, and reusable.
F.I.R.S.T
Fast: Test should be fast. They should run quickly.
Independent: Tests should not depend on each other.
Repeatable: Tests should be repeatable in any environment. You should be able to run the tests in the production environment, in our laptop when you do not have an Internet connection.
Self-Validating: The tests should have a boolean output. Either they pass or fail.
Timely: The tests need to be written in a timely fashion. Unit tests should be written just before the production code that makes them pass.
