Sub topic 15
Nondeterministic Responses and Common Pitfalls
This chapter assumes you can already write a test case with an utterance, an expected topic, expected actions, and an expected outcome. It also assumes you have run a suite in the Testing Center and opened a per-case result. Here you will learn why the same test can pass on one run and fail on the next, and how to write tests that stay stable.
Why the same test can give different results
An Agentforce agent is driven by a large language model (LLM). An LLM chooses words by probability, so it can phrase the same answer in many ways. A unit test for ordinary code calls a function with fixed input and compares the result to one exact value. An agent test cannot do that for the whole response.
Variation enters at several points:
- Wording. The agent might say "Your order shipped yesterday" in one run and "Order 1001 left our warehouse on Monday" in the next. Both are correct.
- Reasoning path. For a vague utterance, the model may choose a different topic or action order between runs.
- Data. Actions read live org data. A test that depends on an order's status fails when that status changes.
- Agent changes. Edits to topic descriptions, instructions, or action descriptions change routing, even when the underlying model is unchanged.
The three assertions in a test case differ in how much variation they tolerate.
| Assertion | How it is checked | Stability |
|---|---|---|
| Expected topic | Compared with the topic the agent selected | High, if the utterance is clear |
| Expected actions | Compared with the actions the agent invoked | High to medium, depends on data and phrasing |
| Expected outcome | Judged against the response for meaning, not exact text | Medium, depends on how precisely you wrote it |
The topic and action checks are about structure. The outcome check is about meaning. Put your strictest expectations on the structural checks and keep the outcome description focused on what must be true of the response.
How the variation shows up
A flaky test is one that passes and fails on the same agent without any change. Run the suite more than once before you decide what a failure means.
A case that fails every time points to a real mismatch. A case that fails sometimes points to ambiguity, either in the utterance or in your expectation.
Common pitfalls and fixes
Pitfall 1: Asserting exact wording in the expected outcome
Expected outcomes are descriptions of what should happen. If you copy one sample response into the field, you test for that wording and not for the behavior.
Weak:
expectedOutcome: Your order 1001 shipped on Monday and will arrive Thursday.Better:
expectedOutcome: The agent tells the customer the current status of the order and the expected delivery date.The second version stays true when the phrasing changes and when the order number changes.
Pitfall 2: Vague utterances that justify more than one topic
Take the utterance "I need help with my stuff." A reasonable agent might ask a clarifying question, or pick any topic. If your test expects Order_Status, you are testing a guess.
Write one test per intent. Use utterances a customer would plausibly send, with enough detail to make the intended topic the clear choice. If an utterance is truly ambiguous, test it separately and expect the clarifying behavior in the outcome.
Pitfall 3: Tests that depend on changing data
The Get_Order_Status action returns whatever the org holds for order 1001. If a teammate edits that record, your test might fail even though the agent works.
- Use dedicated test records that nobody else edits.
- Keep the outcome independent of specific field values, as shown above.
- Document which records a suite relies on, for example in the suite's
descriptionfield.
Pitfall 4: Names that do not match the agent
The topic and action names in a spec must match the developer names in your agent. A label such as "Order Status" does not match Order_Status. A mismatch fails the case every time, and it looks like an agent defect.
Check the names in Agent Builder before you write the spec. After you rename a topic or action, update every test that references it.
Pitfall 5: Forgetting that extra actions can matter
If the agent calls an additional action you did not list, the case can fail even when the final answer is fine. Consider whether the extra call is correct. If it is, add it to the expected list. If it is not, you found a real defect, such as a topic that triggers a lookup it does not need.
Pitfall 6: Testing only the happy path
A suite made of five friendly order-status questions says little. Include:
- Main paths: the common requests the agent exists to serve.
- Edge cases: missing order numbers, unknown records, mixed requests, and unusual phrasing.
- Out-of-scope requests: questions the agent must decline, with
expectedActions: [].
An empty action list on an out-of-scope case is a useful check. It proves the agent did not call an action it should not.
Pitfall 7: Rewriting expectations to make a failure go away
When a case fails, the quick fix is to change the test to match what the agent did. Only do that when the agent's behavior is actually correct and your expectation was wrong. If you edit tests to match every output, the suite stops detecting regressions.
Writing resilient cases
The following suite applies the guidance above. It uses a plain request, a request with a typo and informal wording, a request with a missing order number, and an out-of-scope request.
name: Order_Support_Resilient_Tests
description: Routing and action checks that tolerate wording changes. Uses test orders 1001 and 1002 only.
subjectType: AGENT
subjectName: Order_Support_Agent
testCases:
- utterance: Where is my order 1001?
expectedTopic: Order_Status
expectedActions:
- Get_Order_Status
expectedOutcome: The agent reports the current status of the order without asking for information the customer already gave.
- utterance: wheres order 1002 at rn
expectedTopic: Order_Status
expectedActions:
- Get_Order_Status
expectedOutcome: The agent reports the current status of the order.
- utterance: Where is my order?
expectedTopic: Order_Status
expectedActions: []
expectedOutcome: The agent asks the customer for the order number before looking anything up.
- utterance: What is the best pizza recipe?
expectedTopic: Off_Topic
expectedActions: []
expectedOutcome: The agent politely declines and explains what it can help with.The third case expects no action because the agent has no order number to pass. That choice depends on how your agent is configured. If your agent is set up differently, adjust the expectation to match the intended design, and confirm it by running the case several times.
Deploy and run it the same way as any other spec:
sf agent test create --spec specs/order-support-resilient.yaml --api-name Order_Support_Resilient_Tests --target-org my-org
sf agent test run --api-name Order_Support_Resilient_Tests --wait 10 --result-format human --target-org my-orgA practical routine for unstable cases
- Run the suite two or three times and note which cases change between runs.
- For a case that flips, read the agent's actual response in the result detail. Decide which of its behaviors you actually care about.
- If the utterance is ambiguous, make it more specific, or split it into two cases.
- If the outcome is too specific, restate it as a behavior.
- If the topic or actions flip, look at the topic descriptions and instructions. Overlapping topic scopes are the most common cause.
- Rerun and confirm that the case is stable before you rely on it.
Summary of rules
- Assert behavior, not wording.
- Make each utterance map clearly to one intent.
- Use stable test data.
- Keep names in the spec identical to the developer names in the agent.
- Rerun failures before diagnosing them.
- Change a test only when the expectation was wrong.
Exercise
A test case with the utterance "Where is my order 1001?" passes on two runs and fails on the third. The failing run shows the correct topic and action, but the expected outcome check fails because the response said the order is "on its way" and your expected outcome reads "The agent says the order shipped on Monday." What is wrong, and how would you fix it?
Show answer
The expected outcome is tied to one specific fact and phrasing, so a correct response that words it differently fails. The agent is not at fault. Rewrite the outcome as a behavior, for example "The agent tells the customer the current status of the order." Then rerun the suite several times to confirm it is stable. If the order data in the org changes, also use a dedicated test record so the case does not depend on a shared record.
Exercise
You add a case with the utterance "I have a question" and expect the topic Order_Status. It fails about half the time. Give two valid ways to resolve this.
Show answer
The utterance is ambiguous, so the agent may reasonably route it differently on each run. One fix is to make the utterance specific, such as "I have a question about where order 1001 is," so Order_Status is the clear choice. The other fix is to keep the vague utterance and change the expectation to the intended behavior, for example an outcome that says the agent asks a clarifying question, with an empty expectedActions list. Do not simply loosen the test until it passes without deciding what the agent should do.