This tutorial is automatically generated from TestWritingTestsTutorial.hpp at revision 3c544f98da9c. Note that the code is given in full at the bottom of the page.
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/simulation work, which involve writing a ’test’ as below but generally without TS_ASSERTs,
just to hijack the testing framework as a convenient way to run things without linking to Chaste as an external library
(which is also possible but a little bit more fuss to set up)
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.
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.
Now we define some tests, which must be public, begin with the word ‘Test’, return void, and take in no parameters.
To test whether two integers are equal, we can use the macro TS_ASSERT_EQUALS.
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
CompareDoubles
for more advanced comparisons.)
This second test shows some of the other TS_ASSERT macros that are available.
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.
To run this code, first copy it into a file, say, called TestWritingTests.hpp in the directory global/test/.
Second, add the full name of your new file to the relevant continuous test pack, say [path/to/Chaste]/global/test/ContinuousTestPack.txt.
Third, from the command line, run
Then press c to configure, e to exit, and g to generate. Finally, run