Unit testing has been here for a long time, and so has Unit Testing in Delphi. Below a summary of historic links together with some notes on how the state of affairs changed over the years.

Charlie Calvert

I’ll start with one of the first large Delphi Unit Testing articles was a paper by Charlie Calvert summarizing the state of the art on Delphi Unit Testing in 2004. It is present in the wayback machine as DUnit Talk and on his elvenware.com site.

DUnit; Juanco Añez

http://sourceforge.net/projects/dunit/

Charlie’s paper describes both NUnit (the .NET unit testing framework) and DUnit (originally by Juanco Añez as a 1-on-1 port from JUnit in 1999, the Delphi 5 era, back when Charlie was evangelist at Borland) with some sample code at charlie’s sourceforge site.

Back then, both the paper and the sample code were very welcome, as there was virtually no DUnit documentation (apart from the README and Juanco’s InPublishing articles about Extreme Testing and Extreme Testing for Delphi programs) and people tended to stick to their development stack (so there was not much mixing of the Delphi, Java and .NET world, luckily times have changed).

Around 2006, DUnit maintenance started to slow down, and – though still part of Delphi – around 2008 it completely stalled on CVS and SVN. But a fork appeared: DUnit2.

Juanco also developed a Delphi friendly build tool called WANT (after Ant, and NAnt), which now is at want-tool.org, but development also came to a halt in 2008 and the forum in 2010.

Mocking in Pascal and Delphi

Unit testing often requires mocking to simulate real world objects. There are about a dozen mocking libraries available for NUnit.

For Delphi, Kylix and FreePascal, PascalMock used to be the mocking library, but it too halted, probably because back then RTTI was only available for published members and there was no way to automatically mock new classes and create instances of them on the fly .

Since Delphi 2009 however, RTTI has broadened a lot, and Delphi XE2 introduced TVirtualInterface in 2011 to implement SOAP consumer clients in Soap.RIO.pas. A great article about that is TVirtualInterface: Interfaces without an Implementing Class.

So I was glad that – after in 2011 a getting started guide and coverage by Nick Hodges in Delphi Mocks The Basics appeared, the DelphiMocks got officially announced in late 2012: VSoft Technologies Blogs | Introducing Delphi Mocks.

DSharp has some mocks too that bind with DUnit.

mORMot has their own mocking classes based on their own virtual class factory and a nice discussion with the DSharp author.

See also: unit testing – What is your favorite Delphi mocking library? – Stack Overflow.

DUnitWizard

But first about DUnitWizard, which still is part of Delphi too. And there is DUnitWizard documentation!

Basically it is a wizard that allows you to select the project and source file for which you want to generate a unit testing skeleton unit.

DUnit2

http://sourceforge.net/projects/dunit2/

DUnit2 is an improvement over DUnit: it improves both speed and structure, for instance allowing for “Early Exit”. The original aim was to merge back into DUnit, but that didn’t happen because DUnit2 also stalled in 2008. It does however have a better GUIRunner, so that might be worthwhile for future frameworks.

DUnit and DUnit2 are both used to test the tiOPF framework. The tiOPF is a native Object Persistence Framework for Delphi and FreePascal.

DUnit2 is also used to integrate into OpenCTF. With OpenCTF, you can test components (including Forms, Frames and DataModules) as well as classes. It tests all kinds of “obvious” things when using Delphi in a RAD way.

Remote testing your GUI using DUnit and automation

With the added RTTI in Delphi 2010 and up, you can now even remote test your GUI: André’s Dev Blog: Remote Control: automated GUI test with Delphi and DUnit. The proof of concept code is at https://code.google.com/p/asmprofiler/source/browse/#svn%2Ftrunk%2F-Other-%2FRemoteControl.

DUnitX

https://github.com/VSoftTechnologies/DUnitX

DUnitX is a complete rewrite, but has a DUnit compatibility layer.

A few main aims of DUnitX are to have it attribute based (for easier configuration), allow – like NUnit – any class to be a test fixture (added benefit of being attribute based), support for new features (collections, inversion of control, attributes) allowed by recent language improvements (generics, anonymous methods, attributes).

Another aim is to support NUnit XML output to make it easier to integrate with Continuous Integration systems (like Continua). On my research list is to check if DUnitX can be ran through the NUnit GUI Runner.

VSoft is actively maintaining DUnitX, introducing it in VSoft Technologies Blogs | Introducing DUnitX – A Unit Test Framework for Delphi.

The open source Delphi Unit Test project uses DUnitX as a foundation of their test suites.

German DUnit walk through

If you can read German, or can use Google translate, there is a good German walk-through on how to use DUnit (old broken link). Recommended!

TSynTest

TSynTest is a very light weight framework not compatible with DUnit or DUnit2.

It is used to test the synopse mORMot, another OPC framework for Delphi.

Sidestep to FreePascal

On the FreePascal side, there is fpcUnit, which is used to test the FreePascal database components. fpcUnit is an independent port of JUnit to FreePascal, so it is like DUnit, but slightly different, as described in Introduction to unit testing with Lazarus | The Programming Works.

fpcUnit is very similar to DUnit, but a lot less similar than DUnitX. For now, DUnitX cannot be used in FreePascal.

For FreePascal, there is also FPTest, which is a port of DUnit2 to FreePascal and used to test the FreePascal port of tiOPF (both now maintained by Graeme).

Mocking and Stubs

These are much related to unit testing, and you should read Martin Fowler’s article Mocks Aren’t Stubs before getting started.

Some frameworks are mentioned in these SO questions; more on those in a later blog post:

The Delphi Mocks Open Source Project on Ohloh.

Stubs and Mocks for Delphi with mORMot – Synopse.

Delphi Mocks: The Basics.

–jeroen