Advertisement
piffy

CunitBoilerPlate

Aug 1st, 2018
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.38 KB | None | 0 0
  1. /*
  2.  *  Scheletro di una unit test con Cunit.
  3.  *
  4.  *  Compilare incudendo la libreria CUnit nel progetto.
  5.  *  Con linea di comando, il comando da usare è il seguente.
  6.  *
  7.  *  gcc cunit_skel.c -o test.exe -lcunit
  8.  *  l'output di test.exe è questo:
  9.  *
  10.  *           CUnit : A Unit testing framework for C.
  11.  *           http://cunit.sourceforge.net/
  12.  *
  13.  * Suite: Suite_1
  14.  * Test: test della somma ...passed
  15.  * Test: test con numeri negativi ...FAILED
  16.  *   1. cunit_skel.c:73  - 4 == somma(2,-1)
  17.  *
  18.  * Run Summary:    Type  Total    Ran Passed Failed Inactive
  19.  *               suites      1      1    n/a      0        0
  20.  *                tests      2      2      1      1        0
  21.  *              asserts      3      3      2      1      n/a
  22.  */
  23.  
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include "CUnit/Basic.h"
  27.  
  28. /*
  29.  * Funzione soggetta al test (molto banale)
  30.  */
  31. int somma(int a, int b)
  32. { return a+b;}
  33.  
  34. /* Funzione che inizializza la suite.
  35.  * (in questo caso non fa fulla)
  36.  */
  37. int init_suite1(void)
  38. {
  39.       return 0;
  40. }
  41.  
  42. /* Funzione che conclude la suite.
  43.  * (in questo caso non fa nulla)
  44.  */
  45. int clean_suite1(void)
  46. {
  47.       return 0;
  48.    
  49. }
  50.  
  51. /* Test della funzione
  52.  * Questo test passa.
  53.  */
  54. void testSOMMA1(void)
  55. {
  56.    CU_ASSERT(2 == somma(1,1));
  57.    CU_ASSERT(4 == somma(3,1));
  58. }
  59.  
  60. /* Altro test
  61.  * Questo test fallisce (in modo da vedere l'output)
  62.  */
  63. void testSOMMA2(void)
  64. {
  65.    CU_ASSERT(4 == somma(2,-1));
  66. }
  67.  
  68. /* Esempio di main() che prepara e lancia i test.
  69.  * Se tutto va bene restituice CUE_SUCCESS on successful
  70.  */
  71. int main()
  72. {
  73.    CU_pSuite pSuite = NULL;
  74.  
  75.    /* Inizializza il registro dei testinitialize the CUnit test registry */
  76.    if (CUE_SUCCESS != CU_initialize_registry())
  77.       return CU_get_error();
  78.  
  79.    /* aggiungi una suite al registro */
  80.    pSuite = CU_add_suite("Suite_1", init_suite1, clean_suite1);
  81.    if (NULL == pSuite) {
  82.       CU_cleanup_registry();
  83.       return CU_get_error();
  84.    }
  85.  
  86.    /* aggiungi i test alla suite */
  87.    if ((NULL == CU_add_test(pSuite, "test della somma", testSOMMA1)) ||
  88.        (NULL == CU_add_test(pSuite, "test con numeri negativi", testSOMMA2)))
  89.    {
  90.       CU_cleanup_registry();
  91.       return CU_get_error();
  92.    }
  93.  
  94.    /* Esegui tutti i test tramite l'interfaccia CUnit Basic */
  95.    CU_basic_set_mode(CU_BRM_VERBOSE);
  96.    CU_basic_run_tests();
  97.    CU_cleanup_registry();
  98.    return CU_get_error();
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement