Read Anthropic’s case study about Graphite Reviewer

Writing effective test cases for code review

Sara Verdi
Sara Verdi
Graphite software engineer
Try Graphite

Test cases are essential in software development, as they ensure that your code functions correctly and meets its requirements. During a code review, effective test cases help reviewers verify that edge cases, integration points, and functionality have been addressed. This guide explores best practices for writing test cases during code reviews and how to use tools like Graphite's PR Inbox to manage reviews effectively.

Define the purpose of the test cases:

  • Validate core functionality.
  • Handle edge cases.
  • Test integration with other components.
  • Ensure performance and scalability.

Example: For a function calculating discounts:

Terminal
# Testing core functionality
assert calculate_discount(100, 0.1) == 90 # Standard case
# Testing edge cases
assert calculate_discount(0, 0.1) == 0 # Zero cost
assert calculate_discount(100, 0) == 100 # Zero discount
assert calculate_discount(100, 1.1) == 0 # Discount exceeds 100%
  • Use descriptive test case names to convey the test's purpose.
  • Group related test cases for better organization.

Example:

Terminal
def test_discount_calculation():
# Core functionality tests
assert calculate_discount(100, 0.1) == 90
def test_edge_cases():
assert calculate_discount(0, 0.1) == 0
assert calculate_discount(100, 1.1) == 0

Automated tests allow for quick validation of code during reviews:

  • Leverage frameworks like Pytest, Jest, or JUnit.
  • Include a test suite in the pull request to enable automated checks.

Command-line example:

Terminal
pytest tests/test_discount.py

Results:

Terminal
5 passed in 0.04 seconds

When submitting a pull request (PR), ensure your test cases are:

  • Linked to the code they validate.
  • Clearly explained in the PR description.

Example PR description:

Terminal
### What does this PR do?
- Implements a discount calculation function.
- Adds test cases for core functionality and edge cases.
### Testing steps
1. Run `pytest tests/test_discount.py`.
2. Verify all test cases pass.

Ensure that:

  • All critical paths and edge cases are tested.
  • Test cases align with the acceptance criteria.

Review test cases for:

  • Clarity and simplicity.
  • Coverage of potential failure scenarios.

Use continuous integration (CI) pipelines or local environments to validate the tests. Example pipeline output:

Terminal
✅ All tests passed. Coverage: 95%.

Graphite's PR Inbox offers a structured way to manage test case reviews:

  1. Organize by priority: Use sections like "Needs your review" to focus on PRs with test cases awaiting review.
  2. Search functionality: Quickly find PRs with test-related changes using keywords like "tests" or "validation".
  3. Custom filters: Create a filter for PRs that modify test files, e.g., path:test/*.

Example workflow:

  • Navigate to "Needs your review" in Graphite's PR Inbox.
  • Use the search bar (cmd + k) to locate PRs containing test cases.
  • Review and leave comments on tests directly in the interface.

Graphite's ability to create and share custom sections helps gurantee that team members can collaboratively review test cases efficiently.

  • Encourage feedback: Use Graphite's PR Inbox to easily communicate suggestions and improvements.
  • Highlight gaps: Comment on missing test scenarios or unhandled edge cases.
  • Track changes: Use the "Returned to you" section to monitor updates to PRs after requesting changes.

By integrating these practices and leveraging tools like Graphite, teams can ensure high-quality test cases and streamlined code review processes.

Built for the world's fastest engineering teams, now available for everyone