A DUnit Folder Iterator Extension

You have unit tests, don’t you? Or at least you run some tests to make sure your recent changes broke something?

These regression tests are vital to any serious software development. What would your customers say when they get a new version breaking their day to day work? Well, at least I can’t afford that – and be it just that I’m not keen to fix those problems at a customer under high pressure. So I have regression tests.

Writing those tests for a long standing program can be quite time consuming, but you have to start somewhere. In this case it came out that my application takes an input file and produces an output file from that. So I asked my customers to send me plenty of their input files. The amount of files I got was – well, let’s say unexpected.

I took each file and produced the corresponding output file as a reference. The regression tests also take each input file, produce the output file and compares it with the reference file. If anything is different the test fails and I have to take a look why it fails. Sometimes the fail is intended and I have to update the reference file.

The big challenge was to write the unit tests for all these input files (> 1000). My first attempt was to put a loop into a test case that iterates over all files. That worked but had a major drawback: when one file failed, the complete test failed at that file and the remaining files were not checked at all.

Writing one test case for each file looked far less comfortable, so I wrote a little extension to DUnit that dynamically creates test cases for each file found in a given folder.

The extension introduces two new classes: TFileTestCase derived from TTestCase and TFolderTestSuite derived from TTestSuite.

To make use of these you derive a test case from TFileTestCase and implement any test methods as published just as you do for a normal test case. The current file for the test case can be taken from the new property TestFileName.

The critical part is to override the class method Suite, which returns an ITestSuite interface. Here you create an instance of TFolderTestSuite with some appropriate parameters. The required first parameter for TestClass can simply be given with self, which in a class method gives the current class type.

Here is the code for the extension unit:

Author: Uwe Raabe

Addicted to Pascal/Delphi since the late 70's