FPTest/fr

From Lazarus wiki
Jump to navigationJump to search

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

Voici une captuire d'écran d'une sortie du Test Runner en mode texte.

échantillon de sortie de la console Test Runner en mode texte.

Voici une capture d'écran du Test Runner avec IHM (utilisant le Toolkit fpGUI) pour exécuter la suite de tests tiOPF.

Une capture d'écran de l'IHM Test Runner utilisant le toolkit fpGUI.

Comment le comparer à FPCUnit?

J'ai (Graeme Geldenhuys) été un utilisateur de longue date de FPCUnit. J'ai contribué à l'extension du projet FPCUnit. Mais FPCUnit a quelques défauts de conception qui le rendent plus utiles pour les projets plus petits et des suites de tests moins compliquées. Les décorateurs de test de FPCUnit, Setup/Teardown etc sont sévèrement délabrés. Le framework de test surmonte tous ces problèmes, ajoute de nouvelles fonctionnalités, il est plus facile de gérer vos tests, et les exécute plus rapidement que des projets équivalents FPCUnit ou Dunit.

La bonne nouvelle est vous n'êtes pas forcés de choisir entre les frameworks de test. Il y plusieurs années, j'ai inclus une interface de compatibilité DUnit/FPTest à FPCUnit. Ce qui vous donne un bon chemin de mise à jour de FPCUnit vers FPTest. Donc si vous concevez votre suite de test en utilisant des appels CheckXXX() (au lieu des appels AssertXXX()), vous pourrez plus tard basculer facilement vers le framework FPTest sans avoir besoin de changer votre code de test.

Voir aussi