I’ve been trying to find a decent unit testing framework for C++. My main requirements:
After I’d hacked something up myself, I was pointed to tut, which looks quite decent. It does everything with templates, which is cleaner than my cpp stuff. I’m no template guru, though, so I couldn’t figure out how to give names to tests. With tut, tests are numbered, which looks a bit annoying. Test 6 failed? What was test 6?
I carried on with my own hacked up framework, which seems to be approximately complete now. I’ll ask the tut author if he knows whether it’ll be possible to add names to tests, and to compile all the tests into a shared library for running with a separate front-end.
You can see the current state of my framework here: UnitTestFramework.tar.gz.
My plan is to write a plugin for kdevelop which gives a nice GUI for running tests. I’ll probably use my framework for the moment, but if I find a better one, I’ll switch to it.
With my framework, tests look like this:
#include "UnitTest.h"
#include "BankAccount.h"
UNIT_TESTS
TEST_DATA(BankAccount)
BankAccount account;
TEST_END_DATA(BankAccount)
TEST_SET_UP(BankAccount)
{
account.setBalance(3);
}
TEST_TEAR_DOWN(BankAccount)
{
account.setBalance(0);
}
TEST(BankAccount, InitialBalance)
{
ASSERT_EQUAL(account.balance(), 3);
}
TEST(BankAccount, Credit)
{
account.credit(100);
ASSERT_EQUAL(account.balance(), 103);
}
TEST(BankAccount, Debit)
{
account.debit(100);
ASSERT_EQUAL(account.balance(), -97);
}
TEST(BankAccount, Bogus)
{
account.debit(100);
ASSERT_EQUAL(account.balance(), -10000);
}
There are console-based and Qt-based test runners. The Qt version isn’t quite complete yet. It needs to show what went wrong in which test, and allow navigating to the source position, but it shows the general idea of what I’m trying to do.