Sub topic 14
Creating and running tests with Salesforce CLI
The Testing Center UI is fine for a first suite. Salesforce CLI lets you keep tests in source control, review changes in pull requests, and run them from a terminal or a script. This chapter covers the commands for that workflow.
Prerequisites
This chapter assumes you have:
- Salesforce CLI installed and an org authorized under the alias
my-org. - An Agentforce agent in that org, with the Testing Center enabled.
- Basic comfort with YAML and a command line.
- Knowledge of the four parts of a test case: utterance, expected topic, expected actions, and expected outcome.
The workflow
The CLI workflow has four steps. You write a spec file, create the test definition in the org, run it, and read the results.
Step 1: Write the spec
The spec names the agent under test and lists the test cases. Save this as specs/order-support-tests.yaml.
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.The values for subjectName, expectedTopic, and expectedActions must match the developer names in your own agent. The names above are examples. An empty list for expectedActions asserts that the agent calls no action.
Step 2: Create and run the test
The first command creates the test definition in the org from the spec. The second runs it, waits up to 10 minutes, and prints readable results.
sf agent test create --spec specs/order-support-tests.yaml --api-name Order_Support_Agent_Tests --target-org my-org
sf agent test run --api-name Order_Support_Agent_Tests --wait 10 --result-format human --target-org my-orgThe --api-name value becomes the API name of the test definition in the org. Use the same value in later commands.
Step 3: List tests and fetch results
If you do not use --wait, or the wait times out, the run keeps going in the org. You can list the definitions and fetch a finished run later by job ID.
sf agent test list --target-org my-org
sf agent test results --job-id <JOB_ID> --result-format json --target-org my-orgUse --result-format human when you read results yourself. Use json when a script parses them.
Common errors
- Name mismatch. A topic or action name in the spec that differs from the agent's developer name makes that case fail. The agent is not at fault. Compare the spec against the agent's actual names first.
- Wrong org.
--target-orgdecides where the test is created and run. Confirm the alias before you run a suite. - Timeout. A short
--waitcan return before the run finishes. Use the job ID withsf agent test resultsto collect the results. - Stale definition. Editing the YAML file does not change the test in the org. You must create the definition again.
Exercise
You edited an expected outcome in your YAML spec and ran sf agent test run, but the results still show the old text. What went wrong, and how do you fix it?
Show answer
The run uses the test definition stored in the org, not your local file. Update the definition from the edited spec with sf agent test create (using the same --spec and --api-name), then run the suite again.