Build and Test the Tool

Software Development with Claude Code

Dani Zysman

AI Curriculum Manager @ DataCamp

From Plan to Code

recraft: half: Developer at desk coding a CLI tool with terminal showing argparse output

  • Our specification from Lesson 4.1:
    • --artist — artist name to query
    • --format — JSON or CSV output
    • --output — file path for results
  • Claude will generate:
    • argparse argument parsing
    • Database query via MCP
    • File output logic
Software Development with Claude Code

CLI Argument Patterns

Pattern Example When to Use
Required --artist "AC/DC" Must be provided
Optional --format json Has sensible default
Flag --verbose Boolean on/off
Positional reporter.py data.csv Primary input
  • Best practices:
    • Add --help descriptions for every argument
    • Use defaults to reduce required inputs
    • Validate early, fail fast with clear messages
Software Development with Claude Code

Building the Reporter

> Use @PLAN.md to build reporter.py
Done! Created reporter.py following PLAN.md

Core functions:
- get_db_connection()     Read-only SQLite with validation
- fetch_artist_stats()    4 SQL queries for aggregates
- format_json/csv()       Output formatting
- write_output()          File writing
- parse_args() / main()   CLI orchestration

Features:
- Case-insensitive artist lookup
- Per-album breakdown with revenue
- All error messages as specified
- --db flag for test injection
Software Development with Claude Code

The Generated Code

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)
Software Development with Claude Code

Test the Tool

flowchart: reporter.py, pytest, Verify Output

  • Tests verify:
    • Valid artist returns data
    • Output file is created
    • JSON/CSV format is correct
Software Development with Claude Code

Testing CLI Tools

  • Unit Tests
    • Test individual functions
    • Mock database calls
    • Fast, isolated, repeatable
  • Integration Tests
    • Run the actual CLI command
    • Use real database
    • Slower but more realistic
  • What to test:
    • Happy path — valid artist, correct output
    • Error cases — artist not found, bad format
    • Edge cases — empty name, special characters
    • File handling — write permissions, overwrite
Software Development with Claude Code

Testing in Action

> Use @PLAN.md to build test_reporter.py
Created test_reporter.py: 31 tests across 7 classes

Coverage:
- Connection     Valid DB, missing file, invalid file
- Stats          Known/unknown artist, case-insensitive, multi-genre
- JSON           Format, indentation, round-trip serialization
- CSV            Two sections, pipe-delimited genres, row counts
- File I/O       Write, overwrite, unwritable path
- CLI args       Required args, choices validation
- Integration    JSON/CSV output, artist/DB not found, cleanup

Uses minimal seeded SQLite DB: independent of chinook.db

All 31 tests passing!
Software Development with Claude Code

Let's Practice!

Software Development with Claude Code

Preparing Video For Download...