Every run so far has called the model, which costs money and gives a different memo each time. So this chapter answers: how do you test the desk without a model?
In a test, a stand-in takes the agent's place and hands the desk a memo you chose:
Drawing diagram…
InputToolResultA stand-in in the agent's place
Stand-in: a small function that answers in place of the agent, with an answer the test picks.
monkeypatch: pytest's tool for swapping a name for a stand-in during one test.
If you closed the terminal, go back into the folder and switch the environment on, then install pytest:
Terminal
1$ cd document-desk
2$ source env/bin/activate
3(env)$ pip install pytest
A test needs names it can import, so the agent moves into its own file. Let's save this as agents.py:
agents.py
1# agents.py2from crewai import Agent
345defwrite_memo(text):6 writer = Agent(7 role="Contract analyst",8 goal="Tell a manager what could cost them money",9 backstory="You read contracts for a small company.",10)11 answer = writer.kickoff(12"Write a memo of at most 60 words about the risks in this contract. "13"Give the last day to send notice that stops the renewal, as a full date.\n\n"+ text
14)15return answer.raw
The desk calls agents.write_memo, and checks the memo the way chapter 10 did. Let's save this as desk.py:
desk.py
1# desk.py2from datetime import date, timedelta
34from crewai.flow.flow import Flow, listen, start
5from dotenv import load_dotenv
6from pydantic import BaseModel, ConfigDict
78import agents
910load_dotenv()111213classDeskState(BaseModel):14 model_config = ConfigDict(validate_assignment=True)1516 text:str=""17 memo:str=""181920classDesk(Flow[DeskState]):21 suppress_flow_events =True2223@start()24defread_document(self):25withopen("inbox/acme-saas-agreement.md")asfile:26 self.state.text =file.read()2728@listen(read_document)29defreview_contract(self):30 self.state.memo = agents.write_memo(self.state.text)3132@listen(review_contract)33defcheck_memo(self):34 deadline = date(2027,2,28)- timedelta(days=60)35 written =f"{deadline:%B}{deadline.day}, {deadline.year}"36 forms =[written,f"{deadline.day}{deadline:%B %Y}",str(deadline)]37ifany(form in self.state.memo for form in forms):38return"the memo has the deadline"39returnf"Notice is due by {written}, and the memo doesn't say so."
The test runs the desk, and expects the finding. Let's save this as test_desk.py:
test_desk.py
1# test_desk.py2from desk import Desk
345deftest_a_wrong_deadline_is_found():6assert Desk().kickoff()=="Notice is due by December 30, 2026, and the memo doesn't say so."
Terminal
1(env)$ python -m pytest -q
Output
. [100%]...1 passed, 4 warnings in 5.63s
Each dot is a test that passed, and ... stands for warnings CrewAI prints about its own deprecated settings. It passed, but only because the agent wrote a wrong date. The test can't choose the memo, so it checks the agent as much as your code:
Drawing diagram…
InputAgentResultThe test depends on the model
On a day the agent gets December 30 right, this test fails, though your code is fine. And every run calls the model with your key.
monkeypatch.setattr(agents, "write_memo", ...) puts a stand-in in place of write_memo for one test, and puts the real one back after. lambda text: "..." is a one-line function that takes the text and gives back that memo, so each test picks its memo. Change test_desk.py:
test_desk.py
1# test_desk.py2import agents
3from desk import Desk
456deftest_a_wrong_deadline_is_found(monkeypatch):7 monkeypatch.setattr(agents,"write_memo",lambda text:"Send notice by December 31, 2026.")8assert Desk().kickoff()=="Notice is due by December 30, 2026, and the memo doesn't say so."91011deftest_a_right_deadline_passes(monkeypatch):12 monkeypatch.setattr(agents,"write_memo",lambda text:"Send notice by December 30, 2026.")13assert Desk().kickoff()=="the memo has the deadline"
Terminal
1(env)$ python -m pytest -q
Output
.. [100%]2 passed in 2.59s
Two tests now, one for each memo, and no model was called. The stand-in answers in place of the agent:
.F [100%]...> assert Desk().kickoff() == "the memo has the deadline"E assert "Notice is du...esn't say so." == 'the memo has the deadline'E E - the memo has the deadlineE + Notice is due by December 30, 2026, and the memo doesn't say so....FAILED test_desk.py::test_a_right_deadline_passes - assert "Notice is du...es...1 failed, 1 passed, 8 warnings in 8.82s
The right-date test failed: the desk said the deadline was missing, though the stand-in's memo has it. ... stands for lines we left out. The stand-in never ran, and the real agent wrote the memo:
Drawing diagram…
InputToolResultTwo names for one function
from agents import write_memo gives desk.py a name of its own when the file loads. The test swaps the name inside agents, which desk.py no longer looks at. Calling agents.write_memo looks the name up on every call, so the swap is seen.