AUnit

Da Wikipedia, l'enciclopedia libera.
Vai alla navigazione Vai alla ricerca
AUnit
software
Genereframework di unit testing
SviluppatoreEd Falis[1]
Ultima versione3.8
Sistema operativoMultipiattaforma
LinguaggioAda
LicenzaGPL
(licenza libera)
Sito weblibre.adacore.com/tools/aunit/

AUnit è un framework di unit testing della famiglia xUnit per il linguaggio Ada, sviluppato originariamente da Ed Falis e mantenuto da AdaCore, distribuito insieme a GNAT.[1]

Framework[modifica | modifica wikitesto]

AUnit è costituito da un insieme di package che implementano le varie componenti del framework, la cui radice è il package AUnit:

  • AUnit
    • Assertions
    • Last_Chanche_Handler
    • Memory
      • Utils
    • Options
    • Reporter
      • GNATtest
      • Text
      • XML
    • Run
    • Simple_Test_Cases
    • Test_Caller
    • Test_Cases
      • Registration
      • Specific_Test_Case_Registration
    • Test_Filters
    • Test_Fixtures
    • Test_Results
    • Test_Suites
    • Tests
    • Time_Measure

Simple Test Cases[modifica | modifica wikitesto]

Il tipo tagged astratto AUnit.Simple_Test_Cases.Test_Case definisce il test case più semplice, da cui derivano gli altri test case della libreria e che può essere usato come base per tipi di test case definiti dall'utente. Per tale tipo sono definite alcune routine che, tramite l'override, permettono di specificare il comportamento:

function Name (T : Test_Case) return Message_String is abstract;
procedure Run_Test (T : in out Test_Case) is abstract;
procedure Set_Up (T : in out Test_Case);
procedure Tear_Down (T : in out Test_Case);

La funzione Name definisce il nome del test case e la procedura Run_Test contiene il codice da eseguire nel test, con le eventuali asserzioni definite nel package AUnit.Assertions. Le procedure Set_Up e Tear_Down definiscono le eventuali azioni da eseguire rispettivamente prima e dopo ogni test, tipicamente per configurare o pulire l'ambiente di esecuzione.[2]

Un esempio è il seguente:

with AUnit;
with AUnit.Simple_Test_Cases;
package Test_Semplice is
   type Test is new AUnit.Simple_Test_Cases.Test_Case with null record;
   overriding function Name (T : Test) return AUnit.Message_String is
      (if True then AUnit.Format ("Test operazioni") else AUnit.Format (""));
   overriding procedure Run_Test (T : in out Test);
end Test_Semplice;

with AUnit.Assertions; use AUnit.Assertions;
package body Test_Semplice is
   procedure Run_Test (T : in out Test) is
      I, J : Integer := 1;
   begin
      Assert (I + J = 2, "Errore nella somma");
   end Run_Test;
end Test_Semplice;

Test Cases[modifica | modifica wikitesto]

Oltre al più elementare tipo definito in AUnit.Simple_Test_Cases, nel framework è definito il tipo astratto AUnit.Test_Cases.Test_Case, che permette di comporre più routine in un test case. La procedura Run_Test è già definita e non deve essere sovrascritta, e il package fornisce anche un tipo per le routine di test e alcune procedure:

type Test_Routine is access procedure (Test : in out Test_Case'Class);
procedure Register_Tests (Test : in out Test_Case) is abstract;
procedure Set_Up_Case (Test : in out Test_Case);
procedure Tear_Down_Case (Test : in out Test_Case);

Le singole routine di test, contenenti le asserzioni, devono essere accessibili come Test_Routine, e la procedura Register_Tests permette di registrare le routine di test da eseguire, tramite la procedura AUnit.Test_Cases.Registration.Register_Routine, mentre le routine Set_Up_Case e Tear_Down_Case vengono eseguite rispettivamente prima e dopo l'intero test case (ovvero l'insieme di routine registrate).[2]

Test Fixture[modifica | modifica wikitesto]

Il tipo astratto AUnit.Test_Fixtures.Test_Fixture permette di definire delle fixture, le cui variabili sono i campi aggiunti estendendo il record. Ogni routine di test ha una propria istanza della fixture, per cui non c'è il rischio che qualche test influenzi gli altri tramite side effect. Anche per le fixture è possibile definire le procedure Set_Up e Tear_Down, eseguite rispettivamente prima e dopo ogni routine di test. I test case possono essere creati, ad esempio all'interno di una suite, a partire dalla fixture istanziando il package generico AUnit.Test_Caller e invocandone la funzione Create.[2]

with AUnit;
with AUnit.Test_Fixtures;

package Test_Con_Fixture is
   type Test is new AUnit.Test_Fixtures.Test_Fixture with record
      I, J : Integer;
   end record;
   overriding procedure Set_Up (T : in out Test);
   procedure Test_Sottrazione (T : in out Test);
end Test_Con_Fixture;

with AUnit.Assertions; use AUnit.Assertions;
package body Test_Con_Fixture is
   procedure Set_Up (T : in out Test) is
   begin
      T.I := 1;
      T.J := 1;
   end Set_Up;

   procedure Test_Sottrazione (T : in out Test) is
   begin
      Assert (T.I - T.J = 0, "Errore nella sottrazione");
   end Test_Sottrazione;
end Test_Con_Fixture;

Suite[modifica | modifica wikitesto]

Più test case possono essere combinati in una suite di test, usando i tipi definiti in AUnit.Test_Suites. Una suite consiste in un package contenente una funzione Suite, che restituisce un accesso ad un oggetto di tipo Test_Suite, al quale siano stati aggiunti i test case da eseguire. I test possono essere allocati staticamente o dinamicamente nel body del package, e vengono aggiunti alla suite tramite la procedura Add_Test.[2] Le suite e i test sono implementate con il pattern composite, in quanto hanno un tipo base in comune; poiché la procedura Add_Test accetta come parametro un argomento di tipo Test_Suite'Class per l'elemento da aggiungere, è anche possibile combinare più suite aggiungendo una all'altra.[3]

Ad esempio, per creare una suite che esegua i due test definiti negli esempi precedenti (notare che mentre il primo è allocato staticamente, il secondo, essendo basato su una fixture, è creato con la funzione AUnit.Test_Caller.Create):

with AUnit.Test_Suites; use AUnit.Test_Suites;
package Test_Suite is
   function Suite return Access_Test_Suite;
end Test_Suite;

with Test_Semplice;
with Test_Con_Fixture;
with AUnit.Test_Caller;
package body Test_Suite is
   package Caller is new AUnit.Test_Caller (Test_Con_Fixture.Test);
   T: aliased Test_Semplice.Test;

   function Suite return Access_Test_Suite is
      S : constant Access_Test_Suite := new Test_Suite;
   begin
      S.Add_Test (T'Access);
      S.Add_Test (Caller.Create ("Test sottrazione", Test_Con_Fixture.Test_Sottrazione'Access));
      return S;
   end Suite;
end Test_Suite;

Reporter[modifica | modifica wikitesto]

Una suite può essere eseguita tramite un reporter, che esegue i vari test contenuti e genera un report. Il framework fornisce due tipi di default per il reporter che generano un unico output alla fine dell'esecuzione di tutti i test; uno (AUnit.Reporter.Text.Text_Reporter) restituisce un output in testo semplice ed è utilizzabile a riga di comando, mentre l'altro (AUnit.Reporter.XML.XML_Reporter) restituisce un output in XML, utile per generare file di report o includere l'esecuzione degli unit test in un ambiente di sviluppo integrato.[2] La versione di AUnit distribuita con GNAT contiene anche il reporter AUnit.Reporter.GNATtest.GNATtest_Reporter, impiegato per i test generati automaticamente con lo strumento GNATtest, incluso ad esempio nell'ambiente GNAT Programming Studio.[4]

Un esempio di programma per l'esecuzione della suite definita nell'esempio precedente, generando un report in testo semplice a riga di comando:

with Test_Suite;
with AUnit.Run;
with AUnit.Reporter.Text;
procedure Esegui_Test is
   procedure Run is new AUnit.Run.Test_Runner (Test_Suite.Suite);
   Reporter : AUnit.Reporter.Text.Text_Reporter;
begin
   Reporter.Set_Use_ANSI_Colors(True); -- usa colori nell'output a riga di comando
   Run (Reporter);
end Esegui_Test;

Note[modifica | modifica wikitesto]

  1. ^ a b AUnit, su libre.adacore.com, AdaCore.
  2. ^ a b c d e AUnit Cookbook, su docs.adacore.com, AdaCore, 8 agosto 2015 (archiviato dall'url originale il 30 agosto 2015).
  3. ^ Commenti al codice sorgente del file aunit-test.ads del framework AUnit distribuito con GNAT GPL Edition.
  4. ^ Commenti al codice sorgente del file aunit-reporter-gnattest.ads del framework AUnit distribuito con GNAT GPL Edition.

Collegamenti esterni[modifica | modifica wikitesto]

  • AUnit Cookbook, su docs.adacore.com, AdaCore, 8 agosto 2015 (archiviato dall'url originale il 30 agosto 2015).
  Portale Informatica: accedi alle voci di Wikipedia che trattano di informatica