Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2006-08

data driven fpc unit tests

3 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Vincent Snijders

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 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

Reply by email

Dean Zobec
to Vincent Snijders

Vincent Snijders pravi:

Show quoted text 48 lines
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 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

Reply by email

Vincent Snijders
to Dean Zobec
Dean Zobec wrote 58 lines
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 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.

Reply by email