We recommend that you follow the Test-Driven Development (TDD) approach to develop code. Writing tests before code results in a clear understanding of the complexity and dependencies of the code unit being written. In my experience, this leads to better design and better code.
The best projects that I worked on recognize that unit tests are more important than the source code. Applications evolve. Architectures of a few years back are legacy today. By having great unit tests, we can continuously refactor and improve our projects.
A few guidelines are listed as follows:
- Unit tests should be readable. Other developers should be able to understand the test in less than 15 seconds. Aim for tests that serve as documentation for code.
- Unit tests should fail only when there is a defect in the production code. This seems simple. However, if unit tests use external data, they can fail when external data changes. Over a period of time, developers lose confidence in unit tests.
- Unit tests should run fast. Slow tests are run infrequently, losing all benefits associated with unit testing.
- Unit tests should be run as part of Continuous Integration. As soon as there is a commit in the version control, the build (with unit tests) should run and notify developers in case of failures.