Sub topic 10
Defining tests in a YAML spec or CSV file
This chapter covers writing test cases as files instead of clicking them into the Testing Center UI. It assumes you know the four parts of a test case (utterance, expected topic, expected actions, expected outcome), have a Testing Center-enabled org, and can read YAML and use a terminal.
Why define tests in a file
The UI works well for a handful of cases. A file works better once the suite grows, for these reasons:
- You can store it in version control next to the agent's metadata, so changes to tests are reviewed like code.
- You can edit many cases at once in an editor, with copy and paste.
- You can create the same suite in several orgs, such as a sandbox and a scratch org, from one source.
- A pipeline can create and run it without anyone opening a browser.
Two file formats exist, and they serve different entry points:
| Format | Used with | Best for |
|---|---|---|
| YAML spec | Salesforce CLI (`sf agent test create`) | Suites kept in a repo and run from a terminal or pipeline |
| CSV | Bulk upload in the Testing Center UI | Importing many cases from a spreadsheet, often written by non-developers |
Both describe the same thing: a list of test cases. Pick based on who writes the tests and where they run.
The YAML spec structure
A spec has a header that identifies the suite and the agent under test, followed by a list of test cases.
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.Header fields
nameis the label of the test definition.descriptionis free text for humans. Use it to say what the suite protects.subjectTypeisAGENTfor an Agentforce agent.subjectNameis the developer name (API name) of the agent being tested, not its display label.
Test case fields
utteranceis the exact message sent to the agent.expectedTopicis the developer name of the topic the agent should route to.expectedActionsis a list of action developer names the agent should run. Use[]when no action should run.expectedOutcomeis a plain-language description of what the response should accomplish. It is not a string to match word for word. The next chapters cover how it is judged.
The topic and action names in the example come from a fictional agent. They must match the developer names in your own agent exactly. A label such as "Order Status" will not match Order_Status. Open the agent in Agent Builder, or check the metadata, to copy the real names.
YAML pitfalls
Most spec errors are YAML errors, not Agentforce errors.
- Colons in text. An utterance like
Order status: where is 1001?contains a colon followed by a space, which YAML reads as a new key. Wrap the value in quotes. - Quotes inside quotes. If the utterance contains an apostrophe, use double quotes around it, as in
"What's the status of my order?". - Indentation. Use spaces only, and indent every field of a case at the same level under its
-. A misplacedexpectedActionssilently becomes part of the wrong case or causes a parse error. - Empty versus missing actions.
expectedActions: []says "no action should run." Leaving the key out says nothing about actions. For out-of-scope cases, write the empty list so your intent is explicit. - Multiple actions. List each action on its own line. The list is the set of actions you expect the agent to run for that utterance. Only list actions that matter to the behavior you are protecting.
Here is a spec fragment that applies these rules:
testCases:
- utterance: "Order status: where is 1001?"
expectedTopic: Order_Status
expectedActions:
- Get_Order_Status
expectedOutcome: The agent reports the current status of order 1001.
- utterance: "What's your return policy?"
expectedTopic: Returns_Policy
expectedActions: []
expectedOutcome: The agent explains the return policy from its knowledge source.Generating a starting spec
You do not have to write the header from memory. The CLI has a command that walks you through building a spec interactively and writes it to a file (sf agent generate test-spec). Run it with --help to see the options in your installed plugin version. Treat the output as a draft and review each name against the agent.
The CSV format
For the UI's bulk upload, each row is one test case. The Testing Center provides a CSV template. Download it from the new-test flow and use its header row exactly, because the column names and the way multiple actions are written in one cell are defined by that template. Do not rely on the sample below for the exact header.
Utterance,Expected Topic,Expected Actions,Expected Outcome
"Where is my order 1001?",Order_Status,Get_Order_Status,The agent tells the customer the current status of the order.
"What is the best pizza recipe?",Off_Topic,,The agent politely declines and explains what it can help with.CSV has its own traps:
- A comma inside a value splits the cell, so wrap any value with a comma in double quotes.
- Spreadsheet programs can change quote characters to "smart quotes" and can reformat text on save. Export as plain UTF-8 CSV and open the file in a text editor before uploading.
- An empty Expected Actions cell stands for "no actions." Confirm how the template expects that to be written.
From file to a runnable suite
A spec file on disk is not yet a test. It becomes one when you create the test definition in the org. The flow looks like this:
Create the definition from the spec with the command below. The --api-name value becomes the API name of the test definition, and it is the name you use when you run it later.
sf agent test create --spec specs/order-support-tests.yaml --api-name Order_Support_Agent_Tests --target-org my-orgRun sf agent test create --help to see how your plugin version handles a test definition that already exists. When you edit a spec, the org copy does not change on its own. You must push the edit again, or the next run tests the old cases.
Choosing between YAML and CSV
Use YAML when the suite lives in a repository and runs in CI. Differences show up cleanly in a pull request, and a reviewer can see that a case was added or an expected topic changed.
Use CSV when subject-matter experts write cases in a spreadsheet and a developer or admin uploads them. Keep the CSV in the repo too, so the upload is reproducible.
Do not maintain both formats for the same suite by hand. The two copies drift apart. Choose one as the source of truth.
A review checklist for a spec
Before you create the definition, check each of these:
subjectNamematches the agent's API name.- Every
expectedTopicand every action name exists on the agent, with the same capitalization and underscores. - Utterances with colons, apostrophes, or commas are quoted.
- Out-of-scope cases use
expectedActions: []. - Each
expectedOutcomedescribes behavior, such as "declines and explains what it can help with," not exact wording. - The file is committed, so the next change shows as a diff.
Exercise
A teammate's spec has this case: utterance: Cancel order: 1001 with expectedTopic: Order Cancellation and no expectedActions key. The suite fails to parse, and after a fix, the case fails on the topic. Name three corrections.
Show answer
(1) Quote the utterance, as in "Cancel order: 1001", because the colon followed by a space is read as a YAML key separator. (2) Replace Order Cancellation with the topic's developer name, for example Order_Cancellation, copied from the agent. (3) Add expectedActions explicitly, listing the cancel action's developer name, or [] if no action should run.
Exercise
You edited expectedTopic in specs/order-support-tests.yaml and committed it, but the next run still tests the old topic. What happened, and what do you do?
Show answer
The test definition in the org is separate from the file. Editing the file does not update the org. Push the spec to the org again with sf agent test create, using the same --api-name. Check --help for the option that overwrites an existing definition, then run the suite again.