Sub topic 10
Writing and running agent tests
This chapter covers how to write an agent test spec and run it with the Salesforce CLI. It assumes you have already created an agent with sf agent create and tried it with sf agent preview. You should also know basic YAML and how to authorize an org.
Why test an agent
Previewing is manual. You type an utterance, read the reply, and judge it yourself. An agent test records that judgment so you can repeat it after every change. A test sends an utterance to the agent and compares what happened with what you expected.
Agent responses can vary, so tests check behavior instead of exact wording. Each test case can check three things:
- The topic the agent chose.
- The actions the agent called.
- The outcome, which is a plain-language description of what the reply should accomplish.
The test workflow
Writing a test spec
A test spec is a YAML file. Keep it in your project, for example under specs/, so it is versioned with the agent.
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:
| Field | Meaning |
|---|---|
| `subjectType` and `subjectName` | The thing under test. Here it is an agent, identified by its API name. |
| `utterance` | The message sent to the agent. |
| `expectedTopic` | The topic the agent should select. |
| `expectedActions` | The actions the agent should call. |
| `expectedOutcome` | A description of the expected result. |
The topic and action names must match the ones in your org. If you write a name that does not exist, the test fails even when the agent behaves well.
Deploying and running the test
Two commands deploy the definition and run it:
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-orgsf agent test create turns the spec into a test definition in the org, named by --api-name. sf agent test run starts a test run. The --wait 10 flag makes the command wait up to 10 minutes for the run to finish. Without it, the command returns right away, and you have to fetch the results later. The --result-format human flag prints a readable report. Other formats, such as JSON, are better for scripts.
If you edit the spec, run sf agent test create again so the org has the new version before you rerun.
Reading results
Results are reported per test case. For each one you can see whether the topic, the actions, and the outcome matched. When a case fails, compare the expected and actual values:
- A wrong topic usually means the topic descriptions overlap or are vague. Fix the topic's description or instructions.
- Missing actions usually mean the action's description does not tell the agent when to call it.
- A failed outcome with correct topic and actions often means the expectation is too strict or too loose. Reword it.
Common mistakes
- Testing exact wording. Test the outcome, not the sentence.
- Using display labels instead of API names for topics and actions.
- Running a test before the agent exists in the org, or against a different org than the one you created it in.
- Forgetting to rerun
sf agent test createafter editing the spec. - Using test data, such as a case number, that does not exist in the org.
Exercise
You changed the expectedOutcome in your YAML file and reran sf agent test run, but the results did not change. What did you most likely forget?
Show answer
You did not rerun sf agent test create. The run command uses the test definition stored in the org, so the edited spec must be deployed first.
Exercise
A test case fails because the agent chose a different topic than expectedTopic. Name one thing to check in the agent, not the test.
Show answer
Check the topic descriptions and instructions. If they overlap or are vague, the agent may pick the wrong topic for the utterance.