Guide: Writing Your First Tests

Context

The FEAST codebase has zero test coverage. This guide walks you through setting up pytest and writing tests for the pure functions in household.py. These are the easiest functions to test because they have no external dependencies (no database, no network, no filesystem).

Beyond this project: Writing tests forces you to think about boundaries: what inputs break the function? When does behavior change? This same edge-case thinking is how you scope any open-ended problem: a feature has edge cases, a performance problem has boundary conditions, a user complaint has special cases.

Before You Start

Steps

1. Set up pytest

uv add --dev pytest pytest-asyncio mutmut

Create the test directory:

tests/
  __init__.py
  conftest.py
  test_household.py

2. Write tests for has_resources() (line 169)

This function returns False if the household lacks resources based on income and household size. Read it carefully:

def has_resources(self) -> bool:
    if self.income < 10000:
        return False
    if self.household_size >= 2 and self.income < 15000:
        return False
    if self.household_size >= 3 and self.income < 25000:
        return False
    return True

Test cases to write:

Edge cases:

Note: You’ll need to create a Household instance to test these methods. You may need to mock or simplify the constructor since it requires a Mesa model. Figure out the minimal setup needed.

3. Write tests for get_monthly_trip_count() (line 178)

Three paths:

4. Write tests for get_color() (line 57)

This converts an MFAI score to a hex color. Test:

Verify the hex output is a valid color string matching #rrggbb format.

5. Run the tests

uv run pytest tests/ -v

6. Run mutation testing

Mutation testing verifies your tests actually catch bugs. It makes small changes to the code (mutations) and checks if your tests fail. If a mutation survives (tests still pass), your tests have a gap.

uv run mutmut run --paths-to-mutate=food_access_model/abm/household.py
uv run mutmut results

If mutations survive, add test cases to kill them.

LLM Usage

Definition of Done

While You’re In There

Stretch Goals