Wednesday, October 26, 2005

Best Practices for Writing Unit Tests

Here are some principals that will help with successful testing:

  • Avoid creating dependencies between tests such that tests need to run in a particular order. Each test should be autonomous.
  • Use test initialization code to verify that test cleanup executed successfully and re-run the cleanup before executing a test if it did not run.
  • Write tests before writing the any production code implementation.
  • Create one test class corresponding to each class within the production code.
  • Use Visual Studio to generate the initial test project. This will significantly reduce the number of steps needed when manually setting up a test project and associating it to the production project.
  • Avoid creating other machine dependent tests such as tests dependent on a particular directory path.
  • Create mock objects to test interfaces. Mock objects are implemented within a test project to verify that the API matches the required functionality.
  • Verify that all tests run successfully before moving on to creating a new test. That way you ensure that you fix code immediately upon breaking it.
  • Maximize the number of tests that can be run unattended. Make absolutely certain that there is no reasonable unattended testing solution before relying solely on manual testing.

Adpoted from A Unit Testing Walkthrough with Visual Studio Team Test by Mark Michaelis.


Post a Comment

<< Home