Zulkirn Once we know what aspects we should test, we must be able to program it. This class brings us all basic functionality to run a test, register it inside a Test Suite, and so on. Click here for a larger image. We need an auxiliary file that stores one correct DATA structure. Once tests are registered and added to the runner, we can show the dialogs with the run method.
|Published (Last):||6 January 2006|
|PDF File Size:||6.9 Mb|
|ePub File Size:||19.67 Mb|
|Price:||Free* [*Free Regsitration Required]|
It is ported by Michael Feathers. Recall that one of the required program quality attributes for your project is reliability. CppUnit can be used to help you achieve that. This document can be considered a "port" of the JUnit Guide I wrote. As in the JUnit Guide, I have included only the bare minimum to get you started. I will first go through the installation of CppUnit in the next section, followed by a description of how to use CppUnit using a sample program.
Next, I will suggest some ways of organizing your project and test codes before ending the document with pointers to some useful references. Installing CppUnit Installation of CppUnit can be broken down into the following steps: Download CppUnit and unzip the archive into a location of your choice. Change the "Files of type" to "Workspaces". The TestRunner gui will appear. Just click on the Run button to run the tests.
Close the TestRunner window. Another window will pop up. Just close it. CppUnit is now installed properly. As it is quite complicated to incorporate the graphical interface into the test programs you write, I will only illustrate how to use the text-based interface of CppUnit.
Email me if you are interested in running the graphical version. Now, you need to re-compile the CppUnit library for using the text-based interface. A new dialog will appear. Next, add the files for running the text interface. You need to change one more setting. Under the Category dropbox, select Preprocessor. The next section illustrates the key steps using an example. My advice is to skim through this section quickly for an overview.
Then, as you go through the example in the next section, refer back to this section frequently to get the whole picture. Assuming that you want to test a class called Parser. This class must inherit the class TestCase which is defined by the CppUnit framework. Create a constructor for this class, passing a name that is representative of the set of tests for this class as the parameter. Create a fixture. A test fixture is a set of sample objects that you want to re use during testing.
For example, you might create a few sample source files for the Parser to parse. CppUnit provides a setUp and a tearDown method to manage the fixture. Therefore, you can eg. For example, if you want to test whether the parser extracts the tokens correctly, you can implement a method called testGetToken. The collection of test methods you implement forms a test suite.
In each test method you create, use the assertion mechanism provided by CppUnit to compare the results of running the test and the results you expected. This will enable you to create repeatable tests as well as saving you lots of time from visually inspecting the results. Finally, use the textual version of the TestRunner tool to run the tests and collect the results. As each test is run, CppUnit will provide feedback on whether the test ran successfully, or the test failed, or an exception has occurred.
Example In this section, I will describe how you can use CppUnit using an example. A new Window will appear. Under the projects tab, select "Win32 Console Application". This directory will be used to store all the files for this project create the directory if necessary. Next, give the project a name. Under Project Name, enter "tutorial". You will be prompted for the kind of Console Application - just select "An empty project" and click on the Finish button.
A window showing the new project information will be displayed. Click on the Ok button to close it. Next, you need to create the classes used for the example. Take a few minutes to examine the following two classes. Each Course contains a name eg. CSs and an integer grade which ranges from 0 to Each Student has a name, a number as well as a list of course grades.
You can add the grade that a student scores at a particular course using the assignGrade method and retrieve the grade of a particular course using the getGrade method. Next, create the test class. The following are the test files I wrote for the Student class called TestStudent.
Crash Course in using CppUnit
It is ported by Michael Feathers. Recall that one of the required program quality attributes for your project is reliability. CppUnit can be used to help you achieve that. This document can be considered a "port" of the JUnit Guide I wrote. As in the JUnit Guide, I have included only the bare minimum to get you started. I will first go through the installation of CppUnit in the next section, followed by a description of how to use CppUnit using a sample program. Next, I will suggest some ways of organizing your project and test codes before ending the document with pointers to some useful references.
This document describes the recent, stable version of CppUnit we introduce version 1. This document assumes that you already have Visual Studio. Preliminary Download the local copy of CppUnit. Right-click on the cppunit project in the Solution Explorer pane and choose Build.
Unit testing with CPPUnit
You want to know whether your code is working. How do you do it? There are many ways. Stepping through a debugger or littering your code with stream output calls are two of the simpler ways, but they both have drawbacks. Stepping through your code is a good idea, but it is not automatic. You have to do it every time you make changes. Streaming out text is also fine, but it makes code ugly and it generates far more information than you need most of the time.