Difference between revisions of "FPTest/fr"

From Lazarus wiki
(Created page with "{{FPTest}} FPTest (Free Pascal Testing Framework) est un framework de test d'unité. C'est une branche (''fork'') du projet DUnit2 créé par feu Peter McNab, mais adapté sp...")
 
m (Voir aussi)
Line 180: Line 180:
 
= Voir aussi =
 
= Voir aussi =
 
* [[fpcunit/fr|fpcunit]] Framework de test d'unité fourni avec FPC
 
* [[fpcunit/fr|fpcunit]] Framework de test d'unité fourni avec FPC
 +
* https://www.freepascal.org/~michael/articles/unittesting/unittesting.pdf Article sur le test d'unité (DUnit).
 +
<br/>

Revision as of 12:13, 18 October 2017

English (en) français (fr) polski (pl)

FPTest (Free Pascal Testing Framework) est un framework de test d'unité. C'est une branche (fork) du projet DUnit2 créé par feu Peter McNab, mais adapté spécialement pour l'utilisation avec le compilateur Free Pascal.

La plupart des articles listés ci-dessous sont relatifs au projet DUnit2 original (et maintenant le projet FPTest) et comment il diffère du projet DUnit. L'information sur la migration depuis fpcunit est aussi présentée.

Caractéristiques

Objectifs de conception utilisés lors du développement du projet, pas nécessairement dans un ordre particulier :

  • Introduire SetUpOnce et TearDownOnce par TestCase et TestSuite.
  • Améliore la vitesse d'exécutiojn des tests
  • Introduit les projets multiples et ouvre la voie à l'exécution de projet de test dans des threads séparés.
  • Améliore la facilité d'utilisation de FPTest (en partie en ajoutant plus d'exemples dans la documentation).
  • Garantit que les Constructeurs et Destructeurs de TTestCase s'exécutent une seule fois, pas pour chaque méthode de test contenue.
  • Fournir un moyen non global var pour partager des données de configuration de haut niveau entre les unités de test.
  • Des résultats de test plus précis et des retours améliorés.
  • Fournit un décompte visuel des fuites de mémoire détectées et des tests sans Check().
  • Fournit un décompte facultatif pour identifier quels appels Check() ont échoué.
  • Nettoyage du code, en supprimant dans FPTest le code "légataire" (legacy), tel que le code spécifique Delphi Win32 et .Net.
  • Test Runner en mode texte pour ceux qui aiment les consoles ou pour la suite de tests sans assistance qui s'exécute sur les serveurs.
  • Une nouvelle IHM du Test Runner pour fpGUI ou les projet basés sur la LCL.
  • Un décorateur de test encore plus avancé qui tourne actuellement (celui de FPCUnit est totalement délabré).
  • Les tests enregistrés peuvent être bien mieux organisés, en construisant une hiérarchie de tests, les rendant plkus faciles à gérer (activer, désactiver, sauter les tests etc.)

Etat actuel

  • New code remains compatible with existing test suites not relying on user modified DUnit code.
  • If your FPCUnit test projects use the CheckXXX() methods and not the AssertXXX() methods, it should be quite painless to switch from FPCUnit to the FPTest testing framework.
  • New code remains compatible with existing test suites not relying on low level TestFramework functions (e.g. TTestResult).
  • New code introduces a Project and ProjectManager class between the GUI and TextTestRunners to assist the future introduction of threaded unit testing.
  • SetupOnce and TeardownOnce code reduces node and name clutter where test decorators would have previously been required. Test Decorator in FPCUnit has same design flaws, and doesn't work as expected.
  • New Test Decoration code is easier to comprehend and extend.
  • Execution call depth per TestCase procedure has been reduced in an effort to improve testing efficiency. (Execution path is easier to follow and so eases future code change).
  • Individual test procedure timing is now taken on just the test method and excludes SetUp and TearDown times. The intention being to provide higher timing accuracy for method profiling. Overall Timing still brackets all code.
  • All existing Check procedures are supported. Several new Check procedures have been added to complete the range available.
  • New unit tests have been created to verify all Check() procedures detect pass and fail conditions.
  • All tests run without memory leaks or the need to allow leaks.
  • New self-test unit tests are more comprehensive including checks to ensure setup conditions are properly met before executing the required tests.
  • A Project class now handles registration and execution of grouped TestCases (TestSuites) and TestCases.
  • Counted Tests can now be invoked inside nested Test Decorators to provide 2 or more dimensional test data
  • Non Optimized empty tests still fail.
  • Tests that don't call Check() can be globally armed to fail, i.e. from the GUI.
  • Similarly tests causing memory leaks can optionally be globally armed to fail.
  • Individual tests can optionally override the above GUI settings. (e.g. where a third party component has a known leak)
  • All tests that override GUI settings can optionally be identified after test execution by additional node colours.
  • A count of overridden test failures is included in the execution status display.
  • Overridden tests which would otherwise have failed generate a separate warning count visible in the updated GUI and Text output
  • Test Method names are decoupled from the execution process, allowing post compile information to be displayed at the GUI level.
  • Added XML report generator, supplementing both GUITestRunner and TextTestRunner.
  • Added capability in both GUITestRunner and TextTestRunner to skip (Exclude) individual tests separate from Enabling/Disabling tests.
  • Tool buttons to GUITestRunner to ease control and show at runtime which soft options have been selected.

To facilitate early testing without completely re-developing GUITestsRunner and TextTestRunner an intermediate Proxy unit and a TTestResults replacement has been interposed between the GUI and the new TestFramework. Although the original aim was to eventually completely re-develop the GUI the "Proxy" classes are working very well and may not need further work.

Obtenir le code source

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 code tarball (archive) too, using the following URL.

https://github.com/graemeg/fptest/tarball/master

The FPTest project is under active development, so code and documentation are constantly improved. For this reason I highly recommend you get the source code via git, instead of as a tarball.

Documentation

FPTest documentation can be found in the docs directory as HTML files.

Exemple de test

Here is a console sample testing project

program project1;

{$mode objfpc}{$H+}

uses
 Classes,
 TextTestRunner,
 sample_tests;

begin
 // Register all tests
 sample_tests.RegisterTests;

 RunRegisteredTests;
end.

and the unit containing the actual tests...

unit sample_tests;

{$mode objfpc}{$H+}

interface

uses
 TestFramework;  // required for TTestCase and CheckXXX() references

type
 TTestCaseFirst = class(TTestCase)
 published
   procedure TestWarning;
   procedure TestOne;
   procedure TestNoError;
   procedure TestThree;
   procedure TestFour;
 end;

procedure RegisterTests;

implementation

uses
  SysUtils;

{ here we register all our test classes }
procedure RegisterTests;
begin
  TestFramework.RegisterTest(TTestCaseFirst.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.TestNoError;
begin
  Check(1 + 1 = 2, 'Catastrophic arithmetic failure!');
end;

procedure TTestCaseFirst.TestThree;
var
  s: string;
begin
  s := 'hello';
  CheckEquals('Hello', s, 'Failed CheckEquals');
end;

procedure TTestCaseFirst.TestFour;
var
  x, y: integer;
begin
  x := 10;
  y := 0;
  Check(x / y = 0, 'Failed on 1');
end;

end.

Captures d'écran

Here is a screenshot of the Text Test Runner output.

Sample output of the console Text Test Runner.

Here is a screenshot of the GUI Test Runner (using the fpGUI Toolkit) to run the tiOPF test suite.

A screenshot of the GUI Test Runner using the fpGUI toolkit.

Comment le comparer à FPCUnit?

I (Graeme Geldenhuys) was a long time FPCUnit user. I contributed towards, and helped extend the FPCUnit project. But FPCUnit has some design flaws which makes it mostly useful for smaller projects and less complicated test suites. FPCUnit's test decorators, Setup/Teardown etc is severely broken. The FPTest testing framework overcomes all those issues, adds many more features, is easier to manage your tests, and executes faster that FPCUnit or DUnit equivalent projects.

The good news is that you are not forced to choose between either testing framework. 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 (instead of the AssertXXX() calls), later you can easily switch to the FPTest framework without any need for changing your testing code.

Voir aussi