Learn to unit test C/C++ applications using the CPPUnit unit testing framework. Designing software with unit testing in mind may yield better software – the programmer is forced to isolate components. Easy refactoring of code. The unit test is. CppUnit is a unit testing framework module for the C++ programming language. It allows unit-testing of C sources as well as C++ with minimal source.

Author: Kashicage Dugis
Country: Eritrea
Language: English (Spanish)
Genre: Medical
Published (Last): 23 May 2018
Pages: 199
PDF File Size: 1.12 Mb
ePub File Size: 9.40 Mb
ISBN: 748-4-79860-979-6
Downloads: 9653
Price: Free* [*Free Regsitration Required]
Uploader: Gardagrel

Click here for a larger image. Within a Quality Assurance process, we have mainly two kinds of tests:. I’m going to consider that you know what unit testing is, and why it is very important in the software development process.

If you want to read more about the unit testing basis, you can check the JUnit Web site. Think about a typical scenario in a development team: A programmer is testing his or her code by using the debugger. With this tool, you can check each variable value in every program at any time. By running step by step, you can verify whether a variable has the expected value. This is powerful, but pretty slow and it might have plenty of errors.

A few programmers can keep their mind in a deep, hard, and long debugging process and, after one or two hours, the programmer’s brain is near break down.

All these repetitive and hard verifications can be done automatically, with a few programming instructions and proper tools. These tools I’m going to speak about are called “unit testing frameworks. Let’s see this example: We’re programming a small program module, whose main responsibility is just to add two numbers.

CppUnit: C++ unit test framework

As we’re coding in plain C, this module is represented by a C function:. Our testing unit should be coded with another module, that is: Each test compares the addition result with expected value, and it returns FALSE if the result is a value that is different than the expected one. If the execution path reaches the last line, we consider that all tests have been passed correctly, and it returns TRUE.

This small module or function is called Test Caseand it shows a set of checks we do over a single unit. Every verification must be related to a single unit scenario. In this case, we check how “addition operation” behaves about the operand’s sign. We can write other Test Cases, for checking others scenarios.

For example, we can code another Test Case to check our module behavior with typical addition properties:. In this example, we’ve checked some mathematical addition properties. These two Test Cases build a Test Suitethat is: All those Test Cases and Test Suites must be developed while we’re coding the units, and every time the unit changes, the corresponding unit test should reflect changes, modifying a Test Case or adding new one.

For instance, if we improve our “addition” module to add decimal numbers, we have to change our tests, adding, for example, a new addDecimalNumbersTest Test Case. Extreme programming recommends you that you code all these unit tests before you code the target unit. The main reason is very simple: When you’re involved in a development process, you’re in a permanent research stage, in which you’re thinking about how a unit should behave, what public interface you should publish, what parameters you should pass in methods, and other concrete aspects about external access, internal behavior By coding “unit tests” before its development, you’re getting this set of knowledge, and, when you code the main unit, you’ll be able to develop faster and better than the other way.


Each time a team wants to deploy a new release, it should perform a complete unit tests battery. All units must pass their unit or acceptance tests, and in this case, we can release a successful new version. If at least one unit doesn’t pass all its tests, we’ve found a bug. In that case, we must code another test, even add a new Test Case if necessary, checking all conditions to reproduce this bug. When our newly coded test can reproduce the bug properly, we can fix it, and perform the test again.

If unit passes the test, we consider the bug is resolved and we can release our new, bug-free version. They called them JUnit and it became a great hit in the unit testing world. Other developers ported their code to other languages, building a big collection of products, called xUnit frameworks. NET platformand many others. All these frameworks apply similar rules, and probably you can use one if you’ve used another one, with few language-dependent exceptions.

Now, we’re going to explain how you can use CPPUnit to write you own unit tests and improve your units’ quality. CPPUnit uses object-oriented programming, so we’re going to work with concepts such as inheritance, encapsulation, and polymorphism.

Each Test Case should be coded inside a class derived from TestCase.

CppUnit – The Unit Testing Library

This class brings us all basic functionality to run a cppuniit, register it inside a Test Suite, and so on. For instance, we’ve written a small module that stores some data on a disk.

This module coded as a class called DiskData has two main responsibilities: Let’s take ttutorial look:. For now, it isn’t important how cppunnit methods are coded because cpunit most important thing is that we must be sure this class is doing all the things it must do, that is: To do this verification, we’re going to create a new Test Suite with two test cases: You can get latest CPPUnit version herewhere you can find all libraries, documentation, examples, and other interesting stuff.

Once all binaries are built, you can write your own Test Suites. We’re going cppunkt test our DiskData class, which mainly performs two operations: Our test case should test these two operations, with two Test Cases: First of all, we must include TestCase. For the first one, lets us derive our new class from the TestCase base class.

These methods are called tuforial, and are executed when each Test Case starts and ends, respectively. Protected methods implement our test logic, one for each Test Case. In the next few lines, I’ll explain how you can code your test logic. And finally, we define an attribute called fixture. This pointer will hold the target object of our tests. We should create this object inside the setUp method, which is called before each Test Case.

Then, the Test Case code will be executed using our fixture object, and finally we destroy this object inside tearDown, after each Test Case execution. In this way, we get a new, fresh object each time we execute a test cppunot.


Implementation is very simple for now: Next, you can see test case methods, which we’re going to explain. Once we know what aspects we should test, we must be able to program it.

CppUnit: C++ unit test framework

We can perform all operations we need: Sometimes, we’ll need external helps such as an auxiliary file or database table that stores tuorial data.

In our test case, we should compare internal data with the external file data to check that they’re the same. Each time we find an error for instance, if we detect that the internal data isn’t tutotial same as the external correct datawe should raise a concrete exception. There is another way to check a condition and raise an exception if it’s false, all in a single step. The way to do this is through assertions.

Assertions are macros that let us check a condition, and they raise the proper exception if the condition is false, with other cppjnit. Here are some assertion macros:. Following with our example, we should code our loadTest method. We’re going to tutrial the next algorithm: We need an auxiliary file that stores one correct DATA structure. The way you create this auxiliary file isn’t important, but it is very important that this file is correctly created and the DATA structure must be correctly stored.

To check our load method behavior, we’re going to call it with our auxiliary file, and then check whether the loaded data is the same as what we know is stored in our file.

We can write the code like this:. In our second test case, we’ll follow a similar scheme, but things get a little harder. We’re going to fill our fixture data with known data, store it in another temporal disk file, and then open both files new and auxiliary oneread them, and compare the contents. Both files should be identical because the store method must generate the same file structure.

As we can see, we’ve configured a DATA structure with known data and futorial it using our fixture object. Then, we read the resulting file data. We made all kinds of verifications, such as buffers’ and files’ sizes or buffers’ contents. If both buffers are identical, our store method works fine. And finally, we’re going to see how we can show an MFC-based user interface dialog, compiled inside the TestRunner.

We should open our application class implementation file ProjectNameApp. This is simpler isn’t it? Just define a “runner” instance, and add all registered tests. Once tests are registered and added to the runner, we can show the dialogs with the run method.

Now, we’re ready to cppunih our test cases. Just compile your new project and run it from Visual Studio. You’ll see the MFC-based dialog, as above.

Just click Browse and you’ll see this dialog:. But when I tried to port an application on VC7 then all of a sudden the code crashes. From the title, CppUnit, this is a tool for unit testing a code module in isolation from other code modules, which together will comprise an application.