AI Hints
Home›LangGraph Tutorial›Testing and Debugging
LangGraph · Lesson 103 of 120

Testing Nodes

Plain functions.

⏱ 2 min read▶ Video at 1:51:21🎓 Free
Watch this lesson in the LangGraph courseWatch on YouTube ↗Full LangGraph course

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.

Testing Nodes
From the video: Testing Nodes
  • 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.

test_nodes.pypython
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")
Output
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.

keyvalue
pytestrun on every change
Pure nodesexact asserts
Model nodesfake model
End to enda 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
Next up: Testing Graphs — paths and outputs