Unit Test

From CSSEMediaWiki
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.

Contents

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.

Unit Testing Frameworks

Some of the most popular unit testing frameworks include:

A comprehensive list of frameworks can be found here: http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks.

Personal tools