Untitled Document
Delphi Advanced Tools: Part 2: (continuation 3)
UML, Unit Testing, Audits, Metrics, and Refactoring by Michael Rozlog
Page 12
Page 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 |
|
Part III: Integrated Unit Testing
It is one of the great mysteries in life; we provide a tool to help people write better software and the tool is not used… why? There could be many answers to the question, but I would like to believe that most developers of Delphi simply don’t know about the functionality or maybe they don’t know it is integrated into the IDE, or they don’t know the integration writes a majority of the code for you. I refuse to believe that Delphi developers don’t want to use unit testing for whatever reason. So, I’m going to make the assumption that it’s one of the other issues causing Delphi developers not to use the DUnit integration found in all levels of RAD Studio, Delphi, and C++Builder.
This leads me to conclude that we need to review what “standardized” unit testing is. If you have been developing software for any time at all, you most likely do some form of testing: unit, white-box, black-box, etc. Testing is part of every developer because no matter the code being developed, sooner or later you will have to do some type of testing. If you are going to do testing why not make the job as painless as possible? Use a unit testing framework like the ones based on the xUnit approach.
The xUnit testing framework was originally written by Kent Beck in the mid-90s for the SmallTalk language. It was then ported to Java in the late-1990’s and was named JUnit, since the original framework that Kent Beck created was called SUnit. Then over the next couple of years, as JUnit became ever-more popular, various other developers started to port the xUnit framework to other languages and now there is nearly an xUnit framework available for almost every language out there, including Delphi.
The xUnit framework approach is based on code-driven testing. The xUnit framework allows for testing of “units of software,” including Delphi units, classes, objects, functions and procedures. One of the big advantages of xUnit is that most of the code, except for the actual test code is written for the developer. That means that you don’t have to write and rewrite the plumbing over and over again.
Some of the main features of the xUnit framework include the following areas:
- Test cases: these are the actual testing units.
- Test Suites: this give the ability to run a set of Test cases.
- Test Fixtures: these are test with known pre-conditions. Think of a fixture as a database test for Oracle. You already know that every time you write a test for Oracle it is going to have the same connection string. So instead of using a basic test case, you can create a fixture with those known features.
- Test Repeater: this allows you to call a test case “x” number of times.
- Console Runner: this allows the tests to be run from the console.
- GUI Test Runner: this is a nice GUI interface for running and evaluating tests.
- Test Assertions: they verify the behavior of the test. If the assertion fails, it normally throws an exception and aborts the execution of the current test.
Great, but how do I write tests in Delphi? Glad you asked. We should start out with a very basic structure as the first example. Say you have a super simple Delphi program called CalcEngine like the following:
|
unit CalcEngine;
interface
type
TheCalcEngine = class
public
function Calc2Numbers(num1, num2: Integer): String;
end;
implementation
uses SysUtils;
{ TheCalcEngine }
function TheCalcEngine.Calc2Numbers(num1, num2: Integer): String;
var
answer: Integer;
begin
answer := num1 * num2;
result := IntToStr(answer);
end;
end.
|
As you can see this is not overly complicated, but it will do for the example that we are going to write. So in the Delphi environment, I have the project setup as I normally would. The next thing I want to do is to add the unit testing unit for this unit. |
Back Next
Back to Part 1 | Back to Part 2
|
|

New Lazarus-support website http://www.lazarussupport.com/
|
 |
An English version of the recently published (German) Lazarus book is now going to be translated.
We'll keep you informed about the work of the authors: Michael Van Canneyt, Mattias Gaertner, Swen Heinig, Felipe Monteiro de Carvalho and Inoussa Ouedraogo..

|
| We are pleased to announce the
release of DeZign for
Databases V6.2,
an update to our easy-to-use
database design tool.
DeZign for Databases V6.2 introduces schema/owner mapping, a new feature that enables you to map schema names in your data model to schema names in your database when comparing (and synchronizing) your model with your database. The new version also adds support for case insensitive comparison of model and database. Version 6.2 offers several other enhancements, such as a improved generation of PDF reports and improved model-to-database synchro-nization scripts.
Visit our website for a complete list of changes: |
|
Short Privacy Declaration:
Blaise Pascal Magazine will not
provide
or sell any personal information
about
subscribers to any other party,
except in
the case of a verified judicial
request from
a recognised Government
Agency or to its
own related
organisations.
Click here for full details of our
Privacy Statement |
|