Introducing XP-Style Unit Testing
I’ve found that it is difficult to push XP style unit testing when you are first introducing unit testing into a team that is not test infected, whether the developers are experienced or not. The best approach I’ve found is to introduce unit testing incrementally.
First introduce the process necessary to acheive continuous integration with unit testing:
• No non-compiling code should ever be checked into the repository
• All unit tests should pass before and after synchronizing with the repository
• All the tests should be run recursively under the src directory to avoid orphaned tests
Then introduce the fundamental best practices of writing unit tests:
• Unit tests should run in isolation (no external dependencies)
• Unit tests should run individually (they don’t depend on state that is setup in other unit tests)
• Unit tests should run fast
• Non unit-tests should be kept separate. All subsystem or acceptance tests should go in a separate source structure and perhaps have a different execution frequency.
Then introduce the extreme aspects of unit testing:
• Unit tests should be written first
• No code should be committed to the repository without unit tests
Related:
JUnit best practices
JavaUnitBestPractices
junit.org
testdriven.com