Unit Test

From CSSEMediaWiki
Revision as of 22:45, 23 September 2009 by Benjamin Kearns (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Unit tests are classes which test the functionality of other classes. Unit tests are a critical part of some Agile methods and practices such as EXtreme programming and Test Driven Development, where you are required to develop the tests before you start working on the code itself.

Limitations

While unit tests can give some assurances that your code is working as intended at the lowest level, they are unable to find all software bugs even with 100% code coverage, and they obviously can't find bugs with the requirements, etc.

Example

The following is a simple string class for C++ (which breaks a number of "best practice" guidelines):

String.png

This Unit test has 100% code coverage and gives the appearance that the code is bug free, however the string only has a character array of 10 characters allocated internally for the underlying character array, meaning that any string over 10 characters would, at best, crash the program, and at worst cause a security hole which would allow an attacker control over the computer as the user who is running the code.

Guidelines

Don't aim for 100% code completion - It doesn't assure you that the code is bug free, and is difficult to get for even slightly complicated code. Instead you should test "common" usage scenarios for that class.

Don't rely on the internal implementation of a class - Unit tests are designed only to test the external interfaces to classes, so they should not have any knowledge of the implementation at all.

Personal tools