UserTutorials/WritingTests

This tutorial is not automatically generated from a file.

Writing tests

We do not use int main() methods in Chaste. Instead, we write tests, which are run using CxxTest. Tests are used both as: (i) part of the testing environment - every class in the source code has an equivalent test file which tests each aspect of its functionality, making use of the TS_ASSERTs as described below; and (ii) for experimental work, which involve writing a 'test' as below but generally without TS_ASSERTs. This tutorial shows how to write a test using CxxTest. Note that the full code is given at the bottom of the page.

First, the following header file needs to be included.

#include <cxxtest/TestSuite.h>

Now we have to define a class containing the tests. It is sensible to name the class with the same name as the file name. The class should inherit from CxxTest::TestSuite.

class TestWritingTestsTutorial : public CxxTest::TestSuite
{

Now we define some tests, which must be public, begin with the word 'Test', return void, and take in no parameters.

public:
    void TestOnePlusOneEqualsTwo()
    {

To test whether two integers are equal, we can use the macro TS_ASSERT_EQUALS.

        int some_number = 1 + 1;
        TS_ASSERT_EQUALS(some_number, 2);

To test whether two numbers are equal to within a certain (absolute) tolerance we can use TS_ASSERT_DELTA. This should almost always be used when comparing two doubles. (See also  class:CompareDoubles for more advanced comparisons.)

        double another_number = 1.000001 + 1.0001;
        TS_ASSERT_DELTA(another_number, 2.0, 1e-2);
    }

This second test shows some of the other TS_ASSERT macros that are available.

    void TestSomeOtherStuff()
    {
        TS_ASSERT(1==1); // however, it is better to use TS_ASSERT_EQUALS, below
        TS_ASSERT_EQUALS((true||false), true);
        TS_ASSERT_DIFFERS(1.348329534564385643543957436, 1.348329534564395643543957436);
        TS_ASSERT_LESS_THAN(2.71828183, 3.14159265); // Note: to test if x is greater than y, use TS_ASSERT_LESS_THAN(y,x)
        TS_ASSERT_LESS_THAN_EQUALS(-1e100, 1e100);
        TS_ASSERT_THROWS_ANYTHING(throw 0;); // normally you would put a function call inside the brackets

        double x;
        TS_ASSERT_THROWS_NOTHING(x=1;);  // normally you would put a function call inside the brackets
    }
};

Other useful macros include TS_ASSERT_THROWS_THIS and TS_ASSERT_THROWS_CONTAINS for testing exception messages.

Note that methods that don't start with 'Test' are compiled but not run. So, if you want to stop a single test running, just put an 'x' or a 'donot' (for instance) before its name.

    void donotTestThis()
    {
        TS_ASSERT_EQUALS(1,2);
    }

To run this code, copy it into a file (TestWritingTests.hpp, say, in the directory global/test/), and run, either by running

scons test_suite=global/test/TestWritingTests.hpp

from the command line, or by creating a new "Make Target" in Eclipse using the above command.

Code

The full code is given below:

#include <cxxtest/TestSuite.h>

class TestWritingTestsTutorial : public CxxTest::TestSuite
{
public:
    void TestOnePlusOneEqualsTwo()
    {
        int some_number = 1 + 1;
        TS_ASSERT_EQUALS(some_number, 2);
        double another_number = 1.000001 + 1.0001;
        TS_ASSERT_DELTA(another_number, 2.0, 1e-2);
    }

    void TestSomeOtherStuff()
    {
        TS_ASSERT(1==1);
        TS_ASSERT_EQUALS((true||false), true);
        TS_ASSERT_DIFFERS( 1.348329534564385643543957436, 1.348329534564395643543957436);
        TS_ASSERT_LESS_THAN(2.71828183, 3.14159265);
        TS_ASSERT_LESS_THAN_EQUALS(-1e100, 1e100);
        TS_ASSERT_THROWS_ANYTHING(throw 0;);
    }

    void donotTestThis()
    {
        TS_ASSERT_EQUALS(1,2);
    }
};