Sub topic 14
Creating and Running Tests with Salesforce CLI
This chapter assumes you know what an Agentforce agent's topics and actions are. It also assumes you can read a test case made of an utterance, an expected topic, expected actions, and an expected outcome. You need a Salesforce org with Agentforce and the Testing Center enabled, the Salesforce CLI (sf) installed, and basic comfort with YAML and a terminal.
The Testing Center UI is good for building a first suite. The CLI is better when you want tests in version control, reviewed in pull requests, and run by a script. This chapter covers the full command-line loop: write a spec, create the test definition in the org, run it, and read the results.
How the CLI workflow fits together
A test spec is a YAML file on your machine. It is not the test itself. The CLI sends the spec to your org, where it becomes a test definition. You then run that definition, and the org returns a job that produces results.
Keeping this split in mind avoids a common confusion. Editing the YAML file on disk changes nothing in the org until you create the definition again.
Step 1: Authenticate to the org
Every command takes a --target-org flag. It accepts a username or an alias. Log in once and give the org an alias so the commands stay short.
sf org login web --alias my-org
sf org listThe user you log in as needs access to the agent and the Testing Center features. If a later command fails with a permissions or "not found" message, check this first.
Step 2: Write the spec
The spec names the agent under test and lists test cases. Each case pairs an utterance with the expected topic, expected actions, and expected outcome.
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.
- utterance: What is the best pizza recipe?
expectedTopic: Off_Topic
expectedActions: []
expectedOutcome: The agent politely declines and explains what it can help with.Save this as specs/order-support-tests.yaml. Check these fields before you create anything:
| Field | What it holds | Common mistake |
|---|---|---|
| `name` | Display name of the spec | Treating it as the API name used by later commands |
| `subjectType` | The kind of thing under test, here `AGENT` | Misspelling or lowercase |
| `subjectName` | Developer name of the agent | Using the agent's label instead of its developer name |
| `expectedTopic` | Developer name of the topic | Using the topic's display label |
| `expectedActions` | Developer names of actions, as a list | Omitting the key for a case that should call no actions |
The topic and action names in this example are illustrations. They must match the developer names in your own agent. A mismatch here is the most frequent reason a correct agent appears to fail.
Use expectedActions: [] when a case should call no action. An empty list is an assertion, not a missing value. It says the agent should not call anything for this utterance.
Step 3: Create the test definition
The create command reads the spec and builds the definition in the org. The --api-name flag sets the name you will use to refer to the test from now on.
sf agent test create --spec specs/order-support-tests.yaml --api-name Order_Support_Agent_Tests --target-org my-orgIf the command succeeds, the definition now exists in the org, and you can see it in the Testing Center UI. If the spec has a structural problem, such as a malformed YAML list or an unknown field, the command reports it here, before any test runs.
Running create again with an --api-name that already exists is an update question, not a new-test question. Check the command's --help output for how your installed CLI version handles an existing definition, and for any flag that previews the result without saving. Command flags change between plugin releases.
sf agent test create --helpStep 4: Run the suite
The run command starts a job against the definition.
sf agent test run --api-name Order_Support_Agent_Tests --wait 10 --result-format human --target-org my-orgThree flags matter here:
--api-namepicks the definition to run.--wait 10makes the command wait up to 10 minutes for the job to finish and then print results. Without--wait, the command starts the job and returns right away, so you must fetch results yourself.--result-formatchooses the output:humanfor a readable table,jsonfor scripts. Other formats may be available in your version.
Each test case sends its utterance to the agent as a real conversation turn. The run is not instant, and it consumes agent usage in the org. Run against a sandbox or a development org, not production.
Step 5: List definitions and fetch results
Two more commands cover the rest of the loop. list shows the test definitions in the org. results fetches the output of a previous run by its job ID.
sf agent test list --target-org my-org
sf agent test results --job-id <JOB_ID> --result-format json --target-org my-orgThe job ID is printed when a run starts. If you ran without --wait, save it. If you ran with --wait and the wait expired before the job finished, the job keeps running in the org. You can fetch its results later with the same ID.
Reading the output
The results report each test case separately. For each case, look at three checks that mirror the spec: whether the topic matched, whether the actions matched, and whether the outcome was judged to satisfy your description.
The topic and action checks compare names. The outcome check is different: a model evaluates the agent's reply against your expectedOutcome text. This has two consequences:
- A case can pass the topic and action checks and still fail the outcome check, if the reply does not match your description.
- A vague outcome such as "The agent helps the user" passes almost anything, so it protects you from very little.
When a case fails, find which of the three checks failed first. A topic failure usually means the agent routed the utterance elsewhere, or the topic name in the spec is wrong. An action failure means the agent called a different set of actions than you listed. An outcome failure means the reply did not match the description.
A working edit cycle
Most CLI work follows a short loop.
- Edit the YAML spec and add or change a case.
- Run
sf agent test createto push the change to the org. - Run
sf agent test runwith--wait. - Read the failures and decide whether the agent or the test is wrong.
- Fix the agent, or fix the spec, and repeat.
Step 4 is where judgment matters. If the agent behaves sensibly and your expectation was too narrow or misnamed, change the spec. If the agent routed a legitimate request to the wrong topic, fix the agent's topic descriptions or instructions, and leave the test alone.
Common errors
- Spec changed but results did not. You edited the file and ran the test without re-running
create. The org still holds the old definition. - Every case fails on topic. The developer names in
expectedTopicdo not match the agent's. Check them against the agent in the org. - The command returns before results appear. You omitted
--wait, or the wait was too short. Useresultswith the job ID. - Authentication errors. The alias in
--target-orgis not logged in, or points at a different org than you intended. Runsf org listto confirm. - A passing case that should fail. The expected outcome is too vague. Rewrite it to name the specific behavior you require.
Exercise
You edit expectedTopic in specs/order-support-tests.yaml from Order_Status to Order_Tracking, then run sf agent test run --api-name Order_Support_Agent_Tests --wait 10 --target-org my-org. The results are identical to the earlier run. What did you skip, and which command fixes it?
Show answer
You skipped sf agent test create. The YAML file is only local. The org still holds the earlier definition, so the run used the old expectedTopic. Run sf agent test create --spec specs/order-support-tests.yaml --api-name Order_Support_Agent_Tests --target-org my-org to push the change, then run the suite again.
Exercise
A run was started without --wait and printed a job ID. Which command do you use to get the results as JSON, and what must you supply?
Show answer
Use sf agent test results --job-id <JOB_ID> --result-format json --target-org my-org. You must supply the job ID from the run, plus the target org.