Crea e testa lo strumento

Sviluppo software con Claude Code

Dani Zysman

AI Curriculum Manager @ DataCamp

Dal piano al codice

recraft: half: Sviluppatore alla scrivania che crea un tool CLI con terminale che mostra l'output di argparse

  • La nostra specifica dalla Lezione 4.1:
    • --artist — nome artista da cercare
    • --format — output JSON o CSV
    • --output — percorso file per i risultati
  • Claude genererà:
    • parsing degli argomenti con argparse
    • Query al database via MCP
    • Logica di scrittura file
Sviluppo software con Claude Code

Pattern degli argomenti CLI

Pattern Esempio Quando usarlo
Obbligatorio --artist "AC/DC" Deve essere fornito
Opzionale --format json Ha un default sensato
Flag --verbose Booleano on/off
Posizionale reporter.py data.csv Input principale
  • Buone pratiche:
    • Aggiungi descrizioni --help per ogni argomento
    • Usa default per ridurre gli input richiesti
    • Valida presto, fallisci subito con messaggi chiari
Sviluppo software con Claude Code

Costruire il Reporter

> Use @PLAN.md to build reporter.py
Fatto! Creato reporter.py seguendo PLAN.md

Funzioni principali:
- get_db_connection()     SQLite in sola lettura con convalida
- fetch_artist_stats()    4 query SQL per aggregati
- format_json/csv()       Formattazione output
- write_output()          Scrittura file
- parse_args() / main()   Orchestrazione CLI

Funzionalità:
- Ricerca artista case-insensitive
- Dettaglio per album con ricavi
- Tutti i messaggi d'errore come da specifica
- Flag --db per test injection
Sviluppo software con Claude Code

Il codice generato

import argparse, sqlite3, json, csv

def main():
    parser = argparse.ArgumentParser()
    ...
    args = parser.parse_args()

    conn = sqlite3.connect("chinook.db")
    # Query artist's albums and tracks...

    if args.format == "json":
        with open(args.output, "w") as f:
            json.dump(data, f, indent=2)
Sviluppo software con Claude Code

Testa lo strumento

flowchart: reporter.py, pytest, Verify Output

  • I test verificano:
    • Un artista valido restituisce dati
    • Il file di output viene creato
    • Il formato JSON/CSV è corretto
Sviluppo software con Claude Code

Testare strumenti CLI

  • Unit test
    • Testa singole funzioni
    • Mock delle chiamate al database
    • Veloci, isolati, ripetibili
  • Test di integrazione
    • Esegui il comando CLI reale
    • Usa un database reale
    • Più lenti ma più realistici
  • Cosa testare:
    • Happy path — artista valido, output corretto
    • Errori — artista non trovato, formato errato
    • Casi limite — nome vuoto, caratteri speciali
    • Gestione file — permessi di scrittura, sovrascrittura
Sviluppo software con Claude Code

Test in azione

> Use @PLAN.md to build test_reporter.py
Creato test_reporter.py: 31 test in 7 classi

Copertura:
- Connection     DB valido, file mancante, file non valido
- Stats          Artista noto/sconosciuto, case-insensitive, multi-genere
- JSON           Formato, indentazione, serializzazione round-trip
- CSV            Due sezioni, generi separati da pipe, conteggio righe
- File I/O       Scrittura, sovrascrittura, percorso non scrivibile
- CLI args       Argomenti obbligatori, validazione scelte
- Integration    Output JSON/CSV, artista/DB non trovato, cleanup

Usa un piccolo SQLite con seed: indipendente da chinook.db

Tutti i 31 test superati!
Sviluppo software con Claude Code

Esercitiamoci!

Sviluppo software con Claude Code

Preparing Video For Download...