正在学习

14.1 Overview: Why a Cookbook Matters

Bug: The loop reassigns total instead of accumulating.

def calculate_sum(numbers):

total = 0

for num in numbers:

total += num

return total

print(calculate_sum([1, 2, 3])) # Output: 6

Explanation: Claude not only identifies the exact line of logic error but explains the reason concisely — a common debugging prompt pattern that helps build intuition for code review automation.

Clarification Table: Testing and Debugging Prompt Strategies

Goal Prompt Structure Result
Unit Testing “Write complete runnable tests using [framework]” Produces valid test file
Integration Testing “Include API calls, setup, and teardown steps” Adds realistic workflow coverage
Error Diagnosis “Analyze this traceback and explain the cause” Returns cause + fixed code
Bug Reproduction “Simulate this issue and show how to replicate it” Provides reproducible steps
Fix Validation “Verify the corrected code passes all tests” Adds confirmation message
Performance Issues “Identify slow parts and suggest optimizations” Gives measurable code refactors
CI Integration “Generate GitHub Actions workflow for tests” Produces automation-ready pipeline

Claude Code is not just a generator of code — it’s an intelligent partner in maintaining code quality. Well-structured testing and debugging prompts empower Claude to reason like a QA engineer, catching logic errors and ensuring that every output is reliable and reproducible.

The key takeaway: prompt specificity equals reliability. By specifying frameworks, error context, and coverage criteria, you can make Claude’s testing output indistinguishable from that of a seasoned engineer.

In the next section, we’ll expand from local validation to CI/CD and DevOps integration prompts, showing how Claude can automatically generate pipelines, test runners, and deployment configurations for complete development automation.

练习题

What is the primary bug in the given calculate_sum function?

A. The function returns None instead of the sum
B. The loop reassigns total instead of accumulating
C. The function does not handle empty lists
D. The function uses incorrect variable names

The corrected version of calculate_sum should use total = total + num instead of total += num for clarity.

The correct line to replace total = num in the loop is ___.

Explain why the original calculate_sum function fails to compute the correct sum.

Which of the following are valid ways to fix the calculate_sum function? (Select all that apply)

A. Replace total = num with total += num
B. Initialize total to 1 instead of 0
C. Replace total = num with total = total + num
D. Add a check for empty lists before the loop
E. Use a while loop instead of a for loop

The calculate_sum function will work correctly if the loop is replaced with a recursive implementation that accumulates the sum.

Which prompt structure is used for unit testing in the clarification table?

A. “Include API calls, setup, and teardown steps”
B. “Write complete runnable tests using [framework]”
C. “Analyze this traceback and explain the cause”
D. “Simulate this issue and show how to replicate it”

The result of the unit testing prompt is a ___.

Why is specifying a testing framework important in a unit testing prompt?

Which prompt structure is used for integration testing in the clarification table?

A. “Write complete runnable tests using [framework]”
B. “Include API calls, setup, and teardown steps”
C. “Analyze this traceback and explain the cause”
D. “Verify the corrected code passes all tests”

Integration testing prompts should avoid including API calls to keep the tests simple.

What is the purpose of setup and teardown steps in integration testing?

Which prompt structure is used for error diagnosis in the clarification table?

A. “Write complete runnable tests using [framework]”
B. “Include API calls, setup, and teardown steps”
C. “Analyze this traceback and explain the cause”
D. “Simulate this issue and show how to replicate it”

The result of an error diagnosis prompt includes the ___ and the fixed code.

Why is providing the fixed code important in an error diagnosis prompt?

Which prompt structure is used for bug reproduction in the clarification table?

A. “Write complete runnable tests using [framework]”
B. “Include API calls, setup, and teardown steps”
C. “Analyze this traceback and explain the cause”
D. “Simulate this issue and show how to replicate it”

Bug reproduction prompts should include only high-level descriptions of the issue, not specific steps.

Why are reproducible steps important in bug reproduction prompts?

Which prompt structure is used for fix validation in the clarification table?

A. “Write complete runnable tests using [framework]”
B. “Include API calls, setup, and teardown steps”
C. “Verify the corrected code passes all tests”
D. “Generate GitHub Actions workflow for tests”

The result of a fix validation prompt includes a ___ message to confirm the fix works.

Why is running tests important in fix validation prompts?

Which prompt structure is used for performance issues in the clarification table?

A. “Write complete runnable tests using [framework]”
B. “Include API calls, setup, and teardown steps”
C. “Identify slow parts and suggest optimizations”
D. “Generate GitHub Actions workflow for tests”

Performance issues prompts should focus only on theoretical optimizations without measuring their impact.

Why are measurable refactors important in performance issues prompts?

Which prompt structure is used for CI integration in the clarification table?

A. “Write complete runnable tests using [framework]”
B. “Include API calls, setup, and teardown steps”
C. “Identify slow parts and suggest optimizations”
D. “Generate GitHub Actions workflow for tests”

The result of a CI integration prompt is an ___ pipeline for tests.

Why is automation important in CI integration prompts?

Which knowledge points are tested by the question about fixing the calculate_sum function? (Select all that apply)

A. Bug: The loop reassigns total instead of accumulating - Example Code
B. Clarification Table: Testing and Debugging Prompt Strategies - Unit Testing
C. Clarification Table: Testing and Debugging Prompt Strategies - Error Diagnosis
D. Clarification Table: Testing and Debugging Prompt Strategies - Fix Validation
E. Key Takeaway: Prompt Specificity Equals Reliability

How does prompt specificity improve reliability in testing and debugging prompts? Give an example.

Combining knowledge points from testing strategies and bug fixing improves the ability to integrate concepts and solve real-world problems.

When debugging the following code snippet, which testing strategy from the clarification table would be most appropriate to identify the root cause of incorrect output?

def calculate_sum(numbers):
    total = 0
    for num in numbers:
        total = num  # Bug: reassigns instead of accumulating
    return total

A. Unit Testing
B. Integration Testing
C. Error Diagnosis
D. Performance Issues

Which prompt strategies from the clarification table would help ensure the corrected version of calculate_sum() produces reliable output? (Select all that apply)

A. Unit Testing
B. Fix Validation
C. CI Integration
D. Performance Issues

The key takeaway 'Prompt specificity equals reliability' implies that specifying error context in a debugging prompt will always produce a fixed version of the code in a single iteration.

To reproduce the bug in calculate_sum(), a ___ strategy would involve creating a test case like calculate_sum([1, 2, 3]) and comparing the output to the expected result of 6.

登录后解锁笔记、知识点解析、AI 问答

立即登录