Difference between revisions of "UnitTesting"
Line 38: | Line 38: | ||
When writing unit tests, one will create a lot of similar code. Since unit tests are nothing more then little programs, several tasks will be repeated, setting up the test environment, create your class, call a method, verify it's result, write out an error message when the test fails, preferably specifying what the wrong outcome was, and what the expected result is. | When writing unit tests, one will create a lot of similar code. Since unit tests are nothing more then little programs, several tasks will be repeated, setting up the test environment, create your class, call a method, verify it's result, write out an error message when the test fails, preferably specifying what the wrong outcome was, and what the expected result is. | ||
All these repetitive chores are taken care of by the frame work so the developer can focus on the real code of the test. | All these repetitive chores are taken care of by the frame work so the developer can focus on the real code of the test. | ||
− | This document will use a small but very effective framework for the C++ language. It is called UnitTest++. It's homepage is at : [ | + | This document will use a small but very effective framework for the C++ language. It is called UnitTest++. It's homepage is at : [http://unittest-cpp.sourceforge.net/ UnitTest++] |
Revision as of 18:40, 8 September 2009
This document is under development by killerbot. Starting from 7 September 2009.
Introduction
This document will describe how unit testing can be combined with the Code::Blocks IDE. It will give a hands-on example on a unit testing framework and how it can be used with Code::Blocks.
What is Unit Testing
Simply put Unit Testing is the discipline and best practice of writing and running little test programs that test little units of code. Such a unit can be :
- a class
- a free function
- an interface
- ...
There are several unit test frameworks available to help you write, deploy, manage your unit tests. The most famous family is the xUnit framework, and some of it's descendants.
Why Unit Testing
Well we write code that implements some functionality. We need a way to check and prove that the implementation is correct. Therefor we need tests. We need tests on the application level, at the integration level of different components, classes, interfaces. But we also need tests for the smallest building blocks, the units. We also wans these tests to run very quickly. That we we can run the tests often, manually or automated. These tests will also be our safeguards during refactoring of the code, extending the code or whatever maintenance tasks we carry out on the code. These tests will also make us think of how our class, method, ... will be used. Since the test can be seen as a user of our code. Thanks do this, thinking about the tests before implementing the functionality can help us in the design : emergent design. It will help us to avoid creating big classes, complex functions. We will only write the methods that are really needed, and no methods that we think might (n)ever be useful in the future. We are no good future tellers ! Those 'might be needed' methods and their tests are just a waste of time. Extending, refactoring will be done when it is actually needed, and at that time we have our unit tests to ensure we don't break any existing code.
Other times to add new unit tests are typically when a bug is discovered; Write a unit test to reproduces the bug, fix the code, and from now on the new unit test will watch your back so the bug won't reappear.
Unit test are also a good place of documentation. The little tests show how the class or method is used. The code documents itself.
So the benefits are :
- better design
- prove of your code
- safe guard during code maintenance (refactoring, extensions)
- documentation
- ...
Unit Test frameworks
When writing unit tests, one will create a lot of similar code. Since unit tests are nothing more then little programs, several tasks will be repeated, setting up the test environment, create your class, call a method, verify it's result, write out an error message when the test fails, preferably specifying what the wrong outcome was, and what the expected result is. All these repetitive chores are taken care of by the frame work so the developer can focus on the real code of the test. This document will use a small but very effective framework for the C++ language. It is called UnitTest++. It's homepage is at : UnitTest++