Sub topic 16
Keeping tests as a regression suite and automating runs
A test suite earns its value after the first release. Every change to a topic, action, or instruction can alter how the agent behaves. A regression suite is the set of tests you rerun after each change to confirm that behavior that worked before still works. This chapter covers how to keep the suite healthy, how to version it, and how to run it automatically with Salesforce CLI.
This chapter assumes you can already write a test case (utterance, expected topic, expected actions, expected outcome). It also assumes you can run a suite from the UI or with sf agent test run, and that you can read per-case results. You should know basic Git and CI concepts.
What changes break an agent
Agent behavior depends on several parts that change independently. A regression suite should run after any of these:
| Change | Typical regression |
|---|---|
| Topic description or classification instructions edited | Utterances route to a different topic |
| New topic added | The new topic captures utterances that an older topic used to handle |
| Action renamed, rewired, or its input changed | The expected action no longer runs, or runs with different inputs |
| Instructions reworded | Tone or content of the response shifts, and the outcome check fails |
| Underlying Flow, Apex, or prompt template changed | The action runs but returns different data |
| Model or platform release | Routing or wording changes with no change on your side |
The last row is why scheduled runs matter. A suite that only runs when you edit the agent misses changes that come from the platform.
Treat the suite as source code
Keep the YAML spec in the same repository as the agent metadata. This gives you three things:
- A pull request that changes a topic can also change the tests that cover it, and a reviewer sees both.
- Git history shows when an expectation changed and why.
- CI can create the test definition from the file, so the org copy never drifts from the reviewed copy.
A layout that works:
my-project/
force-app/main/default/... # agent and action metadata
specs/
order-support-tests.yaml # core routing and action checks
order-support-edge-tests.yaml # edge cases and out-of-scope requests
.github/workflows/
agent-tests.ymlSplitting suites by purpose is useful. A small smoke suite of 10 to 20 main-path cases can run on every pull request. A larger full suite with edge cases can run nightly. Each suite is a separate spec with its own --api-name.
Maintaining the suite
A suite left alone goes stale in two ways. Tests start failing for reasons that no longer matter, and the team learns to ignore failures. Or the agent gains behavior that no test covers.
Apply these habits:
- Update the test in the same change as the agent. If you deliberately rename
Get_Order_StatustoLookup_Order, updateexpectedActionsin the same pull request. A failure caused by an intended change is a test defect, not an agent defect. - Add a test for every bug you fix. Take the utterance that failed in production, write the expected topic, actions, and outcome, and confirm the test fails before the fix and passes after. This is the cheapest way to prevent the same bug twice.
- Add tests when you add scope. A new topic needs main-path cases, plus at least one case that checks older topics still win their own utterances.
- Remove or rewrite tests that no longer reflect intended behavior. Do not leave a known-failing test in the suite. Fix it, change it, or delete it with a note in the commit message.
- Keep the expected outcome specific but not literal. Describe what the response must accomplish ("tells the customer the current status of the order") instead of quoting exact wording. Literal wording makes the test fail on harmless rephrasing.
Compare runs over time
A single pass or fail result tells you the current state. A regression check needs a baseline to compare against. Save the results of a known-good run, then compare later runs to it.
sf agent test list shows the test definitions in the org, and sf agent test results fetches a previous run by job ID. With JSON output you can script a comparison:
sf agent test list --target-org my-org
sf agent test results --job-id <JOB_ID> --result-format json --target-org my-org > baseline.jsonKeep these points in mind when you compare:
- Compare per-case results, not just the total. Two runs can both show 18 of 20 passing while a different pair of cases fails each time.
- A case that flips between passing and failing across runs with no change in between is a flaky case. Flakiness usually comes from nondeterministic wording or a vague expected outcome. Tighten the utterance or the outcome statement before you trust that case.
- Record the agent version or commit hash next to each saved result, so a regression points to a specific change.
The regression loop
Automating runs with Salesforce CLI
The CLI commands from the earlier chapter work unchanged in a script. Automation adds three needs: a non-interactive login, a way to wait for results, and a machine-readable report.
The first two commands create (or refresh) the definition from the spec and run it. --wait makes the command block until the run finishes or the time limit passes.
sf agent test create --spec specs/order-support-tests.yaml --api-name Order_Support_Agent_Tests --force-overwrite --target-org ci-org
sf agent test run --api-name Order_Support_Agent_Tests --wait 10 --result-format junit --output-dir test-results --target-org ci-orgJUnit is an XML format that most CI systems display natively, so failed cases appear as failed tests in the pull request. Use --result-format json instead if you want to process results in your own script.
Here is a GitHub Actions workflow that runs the suite on pull requests and every weekday morning. It uses JWT-based login, which needs no browser. Store the private key, client ID, and username as repository secrets.
name: Agent regression tests
on:
pull_request:
paths:
- "force-app/**"
- "specs/**"
schedule:
- cron: "0 6 * * 1-5"
jobs:
agent-tests:
runs-on: ubuntu-latest
env:
SF_CLIENT_ID: ${{ secrets.SF_CLIENT_ID }}
SF_USERNAME: ${{ secrets.SF_USERNAME }}
SF_JWT_KEY: ${{ secrets.SF_JWT_KEY }}
steps:
- uses: actions/checkout@v4
- name: Install Salesforce CLI
run: npm install --global @salesforce/cli
- name: Log in to the test sandbox
run: |
printf '%s' "$SF_JWT_KEY" > server.key
sf org login jwt \
--client-id "$SF_CLIENT_ID" \
--jwt-key-file server.key \
--username "$SF_USERNAME" \
--instance-url https://test.salesforce.com \
--alias ci-org
rm server.key
- name: Create test definition from spec
run: sf agent test create --spec specs/order-support-tests.yaml --api-name Order_Support_Agent_Tests --force-overwrite --target-org ci-org
- name: Run test suite
run: sf agent test run --api-name Order_Support_Agent_Tests --wait 10 --result-format junit --output-dir test-results --target-org ci-org
- name: Upload results
if: always()
uses: actions/upload-artifact@v4
with:
name: agent-test-results
path: test-resultsThe if: always() on the upload step keeps the report available even when the run step fails, which is when you most need it.
Pitfalls in automation
- Run against a sandbox, not production. Tests invoke real actions. An action that creates or updates records will do so on every run. Use test data, or make sure the actions under test are read-only or safe to repeat.
- Confirm the failure signal once. Break a test on purpose, run the workflow, and check that the job goes red. Behavior of exit codes can vary by CLI version, so verify it in your own pipeline instead of assuming.
- Set
--waithigh enough. If the wait expires before the run finishes, the command returns without full results. You can fetch them later withsf agent test results --job-id. - Keep secrets out of logs. Write the key to a temporary file, delete it after login, and never echo it.
- Expect some variation. Agent responses are nondeterministic. If a case fails on one run and passes on a rerun, treat it as a flaky test to fix, not as a reason to add automatic retries that hide real regressions.
- Cap run time. A large suite on every pull request slows reviews. Use the smoke suite for pull requests and the full suite on a schedule.
Exercise
Your team renames the action GetOrderStatus to Lookup_Order. The next pull request run shows the "Where is my order 1001?" case failing on expectedActions, but the agent still returns the correct order status to the customer. Is this an agent regression or a test defect, and what should you change?
Show answer
It is a test defect. The agent behaves correctly. The expectedActions list still names the old action. Update the test spec so expectedActions contains Lookup_Order, in the same pull request as the rename, then rerun the suite. Going forward, treat the spec as part of the change so it never lags the agent metadata.
Exercise
A scheduled nightly run fails one case that passed yesterday, and nothing in the repository changed. The same case passes when you rerun it. What are two likely causes, and what should you do?
Show answer
Likely causes are a platform or model change that shifted routing or wording, or a flaky case with a vague utterance or an overly literal expected outcome. Compare the per-case result to the saved baseline, rerun a few times to see how often it fails, and then tighten the utterance or outcome statement. If it fails consistently after a platform change, investigate the topic or instruction wording as a real regression.