|
|
|
C++ automatic testing similar to JUnit
|
|
|
|
Stefan Westerfeld <stw@kde.org>
|
|
|
|
|
|
|
|
What it is:
|
|
|
|
===========
|
|
|
|
|
|
|
|
These tests use a little testing framework, which consists currently only
|
|
|
|
of the file test.h. It is very much inspired by JUnit (www.junit.org), but it
|
|
|
|
is maybe even simpler. But it integrates nicely in Makefile.am automatic
|
|
|
|
testing. You can simply type
|
|
|
|
|
|
|
|
make check
|
|
|
|
|
|
|
|
to see if all tests pass. The motivation behind it is, when you are working
|
|
|
|
on a large project, it is often impossible to test quickly whether everything
|
|
|
|
still works all right after changes (which you should before committing ...).
|
|
|
|
This is even more true if you are not working alone on the code.
|
|
|
|
|
|
|
|
With a consistent set of tests that verify whether everything is still all
|
|
|
|
right, things like breaking one thing while fixing another can be made less
|
|
|
|
likely at least.
|
|
|
|
|
|
|
|
How to add a new set of tests:
|
|
|
|
==============================
|
|
|
|
|
|
|
|
1. create a new .cpp file, for this example we'll suppose testarithmetic.cpp
|
|
|
|
2. #include "test.h"
|
|
|
|
3. create a struct which will hold all your tests
|
|
|
|
|
|
|
|
struct TestArithmetic : public TestCase {
|
|
|
|
TESTCASE(TestArithmetic);
|
|
|
|
};
|
|
|
|
|
|
|
|
4. if you want to, override the following methods:
|
|
|
|
|
|
|
|
void setUp()
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* This method is supposed to build a little test world, which will be
|
|
|
|
* built for each test method that is executed - the idea is that your
|
|
|
|
* tests will use this environment to test in.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
void tearDown()
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* This method should completely destroy your test world again, so
|
|
|
|
* that for the next test, a fresh one can be created.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
5. define new tests like this
|
|
|
|
|
|
|
|
TEST(arithmetic) {
|
|
|
|
long a = 2;
|
|
|
|
long b = 2;
|
|
|
|
long c = a+b;
|
|
|
|
|
|
|
|
testAssert(c < 5); // you assert that this condition is true
|
|
|
|
testEquals(4,c); // you assert that the first (expected) value
|
|
|
|
} // equals the second (computed) value
|
|
|
|
|
|
|
|
6. define a main for your test like this
|
|
|
|
|
|
|
|
TESTMAIN(TestArithmetic);
|
|
|
|
|
|
|
|
7. add the following to the Makefile.am (you may want to add more source files,
|
|
|
|
as required)
|
|
|
|
|
|
|
|
testarithmetic_SOURCES = testarithmetic.cpp
|
|
|
|
|
|
|
|
8. add it to check_PROGRAMS
|