Sub topic 1
Why agents need a different kind of testing
Prerequisites
This chapter assumes you have:
- Basic familiarity with Agentforce agents, topics, and actions.
- Access to a Salesforce org with Agentforce and the Testing Center enabled.
- Basic comfort with YAML and a command line, which later chapters use.
Code tests check exact answers, agent tests check behavior
A unit test for code calls a function with fixed input and compares the result to one exact value. If add(2, 3) returns 5 today, it returns 5 tomorrow. The test passes or fails for a reason you can trace to a line of code.
An Agentforce agent works differently. A customer types a free-text request, and a large language model (LLM) decides what to do with it. The agent has no code path that maps "Where is my order 1001?" to a handler. Instead, the agent reasons over its instructions, picks a topic, and picks actions to run. It then writes a reply in natural language.
Three properties follow from this design.
- Input is open-ended. Customers phrase the same request many ways, and they also ask things the agent was never built for.
- Output is not fixed. Two runs can produce differently worded replies that are both correct.
- Behavior depends on configuration. Editing a topic description, an instruction, or an action can change routing for requests you did not touch.
What goes wrong without agent-specific tests
Manual spot checks in a chat window miss most of these failures. You try five phrasings, they work, and you ship. Then a customer words the request differently and the agent routes to the wrong topic. Or the agent answers an off-topic question that it should decline.
Exact string matching on replies does not fix this. A test that demands one exact sentence fails whenever the wording varies, even if the behavior is right.
What to check instead
Agent tests assert on the decisions the agent makes and on the quality of the result. Each decision can be checked on its own.
| Layer | Question the test asks |
|---|---|
| Topic | Did the agent choose the right topic for this request? |
| Actions | Did it run the expected actions? |
| Outcome | Does the final reply do what the request needed? |
The Testing Center, covered in later chapters, lets you check all three layers for each test case.
Common mistakes to avoid
- Testing only the happy path. Include out-of-scope requests, since declining correctly is also behavior you want to protect.
- Treating one passing run as proof. Because replies vary, a single pass shows less than it would for code.
- Checking only the reply text. A reply can sound right while the agent used the wrong topic or skipped an action.
Exercise
A teammate writes a test that passes only if the agent's reply equals the exact sentence "Your order 1001 has shipped." Give two reasons this test is fragile for an agent.
Show answer
First, the agent is LLM-driven, so it can word a correct reply differently on each run, and the test would fail even though the behavior is right. Second, the test checks only the reply text. It would not show whether the agent chose the right topic or ran the expected action, so a wrong routing decision could go unnoticed.