Skip to content

Commit 1c853d9

Browse files
committed
added 11 testunit part2
1 parent aa1b013 commit 1c853d9

2 files changed

Lines changed: 87 additions & 67 deletions

File tree

11_testunit.ipynb

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"source": [
88
"# 🧪 Test Unitari con `unittest` 🧪\n",
99
"\n",
10-
"---\n",
10+
"---",
11+
"\n",
1112
"## 1. Introduzione ai Test Unitari: Principi e Pratica\n",
1213
"\n",
1314
"Il **testing del software** è un processo cruciale che verifica che un programma funzioni come previsto. Non si tratta solo di trovare bug, ma di garantire la qualità, l'affidabilità e la manutenibilità del codice nel tempo. Tra le varie strategie di testing, il **testing unitario** è la più granulare e viene eseguita dagli sviluppatori stessi.\n",
@@ -16,7 +17,7 @@
1617
"\n",
1718
"Un **test unitario** è una porzione di codice che verifica il comportamento di una singola e isolata \"unità\" del tuo programma. Un'unità può essere una funzione, un metodo di una classe o una singola classe. L'obiettivo è isolare l'unità e assicurarsi che restituisca l'output corretto per un determinato input, in un ambiente controllato.\n",
1819
"\n",
19-
"Pensa al testing unitario come all'assemblaggio di un'automobile . Prima di testare l'intera vettura, verifichi che ogni singolo componente, come il motore, i freni o il volante, funzioni perfettamente per conto suo. Se un componente è difettoso, lo individui e lo ripari subito, senza dover smontare l'intera macchina in un secondo momento.\n",
20+
"Pensa al testing unitario come all'assemblaggio di un'automobile. Prima di testare l'intera vettura, verifichi che ogni singolo componente, come il motore, i freni o il volante, funzioni perfettamente per conto suo. Se un componente è difettoso, lo individui e lo ripari subito, senza dover smontare l'intera macchina in un secondo momento.\n",
2021
"\n",
2122
"### I Principi del Testing Unitario\n",
2223
"\n",
@@ -40,7 +41,8 @@
4041
"\n",
4142
"Con `unittest`, si crea una classe di test, si definiscono metodi di test (`test_...`), si usano i metodi di asserzione (`assertEqual`, `assertTrue`, ecc.) per verificare i risultati e si esegue tutto tramite `unittest.main()`.\n",
4243
"\n",
43-
"---\n",
44+
"---",
45+
"\n",
4446
"## 2. Metodologia e Pratica di `unittest`\n",
4547
"\n",
4648
"Un test unitario con `unittest` segue una struttura ben definita:\n",
@@ -97,7 +99,8 @@
9799
"id": "9dce0a80",
98100
"metadata": {},
99101
"source": [
100-
"---\n",
102+
"---",
103+
"\n",
101104
"## 3. Esercizi 📝\n",
102105
"\n",
103106
"Scrivi i test unitari per le seguenti funzioni e classi. Crea un file di test separato per ogni esercizio."
@@ -134,7 +137,8 @@
134137
"id": "7c93abf7",
135138
"metadata": {},
136139
"source": [
137-
"---\n",
140+
"---",
141+
"\n",
138142
"### Esercizio 2: Classe `Utente`\n",
139143
"\n",
140144
"Crea un file `utenti.py` con la seguente classe e scrivi i test per i suoi metodi in `test_utenti.py`.\n",
@@ -169,14 +173,28 @@
169173
"id": "be724524",
170174
"metadata": {},
171175
"source": [
172-
"---\n",
173-
"## 4. Soluzioni ✅\n",
176+
"---",
174177
"\n",
178+
"## 4. Soluzioni ✅"
179+
]
180+
},
181+
{
182+
"cell_type": "markdown",
183+
"id": "4955b271",
184+
"metadata": {},
185+
"source": [
175186
"### Soluzione Esercizio 1: Test per `calcola_media`\n",
176187
"\n",
177-
"Copia questo codice in un file `test_statistiche.py`.\n",
178-
"\n",
179-
"```python\n",
188+
"Copia questo codice in un file `test_statistiche.py`."
189+
]
190+
},
191+
{
192+
"cell_type": "code",
193+
"execution_count": null,
194+
"id": "d0e0ac8d",
195+
"metadata": {},
196+
"outputs": [],
197+
"source": [
180198
"# test_statistiche.py\n",
181199
"import unittest\n",
182200
"from statistiche import calcola_media\n",
@@ -197,8 +215,7 @@
197215
" self.assertEqual(calcola_media([10]), 10.0)\n",
198216
"\n",
199217
"if __name__ == '__main__':\n",
200-
" unittest.main()\n",
201-
"```\n"
218+
" unittest.main()"
202219
]
203220
},
204221
{
@@ -208,9 +225,16 @@
208225
"source": [
209226
"### Soluzione Esercizio 2: Test per la classe `Utente`\n",
210227
"\n",
211-
"Copia questo codice in un file `test_utenti.py`.\n",
212-
"\n",
213-
"```python\n",
228+
"Copia questo codice in un file `test_utenti.py`."
229+
]
230+
},
231+
{
232+
"cell_type": "code",
233+
"execution_count": null,
234+
"id": "89396263",
235+
"metadata": {},
236+
"outputs": [],
237+
"source": [
214238
"# test_utenti.py\n",
215239
"import unittest\n",
216240
"from utenti import Utente\n",
@@ -244,8 +268,7 @@
244268
" Utente(\"Nome\", \"\")\n",
245269
"\n",
246270
"if __name__ == '__main__':\n",
247-
" unittest.main()\n",
248-
"```\n"
271+
" unittest.main()\n"
249272
]
250273
}
251274
],
@@ -270,4 +293,4 @@
270293
},
271294
"nbformat": 4,
272295
"nbformat_minor": 5
273-
}
296+
}

12_progetto.ipynb

Lines changed: 46 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
"cells": [
33
{
44
"cell_type": "markdown",
5-
"id": "86f6f880",
5+
"id": "c32b393f",
66
"metadata": {},
77
"source": [
8-
"# 🚀 Progetto finale: Gestione di un inventario avanzato\n",
8+
"# 🚀 11 - Progetto finale: Gestione di un inventario avanzato\n",
99
"\n",
1010
"---\n",
1111
"## Introduzione al progetto\n",
@@ -26,7 +26,7 @@
2626
},
2727
{
2828
"cell_type": "markdown",
29-
"id": "129f6294",
29+
"id": "0a7aa72a",
3030
"metadata": {},
3131
"source": [
3232
"---\n",
@@ -51,7 +51,7 @@
5151
},
5252
{
5353
"cell_type": "markdown",
54-
"id": "beb7e015",
54+
"id": "87f28809",
5555
"metadata": {},
5656
"source": [
5757
"---\n",
@@ -90,7 +90,7 @@
9090
},
9191
{
9292
"cell_type": "markdown",
93-
"id": "d4995c19",
93+
"id": "cd7ffc7e",
9494
"metadata": {},
9595
"source": [
9696
"---\n",
@@ -101,7 +101,7 @@
101101
},
102102
{
103103
"cell_type": "markdown",
104-
"id": "8e547474",
104+
"id": "43881c13",
105105
"metadata": {},
106106
"source": [
107107
"### `models.py`\n",
@@ -112,7 +112,7 @@
112112
{
113113
"cell_type": "code",
114114
"execution_count": null,
115-
"id": "9d8fe588",
115+
"id": "2e788715",
116116
"metadata": {},
117117
"outputs": [],
118118
"source": [
@@ -193,7 +193,7 @@
193193
},
194194
{
195195
"cell_type": "markdown",
196-
"id": "3525552b",
196+
"id": "d1e197e5",
197197
"metadata": {},
198198
"source": [
199199
"### `inventory_manager.py`\n",
@@ -204,7 +204,7 @@
204204
{
205205
"cell_type": "code",
206206
"execution_count": null,
207-
"id": "4f1cd8e5",
207+
"id": "e4285097",
208208
"metadata": {},
209209
"outputs": [],
210210
"source": [
@@ -287,7 +287,7 @@
287287
},
288288
{
289289
"cell_type": "markdown",
290-
"id": "89d3b2a2",
290+
"id": "73031b16",
291291
"metadata": {},
292292
"source": [
293293
"### `main.py`\n",
@@ -298,7 +298,7 @@
298298
{
299299
"cell_type": "code",
300300
"execution_count": null,
301-
"id": "a011eed3",
301+
"id": "72655d91",
302302
"metadata": {},
303303
"outputs": [],
304304
"source": [
@@ -372,7 +372,7 @@
372372
},
373373
{
374374
"cell_type": "markdown",
375-
"id": "364078a1",
375+
"id": "eca4ed35",
376376
"metadata": {},
377377
"source": [
378378
"---\n",
@@ -419,7 +419,7 @@
419419
},
420420
{
421421
"cell_type": "markdown",
422-
"id": "4afe141f",
422+
"id": "917ce1ad",
423423
"metadata": {},
424424
"source": [
425425
"---\n",
@@ -465,81 +465,78 @@
465465
{
466466
"cell_type": "code",
467467
"execution_count": null,
468-
"id": "59985144",
468+
"id": "d7ecbb8e",
469469
"metadata": {},
470470
"outputs": [],
471471
"source": [
472-
"# tests/test_inventory.py\n",
473-
"\n",
472+
"# test_inventory.py\n",
474473
"import unittest\n",
475474
"import os\n",
476-
"import json\n",
477475
"from inventory_manager import GestoreInventario\n",
478476
"from models import Prodotto, ProdottoAlimentare, ProdottoElettronico\n",
479477
"\n",
480478
"class TestGestoreInventario(unittest.TestCase):\n",
481479
"\n",
482480
" def setUp(self):\n",
483481
" # Viene eseguito prima di ogni test. \n",
484-
" # Rimuove il file inventario per garantire un ambiente pulito.\n",
482+
" # Assicura un ambiente di test pulito eliminando il file di salvataggio.\n",
485483
" self.gestore = GestoreInventario()\n",
486484
" if os.path.exists(self.gestore.NOME_FILE):\n",
487485
" os.remove(self.gestore.NOME_FILE)\n",
488-
" self.gestore = GestoreInventario() # Crea una nuova istanza pulita\n",
486+
" # Inizializza un nuovo gestore che inizierà con un inventario vuoto\n",
487+
" self.gestore = GestoreInventario()\n",
488+
" self.prodotto_test = Prodotto(\"Test Prodotto\", 10.0, 5)\n",
489489
"\n",
490490
" def tearDown(self):\n",
491491
" # Viene eseguito dopo ogni test. \n",
492-
" # Rimuove il file inventario per pulire l'ambiente.\n",
492+
" # Pulisce l'ambiente eliminando il file di salvataggio creato.\n",
493493
" if os.path.exists(self.gestore.NOME_FILE):\n",
494494
" os.remove(self.gestore.NOME_FILE)\n",
495495
"\n",
496-
" def test_aggiungi_prodotto(self):\n",
497-
" prodotto1 = Prodotto(\"Pane\", 2.50, 10)\n",
498-
" self.assertTrue(self.gestore.aggiungi_prodotto(prodotto1))\n",
496+
" def test_aggiungi_prodotto_successo(self):\n",
497+
" self.assertTrue(self.gestore.aggiungi_prodotto(self.prodotto_test))\n",
499498
" self.assertEqual(len(self.gestore.inventario), 1)\n",
500-
" self.assertIn(\"Pane\", self.gestore.inventario)\n",
499+
" self.assertIn(\"Test Prodotto\", self.gestore.inventario)\n",
501500
"\n",
502501
" def test_aggiungi_prodotto_esistente(self):\n",
503-
" prodotto1 = Prodotto(\"Pane\", 2.50, 10)\n",
504-
" self.gestore.aggiungi_prodotto(prodotto1)\n",
505-
" prodotto2 = Prodotto(\"Pane\", 3.00, 5) # Stesso nome\n",
506-
" self.assertFalse(self.gestore.aggiungi_prodotto(prodotto2))\n",
507-
" self.assertEqual(self.gestore.inventario['Pane'].quantita, 10)\n",
502+
" self.gestore.aggiungi_prodotto(self.prodotto_test)\n",
503+
" prodotto_duplicato = Prodotto(\"Test Prodotto\", 12.0, 3)\n",
504+
" self.assertFalse(self.gestore.aggiungi_prodotto(prodotto_duplicato))\n",
505+
" self.assertEqual(self.gestore.inventario['Test Prodotto'].quantita, 5)\n",
508506
"\n",
509507
" def test_vendi_prodotto_successo(self):\n",
510-
" prodotto = Prodotto(\"Latte\", 1.20, 20)\n",
511-
" self.gestore.aggiungi_prodotto(prodotto)\n",
512-
" self.assertTrue(self.gestore.vendi_prodotto(\"Latte\", 5))\n",
513-
" self.assertEqual(self.gestore.inventario['Latte'].quantita, 15)\n",
508+
" self.gestore.aggiungi_prodotto(self.prodotto_test)\n",
509+
" self.assertTrue(self.gestore.vendi_prodotto(\"Test Prodotto\", 3))\n",
510+
" self.assertEqual(self.gestore.inventario['Test Prodotto'].quantita, 2)\n",
514511
"\n",
515512
" def test_vendi_prodotto_quantita_insufficiente(self):\n",
516-
" prodotto = Prodotto(\"Latte\", 1.20, 20)\n",
517-
" self.gestore.aggiungi_prodotto(prodotto)\n",
518-
" self.assertFalse(self.gestore.vendi_prodotto(\"Latte\", 25))\n",
519-
" self.assertEqual(self.gestore.inventario['Latte'].quantita, 20)\n",
513+
" self.gestore.aggiungi_prodotto(self.prodotto_test)\n",
514+
" self.assertFalse(self.gestore.vendi_prodotto(\"Test Prodotto\", 10))\n",
515+
" self.assertEqual(self.gestore.inventario['Test Prodotto'].quantita, 5)\n",
520516
"\n",
521517
" def test_vendi_prodotto_non_esistente(self):\n",
522-
" self.assertFalse(self.gestore.vendi_prodotto(\"Acqua\", 1))\n",
518+
" self.assertFalse(self.gestore.vendi_prodotto(\"Prodotto Inesistente\", 1))\n",
523519
"\n",
524-
" def test_salva_carica_inventario(self):\n",
525-
" prodotto_ali = ProdottoAlimentare(\"Pasta\", 1.0, 50, \"2026-01-01\")\n",
526-
" prodotto_ele = ProdottoElettronico(\"TV\", 800.0, 3, 3)\n",
520+
" def test_salva_e_carica_inventario(self):\n",
521+
" prodotto_ali = ProdottoAlimentare(\"Latte\", 1.50, 10, \"30-12-2025\")\n",
522+
" prodotto_ele = ProdottoElettronico(\"Laptop\", 1200.0, 2, 2)\n",
527523
" self.gestore.aggiungi_prodotto(prodotto_ali)\n",
528524
" self.gestore.aggiungi_prodotto(prodotto_ele)\n",
529525
" self.gestore.salva_inventario()\n",
530526
"\n",
531-
" # Crea un nuovo gestore per simulare il caricamento da zero\n",
527+
" # Crea una nuova istanza di GestoreInventario per simulare un nuovo avvio del programma\n",
532528
" nuovo_gestore = GestoreInventario()\n",
533529
"\n",
534-
" # Verifica che i prodotti siano stati caricati correttamente\n",
535-
" self.assertIn(\"Pasta\", nuovo_gestore.inventario)\n",
536-
" self.assertIsInstance(nuovo_gestore.inventario['Pasta'], ProdottoAlimentare)\n",
537-
" self.assertEqual(nuovo_gestore.inventario['Pasta'].quantita, 50)\n",
538-
" self.assertEqual(nuovo_gestore.inventario['TV'].get_prezzo(), 800.0)\n",
530+
" # Verifica che l'inventario sia stato caricato correttamente\n",
531+
" self.assertIn(\"Latte\", nuovo_gestore.inventario)\n",
532+
" self.assertIn(\"Laptop\", nuovo_gestore.inventario)\n",
533+
" self.assertEqual(nuovo_gestore.inventario['Latte'].quantita, 10)\n",
534+
" self.assertEqual(nuovo_gestore.inventario['Laptop'].get_prezzo(), 1200.0)\n",
535+
" self.assertIsInstance(nuovo_gestore.inventario['Latte'], ProdottoAlimentare)\n",
536+
" self.assertIsInstance(nuovo_gestore.inventario['Laptop'], ProdottoElettronico)\n",
539537
"\n",
540538
"if __name__ == '__main__':\n",
541-
" unittest.main()\n",
542-
"```"
539+
" unittest.main()\n"
543540
]
544541
}
545542
],

0 commit comments

Comments
 (0)