Sub topic 10
Writing and running agent tests
This chapter assumes you can run sf commands against an authorized org, you have created an agent with sf agent create, and you have tried it with sf agent preview. You should also know basic YAML. Here you turn the informal checks you did in preview into repeatable tests that you can run from the terminal.
Why test an agent separately from previewing it
Preview is interactive. You type an utterance, read the reply, and decide by eye whether it looks right. That works for exploration, but it does not tell you whether a change to a topic description or an action broke something that worked yesterday.
An agent test records an utterance and the behavior you expect. Running the test sends the utterance to the agent and compares what happened with what you wrote down. Because an agent is driven by a language model, replies vary in wording. The tests therefore check the agent's decisions (which topic it picked and which actions it called) and then judge the final reply against a plain-language description.
A test case can check three things:
| Check | What it verifies | How strict it is |
|---|---|---|
| Expected topic | The topic the agent chose for the utterance | Exact name match |
| Expected actions | The actions the agent called while answering | Names must match the actions in your org |
| Expected outcome | A description of what the reply should accomplish | Judged against the actual reply, so wording can differ |
The test lifecycle
A test goes through four steps. You write a spec file, deploy it to the org as a test definition, run it, and read the results.
The spec is a local file you keep in source control. The test definition is the copy that lives in the org. The run executes the definition, so after you edit the spec you must create or update the definition again before the change takes effect.
Writing a test spec
A spec names the agent under test and lists test cases. This example has one case:
name: Case Helper Tests
description: Checks topic and action selection for case status questions
subjectType: AGENT
subjectName: Case_Helper
testCases:
- utterance: What is the status of case 00001026?
expectedTopic: case_status
expectedActions:
- GetCaseStatus
expectedOutcome: The agent tells the user the current status of the case.The fields work as follows:
subjectTypeisAGENTfor agent tests.subjectNameis the API name of the agent, the same value you passed as--api-namewhen you created and previewed it (Case_Helper).utteranceis the exact text sent to the agent.expectedTopicis the API name of the topic you expect the agent to select.expectedActionslists the action names you expect the agent to invoke.expectedOutcomedescribes the result in words.
The topic and action names must match the ones in your org. A common mistake is to copy the label shown in the Setup UI or the @InvocableMethod label instead of the name the agent metadata uses. If a test fails on the very first run with a name mismatch, compare your spec against the agent's topic and action names before you suspect the agent's behavior.
Choosing what to test
A useful suite covers more than the happy path. For the case status agent, consider these groups of utterances:
- Direct requests that should reach the
case_statustopic and call the action. - Paraphrases, such as "Where is my ticket 00001026 at?", to check that topic descriptions are broad enough.
- Requests for a case that does not exist, where the action returns "Not found" and the agent should say so instead of inventing a status.
- Off-topic requests, such as asking for a recipe, where the agent should not call any case action.
Each of these tests a different failure. Direct requests catch broken wiring. Paraphrases catch topic descriptions that are too narrow. Missing-record cases catch an agent that makes up an answer. Off-topic cases catch descriptions that are too broad.
A second case file for the off-topic and missing-record checks might look like this:
name: Case Helper Edge Tests
description: Checks paraphrases, missing records, and off-topic requests
subjectType: AGENT
subjectName: Case_Helper
testCases:
- utterance: Where is my ticket 00001026 at?
expectedTopic: case_status
expectedActions:
- GetCaseStatus
expectedOutcome: The agent tells the user the current status of the case.
- utterance: What is the status of case 99999999?
expectedTopic: case_status
expectedActions:
- GetCaseStatus
expectedOutcome: The agent says the case could not be found and does not invent a status.Keep one spec per area of behavior. Smaller specs run faster and make a failure easier to locate.
Test data
The utterance refers to case 00001026. The action reads real records, so the test passes only if that case exists in the org you run against. Tests that depend on specific records break when a sandbox is refreshed or a scratch org is recreated. For a scratch org, load known records as part of org setup. For a shared sandbox, pick records that are stable, or write the expected outcome so it does not depend on the exact status value.
Creating the test definition
Deploy the spec with sf agent test create, then run it with sf agent test run:
sf agent test create --spec specs/caseAgentTest.yaml --api-name Case_Helper_Tests --target-org my-dev-org
sf agent test run --api-name Case_Helper_Tests --wait 10 --result-format human --target-org my-dev-orgThe create command takes the local file (--spec) and stores it in the org under the name given by --api-name. That API name is separate from the agent's API name. Use a clear convention, such as the agent name plus _Tests.
The run command starts a test run for that definition. The options that matter most:
--wait 10makes the command wait up to 10 minutes and print results when the run finishes. Without--wait, the command starts the run and returns right away, and you fetch results separately.--result-format humanprints a readable table. Other formats, such asjson, are better for scripts.--target-orgselects the org, as with every othersfcommand.
Run sf agent test run --help to confirm the options your installed plugin version supports, since the agent plugin changes between releases.
Reading results
A run reports each test case as passed or failed, and for each expectation it shows what you expected and what the agent did. Read a failure from the top down:
- Check the topic first. If the agent chose the wrong topic, nothing after it can be right, and the fix usually belongs in topic descriptions or classification instructions.
- Check the actions next. A correct topic with a missing action points to the action's description or to the topic's instructions about when to call it.
- Check the outcome last. A correct topic and action with a failed outcome often means the reply was accurate but did not say what you described, or the expected outcome was too specific.
Run a failing utterance through sf agent preview to see the full conversation. Preview shows the reply in context, and the test result shows the verdict.
Flaky results
The model can behave slightly differently between runs. If a case passes sometimes and fails other times, run it several times before changing the agent. A case that flips often is usually ambiguous: the utterance could fit two topics, or the outcome is vague. Make the utterance more specific, tighten the topic descriptions so they do not overlap, or rewrite the outcome so a reasonable reply clearly passes or fails.
Updating tests after changes
When you rename a topic or action, update the spec, then run sf agent test create again against the same API name so the definition in the org matches. If the command reports that the definition already exists, check sf agent test create --help for the overwrite or update behavior in your plugin version. Commit the spec and the agent metadata in the same change, so reviewers see a behavior change and its tests together.
Common errors
| Symptom | Likely cause | What to do |
|---|---|---|
| Test fails immediately on topic or action name | Spec uses a label instead of the API name | Copy the names from the agent metadata |
| Test passes in one org and fails in another | Test data or agent version differs | Check the records the utterance relies on and the agent's active version |
| Run command returns without results | `--wait` was not set | Add `--wait`, or fetch the results of the run afterward |
| Edits to the spec have no effect | The definition in the org is stale | Run `sf agent test create` again |
| Action never appears in results | The action is not attached to the topic, or its metadata was not deployed | Deploy the Apex class and agent metadata, then retry |
Exercise
A test case expects the topic case_status and the action GetCaseStatus. It fails on the first run, and the result shows the agent called an action named Get_Case_Status. You have not changed the agent. What is the most likely cause, and what do you do?
Show answer
The spec uses a different action name than the one in the org's agent metadata, so the comparison fails even though the agent behaved correctly. Look up the exact action name in the agent's metadata, change expectedActions to match it, run sf agent test create again so the definition in the org is updated, and re-run the test.
Exercise
You want a test that checks the agent does not invent a status when a case number does not exist. Which three fields do you set, and what should the expected outcome say?
Show answer
Set utterance to a request that uses a case number that does not exist (for example, "What is the status of case 99999999?"), expectedTopic to case_status, and expectedActions to the case status action. The expectedOutcome should say that the agent states the case could not be found and does not give a made-up status.
Exercise
A test case passes on three runs and fails on the fourth with no changes in between. What should you do before editing the agent?
Show answer
Run the case several more times to confirm it is flaky, then look at why the result is unstable. The utterance may fit more than one topic, or the expected outcome may be too vague. Make the utterance more specific, narrow overlapping topic descriptions, or reword the outcome so a correct reply clearly passes.