Home | Doxygen Documentation | Tutorials | Developer Tools (restricted)

Classes | Namespaces
Unit Tests

Unit tests to automatically test intended behaviour. More...

Classes

class  test::TestCase
 Base class for tests. More...
class  test::TestSuite
 Suite of tests. More...

Namespaces

namespace  test
 

Unit tests.



Detailed Description

Unit tests to automatically test intended behaviour.

Introduction
It is good practice to cover all code by unit tests. This should certainly be aimed at for all new code. Examples for such unit tests can be found in the testsuite directories and in the classes in the test namespace.
Test case and test suite
test::TestCase is the base class for all tests. A new test (for a class or a part of a class or a small set of classes) should be derived from test::TestCase. Several test cases can be grouped together by using test::TestSuite in the following way:
 int main(int argc, char** argv) {
  try {
    test::TestSuite t("Constrained Matrix Eigenvalue Problems");
    test::GolubExample t1;           t.addTest(&t1);
    test::MaxwellTransmissionEVP t2; t.addTest(&t2);
    test::GolubExampleSum t3;        t.addTest(&t3);
    t.run();
    return t.report();
  }
  catch (concepts::ExceptionBase& e) {
    std::cout << e << std::endl;
  }
  return 1;
} 
Template
In doc/test.hh.template, a template for a header file of a new test and in doc/test.cc.template, a template for an implementation file of the test are provided. Header file template:
/** @file filename.cc Description of file
    @author Joe Foo, 2004
*/

#ifndef testname_hh
#define testname_hh

#include "basics/testcase.hh"

namespace test {

  // ************************************************************** Testname **

  /** Description of class
      @author Joe Foo, 2004
  */
  class Testname : public TestCase {
  public:
    virtual ~Testname() {}
    virtual void run();

    /// @name Test routines
    //@{
    /// Description of testValue function
    void testValue();
    //@}
  };

} // namespace test

#endif // testname_hh
Implementation file template:
/* Tests something
 */

#include "test.hh"

namespace test {

  // ************************************************************** Testname **

  void Testname::run() {
    testValue();
  }

  void Testname::testValue() {
    _test(true);
  }

} // namespace test


int main(int, char**) {
  try {
    test::Testname t;
    t.run();
    return t.report();
  }
  catch (concepts::ExceptionBase& e) {
    std::cout << e << std::endl;
  }
  return 1;
}
See also:
Chuck Allison, The Simplest Automated Unit Test Framework That Could Possibly Work, C/C++ Users Journal, September 2000.
Author:
Philipp Frauenfelder, 2004

Home | Doxygen Documentation | Tutorials | Developer Tools (restricted)