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!