Sunday, December 20, 2009

Notes on Pragmatic Unit Testing

Andy Hunt's book on Pragmatic Unit Testing is a good resource and should be on the bookshelf of every .Net developer.  I took a few notes while going through the book...

What to Test? Use Right BICEP method.

Right: Are the results right?
B: Are all the Boundary conditions correct?
I: Can we check Inverse relationships?
C: Can we Cross-check results using other means?
E: Can we force Error conditions to happen?
P: Are Performance characteristics within bounds?

Are the results right?
• We must validate method results and verify that the code ran correctly?
• Consider using data files for testing larger amounts of data.
• Use test classes/methods to prove the existing code is right.

Boundary Conditions
• Test the boundaries of code inputs using extreme data.
• Try to break the code and/or ensure data validation using garbage data that seems insane.
• Test for duplicates, data sequence, and security holes
• CORRECT (Conformance, Ordering, Range, Reference, Existence, Cardinality, Time)…more later

Check Inverse Relationships
• Work your methods backwards.
• Execute cycle of: 1) start with original data, 2) Inject Test Data, 3) Inverse new test data back to original data using test(s).

Cross-Check using Other Means
• Create a system of checks and balances by having test methods cross-check one another.
• Explicitly using different code to achieve the same results.

Force Error Conditions
• Create real-world scenarios by forcing errors to occur….creating negative tests.
• Some sample environmental scenarios include network availability, insufficient permissions, not enough disk space, overloaded CPU, etc.