Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Scheletro di una unit test con Cunit.
- *
- * Compilare incudendo la libreria CUnit nel progetto.
- * Con linea di comando, il comando da usare è il seguente.
- *
- * gcc cunit_skel.c -o test.exe -lcunit
- * l'output di test.exe è questo:
- *
- * CUnit : A Unit testing framework for C.
- * http://cunit.sourceforge.net/
- *
- * Suite: Suite_1
- * Test: test della somma ...passed
- * Test: test con numeri negativi ...FAILED
- * 1. cunit_skel.c:73 - 4 == somma(2,-1)
- *
- * Run Summary: Type Total Ran Passed Failed Inactive
- * suites 1 1 n/a 0 0
- * tests 2 2 1 1 0
- * asserts 3 3 2 1 n/a
- */
- #include <stdio.h>
- #include <string.h>
- #include "CUnit/Basic.h"
- /*
- * Funzione soggetta al test (molto banale)
- */
- int somma(int a, int b)
- { return a+b;}
- /* Funzione che inizializza la suite.
- * (in questo caso non fa fulla)
- */
- int init_suite1(void)
- {
- return 0;
- }
- /* Funzione che conclude la suite.
- * (in questo caso non fa nulla)
- */
- int clean_suite1(void)
- {
- return 0;
- }
- /* Test della funzione
- * Questo test passa.
- */
- void testSOMMA1(void)
- {
- CU_ASSERT(2 == somma(1,1));
- CU_ASSERT(4 == somma(3,1));
- }
- /* Altro test
- * Questo test fallisce (in modo da vedere l'output)
- */
- void testSOMMA2(void)
- {
- CU_ASSERT(4 == somma(2,-1));
- }
- /* Esempio di main() che prepara e lancia i test.
- * Se tutto va bene restituice CUE_SUCCESS on successful
- */
- int main()
- {
- CU_pSuite pSuite = NULL;
- /* Inizializza il registro dei testinitialize the CUnit test registry */
- if (CUE_SUCCESS != CU_initialize_registry())
- return CU_get_error();
- /* aggiungi una suite al registro */
- pSuite = CU_add_suite("Suite_1", init_suite1, clean_suite1);
- if (NULL == pSuite) {
- CU_cleanup_registry();
- return CU_get_error();
- }
- /* aggiungi i test alla suite */
- if ((NULL == CU_add_test(pSuite, "test della somma", testSOMMA1)) ||
- (NULL == CU_add_test(pSuite, "test con numeri negativi", testSOMMA2)))
- {
- CU_cleanup_registry();
- return CU_get_error();
- }
- /* Esegui tutti i test tramite l'interfaccia CUnit Basic */
- CU_basic_set_mode(CU_BRM_VERBOSE);
- CU_basic_run_tests();
- CU_cleanup_registry();
- return CU_get_error();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement