Testing Nodes
Plain functions.
Nodes are plain functions, which makes them the easiest part of a graph to test. Give a node a state, and check the update it returns. No graph, no model, no network.

- Nodes are functions
- State in, update out
- No graph needed
Testing the order nodes
We import the three order nodes from our helper graph. Each test calls a node with a small state, and asserts the update is exactly what we expect. Run it. All the asserts pass, so we print a single line, confirming the nodes work.
from order_graph import clean, price, receipt
def test_clean():
assert clean({"title": " dune "}) == {"title": "Dune"}
def test_price():
assert price({"title": "Dune", "quantity": 2}) == {"total": 19.98}
def test_receipt():
out = receipt({"title": "Dune", "quantity": 2, "total": 19.98})
assert out["receipt"] == "2 x Dune = £19.98"
for test in [test_clean, test_price, test_receipt]:
test()
print("all node tests passed")all node tests passed
Also uses order_graph.py
from typing import TypedDict
from langgraph.checkpoint.memory import InMemorySaver
from langgraph.graph import StateGraph, START, END
PRICES = {"Dune": 9.99, "Circe": 10.99}
class Order(TypedDict):
title: str
quantity: int
total: float
receipt: str
def clean(s):
return {"title": s["title"].strip().title()}
def price(s):
return {"total": round(PRICES[s["title"]] * s["quantity"], 2)}
def receipt(s):
return {"receipt": f"{s['quantity']} x {s['title']} = £{s['total']}"}
b = StateGraph(Order)
b.add_sequence([clean, price, receipt])
b.add_edge(START, "clean"); b.add_edge("receipt", END)
graph = b.compile(checkpointer=InMemorySaver())Testing nodes well
Write tests like these with pytest, and run them on every change. For nodes that call a model, test the code around the model, and swap in a fake model that returns a fixed reply. Keep model calls for a few end to end tests.
| key | value |
|---|---|
| pytest | run on every change |
| Pure nodes | exact asserts |
| Model nodes | fake model |
| End to end | a few, graded |
Summary
Test nodes by calling them with a small state, and asserting their update. No graph required. Next, testing whole graphs.
Key takeaways- Call the node
- Assert the update
- Fake models