Unit Testing is a very important part of development of Software’s. In unit testing a proper approach is followed to achieve different goals, such that small chunks (units) of code are tested individually to satisfy the result. There are many ways to test those small chucks of code but in this article “Unit Testing with cppunit”, I will show you how to use CppUnit for unit testing. CppUnit is specifically for C++ and can’t be used with any other language. And here we will use CppUnit in visual studio using windows operating system. I am using visual studio 2010 in my system but you can use previous versions as well. There is a built in testing tool in visual studio but we are using external framework here because testing code of CppUnit will also work in Linux, whereas we can’t run visual studio built in testing logic in Linux. So I would suggest people to use CppUnit for testing C++ code.
In this article I will just show you how you can download and configure your CppUnit Library in Visual studio. To download the latest version of CppUnit CLICK HERE. Unzip the file and save it any where in your system. Open the cppunit project first by going to the cppunit-1.13.2 (your version can be different), go to src > CppUnitLibraries2010.sln file. You will see the solution explorer like shown in the image below.
This image shows that we have five projects in a solution. So, go to the properties of cppunit project by right clicking on it. You will see some thing like shown in the image below. Change the cppunitd.lib to cppunit.lib, which will be in Librarian as shown in the image. Click on apply, then OK and close the solution.
Unit Testing with CppUnit – Visual Studio Configuration
Now, Go to the New Project and make a simple C++ console application and name it as MyCpp. Remember to check the empty project from click next while creating new project. Now right click and go to the properties of newly created project, you will see a new window will open something like shown in the image below.
Now, what you need to do is to give paths of include and lib folder which are inside cppunit. Click on the C/C++ > General, you will see “Additional include directories”, you need to change this. You can see my path here, but when you will open it first you will see blank area after the Additional include directories. Click on that area, it will allow you to edit the path, click on the edit, then folder icon there and add the path of include folder inside your cppunit-1.13.2 (where as your version can be different). Now you need to the provide the path of lib folder. Click on Linker > General, where you see “Additional Library Directories” follow previous steps and give the path of lib folder which is inside your cppunit-1.13.2 same as your gave path of include folder.
Now click on Input which is inside Linker. Here you just need to add cppunit.lib at the end of “Additional Dependencies” as shown in the image below. After doing this, you need to click on Apply and then OK.
Note: If you are using different visual studio version, “Additional Include Directories”, “Additional Library Directories” and “Additional Dependencies” can be in some where else, you just need to find these and give the path of the specific folder as explained.
So, this was all you need to do in order to do Unit Testing with CppUnit in visual studio. If you want to go further to see practical example of cppunit, have a look at my article CppUnit Example. Thank you!






