Sub topic 15
Nondeterministic Responses and Common Pitfalls
This chapter assumes you can write a test case with an utterance, expected topic, expected actions, and expected outcome. It also assumes you can run a suite and read per-case results. Here you learn why the same test can pass on one run and fail on the next, and how to avoid the mistakes that cause most false failures.
Why the same test can give different results
An Agentforce agent uses a large language model (LLM) to pick a topic, choose actions, and write a reply. An LLM can word the same answer differently each time. A unit test on ordinary code gets the same output every run. An agent test does not.
The three assertions differ in how stable they are:
| Assertion | Stability | Why |
|---|---|---|
| Expected topic | High | The choice is among a fixed list of names. |
| Expected actions | High | Action names are fixed, but the agent may skip or add one on unclear input. |
| Expected outcome | Lower | The reply is free text, and it is judged against your description. |
Because of this, assert on meaning, not on exact wording.
Write outcomes that tolerate rewording
A brittle outcome quotes text. A durable outcome describes what the reply must accomplish.
testCases:
- utterance: Where is my order 1001?
expectedTopic: Order_Status
expectedActions:
- Get_Order_Status
# Brittle: fails if the agent says "shipped" in other words
# expectedOutcome: Your order 1001 has shipped and will arrive Friday.
expectedOutcome: The agent tells the customer the current status of order 1001.State the facts that matter, such as "tells the customer the status". Leave out greetings, punctuation, and phrasing.
Common pitfalls
- Names that do not match. Expected topic and action names must match the developer names in your agent, not the labels shown in the UI. A typo fails the test even though the agent behaved correctly.
- Vague utterances. "Help with my thing" can reasonably reach several topics. Use utterances that a real customer would send and that have one clear destination.
- Overlapping topics. If two topic descriptions cover the same request, routing can vary. A failing test here points to the agent, so fix the topic descriptions instead of loosening the test.
- Forgetting empty actions. For out-of-scope requests, write
expectedActions: []. Leaving out real expectations makes the test check less than you think. - Test data that changed. An utterance about order 1001 fails if that record was deleted or edited. Use stable records or sandbox data you control.
- Over-specific outcomes. Checking for an exact date or phrase turns normal variation into failures.
- Judging by one run. One failure may be noise. One pass may be luck.
Handle a flaky test
A flaky test is one that passes and fails without any change to the agent. Follow this order:
- Run the case again, several times, and note how often it fails.
- If the topic or actions vary, the agent is ambiguous. Improve topic descriptions or instructions.
- If only the outcome varies, the expectation is probably too narrow or too vague. Rewrite it to describe meaning.
- If failure is consistent, treat it as a real regression.
Exercise
A test for "Where is my order 1001?" passes on most runs, but sometimes fails. The expected topic and actions are always correct. The expected outcome reads "Your order has shipped and will arrive Friday." What is the most likely cause, and how do you fix it?
Show answer
The outcome is too specific. It depends on exact status and date wording, and the order data or phrasing may differ between runs. Rewrite it to describe the meaning, for example "The agent tells the customer the current status of order 1001", and use stable test data.
Exercise
Name two checks you can make before deciding that a failing test shows a real agent problem.
Show answer
Run the case several times to see whether the failure is consistent, and confirm the expected topic and action names match the agent's developer names exactly.