Tuesday, October 14, 2014

Unit Test Guidelines

For reusable code, unit tests should have more coverage. The more reuse a piece of code will have, the more the coverage. In a centrally used service or widely shared library, be sure to cover every parameter and every combination of parameters that makes sense. For the following parameter types, here is some basic guidance to start with: string - null, empty string, special chars especially ()'/\."{}<>*@,:;!?&%$# -- // and do not forget "O'Leary", short strings, long strings, string with numbers, leading numbers, leading chars, ending chars, \r\n, ... whole numbers - -max, max, -1, 0, 1, null, (-max - 1), (max + 1) decimals, floats, doubles - 0.1,-0.1, null, 0, 0.0, -max, max, (max + .1), specific precisions, culturally specific formats (Germany uses 1.000,00 not 1,000.00) all numbers - rounding errors, divide by zero datetime - null, 1/1/1, 01/01/0001, 99/99/9999, 12/12/9999, today, yesterday, tomorrow, culturally specific formats (dd/mm/yyyy, etc), non-Gregorian calendars if applicable, "what day is it?", 1/1, so many time formats... be sure to always use datetime never a string that represents the datetime unless its the long representation of the datetime in .Ticks, and use UTC whenever possible. Here's a tip for UNIT TESTING classes with internal dependencies - break the dependency out. If your dependency isn't mockable via a mocking framework because it is not an implementation of an interface, create a testable subclass and override to set up your tests. If you mark getters and other methods as virtual and ensure at least protected access to the method or property.

No comments:

Post a Comment