Sub topic 1
Why agents need a different kind of testing
Agentforce agents choose their own path through a conversation. A unit test for ordinary code can't check that behavior the way it checks a function. This chapter explains why, and what the Testing Center checks in its place. Later chapters cover the mechanics of writing and running tests.
Prerequisites
This chapter assumes you have the following:
- 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.
You don't need to have written an agent test before. You should know that a topic groups related instructions and actions, and that an action is something the agent can do, such as run a flow or call Apex.
How testing ordinary code works
A conventional unit test calls a function with fixed input and compares the return value to a fixed expected value.
def apply_discount(price: float, percent: float) -> float:
return round(price * (1 - percent / 100), 2)
def test_apply_discount():
assert apply_discount(100.0, 10) == 90.0
test_apply_discount()
print("passed")This works because of three properties:
- The code is deterministic. The same input gives the same output on every run.
- The input is structured. You pass typed arguments.
- The output is exact. You can compare it with
==.
An agent breaks all three.
What changes with an agent
An agent receives natural language, decides what to do, and replies in natural language. Each of the three properties fails in a specific way.
| Property | Ordinary code | Agent |
|---|---|---|
| Input | Typed arguments | Free text from a person |
| Path | Fixed by the code you wrote | Chosen at run time by a language model |
| Output | Exact value | Free text that can be worded many ways |
| Repeat runs | Identical | Can differ in wording and sometimes in behavior |
Input is open-ended
A customer can ask for the same thing in many ways: "Where is my order 1001?", "order 1001 status", or "has 1001 shipped yet?". You can't list every phrasing. A test can only sample the space, so you have to choose samples with care.
The path is chosen at run time
When an utterance arrives, the agent first picks a topic. Inside that topic, it picks which actions to run and in what order, guided by the topic's instructions. No if statement you can read decides this. The decision comes from the language model reading your topic descriptions and instructions.
That means a small edit can change routing. If you reword a topic description or add a new topic, requests that used to land in one topic can start landing in another. Nothing throws an error. The agent simply behaves differently.
Output is free text
The agent's final reply is generated. "Your order shipped yesterday" and "Order 1001 left our warehouse on Monday" can both be correct. An exact string comparison would fail the second one even though it is fine.
Repeat runs can differ
The same utterance can produce different wording on different runs. Occasionally it can produce different behavior, such as a different action order. A single pass doesn't prove the agent is reliable, and a single failure doesn't prove it is broken. Later chapters return to this in the section on nondeterminism.
What you test instead
Because the final text is the least stable part, you test the decisions that lead to it. Each decision can be checked on its own.
- Topic. The check is an exact match on the topic name. This is stable and cheap to assert.
- Actions. The check is whether the expected actions ran. This is also stable, because action names are fixed.
- Outcome. The check is a plain-language description of what the reply should accomplish. It is judged on meaning, not on exact wording.
Here is a test case that uses all three, taken from the YAML spec format you will use later:
name: Order_Support_Agent_Tests
description: Core routing and action checks for the order support agent
subjectType: AGENT
subjectName: Order_Support_Agent
testCases:
- utterance: Where is my order 1001?
expectedTopic: Order_Status
expectedActions:
- Get_Order_Status
expectedOutcome: The agent tells the customer the current status of the order.The topic and action names must match the developer names in your own agent. They are examples here.
What agent tests can catch
Agent tests are most useful for the failures people actually hit:
- Misrouting. A request goes to the wrong topic, so the wrong instructions apply.
- Missing or extra actions. The agent answers from its own guess instead of calling the lookup action, or it runs an action it shouldn't.
- Scope leaks. The agent answers an out-of-scope question, such as a pizza recipe, instead of declining.
- Regressions after edits. A change to a topic description, instruction, or action breaks a path that used to work.
- Poor replies. The reply is on topic but doesn't do what the customer asked.
What agent tests do not replace
The Testing Center checks behavior against expectations you write. It doesn't replace the following:
- Testing the underlying logic. Flows, Apex, and prompt templates that an action calls still need their own tests. An agent test can tell you that
Get_Order_Statusran. It can't tell you the flow computed the right status. - Review of real conversations. Real users phrase things in ways you didn't predict. Their conversations are a source of new test cases.
- Security and permission review. Check what data an agent can reach through its actions separately.
Common mistakes at this stage
- Expecting exact wording. Writing an expected outcome as a literal sentence leads to brittle tests. Describe the result, not the phrasing.
- Testing only the happy path. Agents fail most visibly at the edges: vague requests, missing order numbers, and questions outside the agent's scope.
- Treating one run as proof. Because runs can vary, a single pass or fail is weak evidence. Look for patterns.
- Skipping tests after a "small" edit. Wording changes in topic descriptions can reroute requests. Run the suite after any change.
Exercise
A teammate says, "We don't need agent tests. We already unit-test the flow behind the GetOrderStatus action." Give two failures that the flow's unit tests would miss and an agent test would catch.
Show answer
Any two of these: (1) The agent routes "Where is my order 1001?" to the wrong topic, so the flow is never called. (2) The agent answers from its own guess and never runs GetOrderStatus. (3) The agent runs the action but its reply doesn't tell the customer the status. (4) The agent answers an out-of-scope request such as a pizza recipe instead of declining. The flow's unit tests only check the flow's own logic, not the agent's decisions before and after it.
Exercise
Why does the Testing Center check the topic and actions as separate expected values instead of only comparing the final reply to an exact string?
Show answer
The reply is free text that can be worded many ways and can differ between runs, so exact comparison is brittle. Topic and action names are fixed, so they can be checked exactly. Checking them separately also shows where a failure happened: routing, action selection, or the reply itself.