UnitTest.h

Go to the documentation of this file.
00001 #ifndef UNIT_TEST_H
00002 #define UNIT_TEST_H
00003 
00004 //!@defgroup unittest Unit Test
00005 //!The CS 240 Utilities include a very simple framework for creating unit test cases.
00006 //!Each class in your program should have a public method with the following (or similar) signature
00007 //!@code
00008 //!static bool Test(ostream & os);
00009 //!@endcode
00010 //!that automatically verifies that the class works correctly. 
00011 //!The Test method on a class usually creates one or more instances of the class,
00012 //!calls methods on those objects, and automatically checks that the results returned
00013 //!by the methods are correct. If all tests succeed, Test should return true.
00014 //!If one or more tests fail, Test should return false. For each test that fails,
00015 //!an informative error message that describes the test that failed should be written 
00016 //!to the passed-in output stream. This will tell you which tests failed so that you can
00017 //!fix the problems.
00018 //!
00019 //!You should also write a test driver program that runs all of your automated test cases.
00020 //!This program will be very simple, consisting only of a main function that calls all of
00021 //!the Test methods on your classes. Whenever you add new classes and methods to your program,
00022 //!you should write new tests cases for the new code. Whenever you modify existing code,
00023 //!you should always run your automated tests to make sure you haven't broken anything.
00024 //!@section The TEST Macro
00025 //!The file UnitTest.h defines a preprocessor macro named TEST that makes it easy to write automated test cases. When TEST is called, it is passed the value of a boolean expression that should be true at the point of the call, like this:
00026 //!@code
00027 //!TEST(temperature > 32 && temperature < 212);
00028 //!@endcode
00029 //!If the expression is true (as it should be), TEST does nothing. If the expression is false, TEST prints a message indicating the location (i.e., file name and line number) of the failed test.
00030 //!
00031 //!In order to do its job, TEST makes some fairly strong assumptions about the structure of your Test methods. A Test method that calls the TEST macro must:
00032 //!
00033 //!@li Have a parameter named os of type ostream &
00034 //!@li Declare boolean local variable named success that is initialized to true
00035 //!@li Call the TEST macro once for each condition that is to be verified
00036 //!@li Return the value of the success variable 
00037 //!
00038 //!The following code example shows how to write Test methods using the TEST macro, as well as how to write a test driver program that calls the Test methods.
00039 //!@include unit_test.cpp
00040 
00041 //!@def TEST
00042 //!@ingroup unittest
00043 #define TEST(cond) \
00044 do {\
00045     if (!(cond)) {\
00046         success = false; os << "Test Failed [" << __FILE__ << ", " << __LINE__ << "]" << endl;\
00047     }\
00048 }while(false)
00049 
00050 
00051 #endif
00052 

Generated on Wed Jul 7 16:30:27 2010 for CS240Utils by  doxygen 1.5.8