How to write unit tests to test individual functionality in C++ classes?

How to write unit tests to test individual functionality in C++ classes??
Explanation with simple example would be more helpful. Thanks in advance
Misunderstood the question, read below.
Last edited on
I think the easiest way would be using some assertions to test what you're going to implement, and then write the implementation to fit the testing code.
take a look at cppunit as well.
you need to write a separate application that will test the classes, this is known as the Test Harness.

So if your application has a class called "BankAccount" you would then write a test harness for it. The test harness would prepare data, call methods and examine results before declaring the test a pass or fail.

eg; to test a method that adds cash to your balance.
1
2
3
4
5
6
float balance = myAccount.GetBalance();
myAccount.Deposit(50.51);
if (myAccount.GetBalance() != (balance+50.51))
   TestFailed();
else
   TestPassed();


You would write each individual test as a separate function and then call them in turn.
1
2
3
4
5
6
void RunTests()
{
   TestDeposits();
   TestWithdrawals();
   TestOverdraft();
}


The idea is that you build up a collection of unit tests, and at any time in the future can compile and run them to make sure you haven't broken your application.
Topic archived. No new replies allowed.