[fpc-pascal] Testing applications with FPCUnit

Marcos Douglas md at delfire.net
Mon Dec 19 12:06:08 CET 2011


On Mon, Dec 19, 2011 at 5:26 AM, Graeme Geldenhuys
<graemeg.lists at gmail.com> wrote:
> On 17 December 2011 18:03, Luciano de Souza  wrote:
>> The idea of FPCUnit is really wonderful. I really would like to use it, but
>> Lazarus dependencies can complicate a lot.
>
> I have been a long time user of FPCUnit and even helped to maintain
> and improve it over the years. It has some design flaws and
> limitations though, so is good for smaller projects and less
> complicated test suites.
>
> For a more feature complete testing suite, I would recommend the
> FPTest (Free Pascal Testing Framework) project instead. I am the
> current maintainer of FPTest - which is a fork/continuation of the
> DUnit2 testing framework created by the late Peter McNabb, but
> specifically tailored for the Free Pascal compiler. FPTest fixes all
> of the problems I experienced with FPCUnit. It supports Text and GUI
> (only fpGUI at the moment, but a LCL UI will follow shortly) test
> projects out of the box. No dependencies on any IDE (Lazarus IDE or
> otherwise), and no GUI toolkits are required either. The GUI UI does
> make the usage (and advanced features) much easier though - but it is
> by no means a requirement. The Text runner can do everything the GUI
> runner can.
>
> FPTest has a many more features than FPCUnit: multiple projects
> support, extended CheckXXX calls (known as AssertXXX in FPCUnit),
> improved error reporting, warning support, huge self-test testing
> frameworking, improved decorator tests (which are flawed in FPCUnit),
> improved Setup/TearDown and SetupOnce/TearDownOnce support etc.
>
> Here is a console sample testing project
>
> ---------------------[ project1.pas ]------------------------
> program project1;
>
> {$mode objfpc}{$H+}
>
> uses
>  Classes,
>  TextTestRunner,
>  sample_tests;
>
> begin
>  // Register all tests
>  sample_tests.RegisterTests;
>
>  RunRegisteredTests;
> end.
> ---------------------------------[ end ]----------------------------------
>
> and the unit containing the actual tests...
>
> -------------------------[ sample_tests.pas ]----------------------
> unit sample_tests;
>
> {$mode objfpc}{$H+}
>
> interface
>
> uses
>  TestFramework;
>
> type
>  TTestCaseFirst = class(TTestCase)
>  published
>    procedure TestWarning;
>    procedure TestOne;
>    procedure TestTwo;
>    procedure TestThree;
>  end;
>
>  TClassA = class(TTestCase)
>  published
>    procedure TestClassA1;
>    procedure TestClassA2; virtual;
>  end;
>
>  TClassB = class(TClassA)
>  published
>    procedure TestClassA2; override;
>    procedure TestClassB1;
>    procedure TestError;
>  end;
>
>
> procedure RegisterTests;
>
>
> implementation
>
> uses
>  sysutils;
>
>
> procedure RegisterTests;
> begin
>  TestFramework.RegisterTest(TTestCaseFirst.Suite);
>  TestFramework.RegisterTest(TClassB.Suite);
> end;
>
> { TTestCaseFirst }
>
> procedure TTestCaseFirst.TestWarning;
> begin
>  // Do nothing here - should cause a Warning
> end;
>
> procedure TTestCaseFirst.TestOne;
> begin
>  Check(1 + 1 = 3, 'Catastrophic arithmetic failure!');
> end;
>
> procedure TTestCaseFirst.TestTwo;
> begin
>  Check(1 + 1 = 2, 'Catastrophic arithmetic failure!');
> end;
>
> procedure TTestCaseFirst.TestThree;
> var
>  s: string;
> begin
>  s := 'hello';
>  CheckEquals('Hello', s, 'Failed CheckEquals');
> end;
>
> { TClassA }
>
> procedure TClassA.TestClassA1;
> begin
>  fail('TClassA.TestClassA1');
> end;
>
> procedure TClassA.TestClassA2;
> begin
>  Fail('This virtual method should never appear.');
> end;
>
> { TClassB }
>
> procedure TClassB.TestClassA2;
> begin
>  Fail('Test overridden method');
> end;
>
> procedure TClassB.TestClassB1;
> begin
>  sleep(2264);
>  Fail('Test sleep() causing extra time');
> end;
>
> procedure TClassB.TestError;
> var
>  x, y: integer;
> begin
>  x := 10;
>  y := 0;
>  Check(x / y = 0, 'Failed on 1');
> end;
>
> end.
> ---------------------------------[ end ]----------------------------------
>
>
> The source code is freely available from Github using the following command.
>
>  git clone git://github.com/graemeg/fptest.git
>
> or
>
>  git clone https://github.com/graemeg/fptest.git
>
>
> If you don't have git installed, you can always grab a source tarball
> too, using the following URL.
>
>  https://github.com/graemeg/fptest/tarball/master
>
>
> FPTest documentation can be found in the 'docs' directory as HTML
> files. The FPTest project is still under active development, so things
> are constantly improved. For this reason I highly recommend you get
> the source code via git, instead of as a tarball.
>
> Good news is that you are not forced to choose either or testing
> frameworks.  Years ago I included a DUnit/FPTest compatibility
> interface to FPCUnit. This gives you a nice upgrade path from FPCUnit
> to FPTest. So if you design your test suites using the CheckXXX calls
> and not the AssertXXX calls, then later you can easily switch to the
> FPTest framework without any need for changing your testing code.

I didn't know this project. I'll try.

Marcos Douglas



More information about the fpc-pascal mailing list