[fpc-pascal] data driven fpc unit tests

Vincent Snijders vsnijders at quicknet.nl
Fri Aug 18 22:04:49 CEST 2006


Dean Zobec wrote:
> Vincent Snijders pravi:
> 
>>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
> 
> 
> Yes, there is an elegant solution. Joost has already asked me the same
> question two months ago and I've prepared in that occasion a small
> example of parameterized testcase that can be used for data driven
> tests. The file with a short comment is attached.
> HTH,
> Dean

Yes, reading it, I think I understand it.

Of course the proof will be when I have used and implemented it for my 
purposes.

Thanks a lot.

Vincent

P.S. Maybe you can add this the wiki.



More information about the fpc-pascal mailing list