도구 빌드 및 테스트

Claude Code로 배우는 Software Development

Dani Zysman

AI Curriculum Manager @ DataCamp

계획에서 코드로

recraft: half: 책상에서 CLI 도구를 코딩하는 개발자, 터미널에 argparse 출력이 표시됨

  • 4.1 레슨의 사양:
    • --artist — 조회할 아티스트 이름
    • --format — JSON 또는 CSV 출력
    • --output — 결과 파일 경로
  • Claude가 생성할 항목:
    • argparse 인수 파싱
    • MCP를 통한 데이터베이스 쿼리
    • 파일 출력 로직
Claude Code로 배우는 Software Development

CLI 인수 패턴

패턴 예시 사용 시기
필수 --artist "AC/DC" 반드시 제공해야 함
선택 --format json 적절한 기본값 존재
플래그 --verbose 불리언 on/off
위치 인수 reporter.py data.csv 기본 입력값
  • 모범 사례:
    • 모든 인수에 --help 설명 추가
    • 기본값을 사용해 필수 입력 최소화
    • 조기에 검증하고, 명확한 메시지로 빠르게 실패 처리
Claude Code로 배우는 Software Development

리포터 빌드하기

> 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
Claude Code로 배우는 Software Development

생성된 코드

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)
Claude Code로 배우는 Software Development

도구 테스트하기

flowchart: reporter.py, pytest, 출력 검증

  • 테스트로 확인할 내용:
    • 유효한 아티스트 입력 시 데이터 반환
    • 출력 파일 생성 여부
    • JSON/CSV 형식의 정확성
Claude Code로 배우는 Software Development

CLI 도구 테스트하기

  • 단위 테스트
    • 개별 함수 테스트
    • 데이터베이스 호출 모킹
    • 빠르고 독립적이며 반복 가능
  • 통합 테스트
    • 실제 CLI 명령 실행
    • 실제 데이터베이스 사용
    • 느리지만 더 현실적
  • 테스트 항목:
    • 정상 경로 — 유효한 아티스트, 올바른 출력
    • 오류 사례 — 아티스트를 찾을 수 없음, 잘못된 형식
    • 경계 사례 — 빈 이름, 특수 문자
    • 파일 처리 — 쓰기 권한, 덮어쓰기
Claude Code로 배우는 Software Development

실전 테스트

> 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!
Claude Code로 배우는 Software Development

연습해 봅시다!

Claude Code로 배우는 Software Development

Preparing Video For Download...