Chapter 7: Static Testing
What is Static Testing?
Testing without running the code - through reviews and analysis.
Types of Reviews
1. Informal Review
Casual discussion of code or documents
2. Walkthrough
Author leads team through their work
3. Technical Review
Experts evaluate technical quality
4. Inspection
Formal, structured review process
Code Review Process
# Code to review
def calculate_total(items):
total = 0
for item in items:
total = total + item.price * item.quantity
return total
# Review checklist:
# ā Function name is clear
# ā Logic is correct
# ā No input validation (what if items is None?)
# ā No error handling
# ā Missing type hints
Improved Version:
from typing import List
def calculate_total(items: List[Item]) -> float:
"""Calculate total price for a list of items.
Args:
items: List of Item objects with price and quantity
Returns:
Total price as float
Raises:
ValueError: If items list is empty
"""
if not items:
raise ValueError("Items list cannot be empty")
total = 0.0
for item in items:
if item.price < 0 or item.quantity < 0:
raise ValueError("Price and quantity must be non-negative")
total += item.price * item.quantity
return total
Static Analysis Tools
Python: pylint, flake8, mypy
# Run static analysis
pylint src/
flake8 src/
mypy src/
Review Metrics
def calculate_review_metrics(review_data):
return {
'inspection_rate': lines_reviewed / review_time_hours,
'defect_detection_rate': defects_found / lines_reviewed * 1000,
'review_efficiency': major_defects_found / total_review_effort
}
Benefits of Static Testing
ā Find defects early (30-70% of all defects!) ā No code execution needed ā Improve code quality ā Knowledge sharing
Key Takeaways
ā Reviews find defects before testing ā Static analysis automates quality checks ā Formal inspections are most effective ā Combine with dynamic testing
Complete Lab 7 to practice code reviews!