[fpc-pascal] data driven fpc unit tests

Dean Zobec dezobec at tin.it
Fri Aug 18 21:54:18 CEST 2006


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

-------------- next part --------------
A non-text attachment was scrubbed...
Name: paramstests.pas
Type: text/x-pascal
Size: 3498 bytes
Desc: not available
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20060818/8ea852e6/attachment.pas>


More information about the fpc-pascal mailing list