[fpc-pascal] data driven fpc unit tests

Vincent Snijders vsnijders at quicknet.nl
Fri Aug 18 21:12:55 CEST 2006


Hi,

Suppose I have a component TMyDataProcessor with the following declaration:

type
   TMyDataProcessor = class
      function process(const s: string) : string;
   end;

Now I want to test it with different strings, but instead of hard coding 
them I put them in a inifile, so I can easily extend the test.

[test 1]
Input=Test
Output=tEST

[test 2]
Input=1234
Output=1234

Now I write a fpcunit test that reads those tests input/output pairs 
from the ini file and executes them, see following (psuedo) code:

procedure TMyTestCase.TestOutputs;
var
   MyDataProcessor: TMyDataProcessor;
begin
   for each section in the ini-file do begin
      MyDataProcessor := TMyDataProcessor.Create;
      Load Input, Load Output
      AssertEquals(SectionName + 'failed. ',
         Output, MyDataProcessor(Input));
      MyDataProcessor.Free;
   end;
end;

As far as I can see, the drawback of using this method is that if there 
is a failure in test 1, test 2 won't run at all. I will loose 
information about what could have been wrong.

A solution would be to have different test methods for each test in the 
ini file, but I want to avoid that, because then adding a test, will 
mean that I need to change the code (after having created the data).

Do you know how I can create data driven tests, so that it will display 
the test results of the second test, even if the first test fails.

Vincent



More information about the fpc-pascal mailing list