Sub topic 16
Keeping tests as a regression suite and automating runs
This chapter assumes you can write a test spec, create it with sf agent test create, and run it with sf agent test run. It covers how to keep the suite useful as the agent changes and how to run it automatically.
Why a passing suite is a regression suite
A regression is a behavior that worked before a change and breaks after it. Agents regress often, because three things can change routing and responses:
- A topic description or its instructions are edited.
- An action is added, renamed, or has its description changed.
- The underlying model or prompt behavior shifts.
A suite that passes today becomes your baseline. Rerun the same cases after every change and compare against that baseline.
Maintaining the suite
Treat the spec file as source code. Keep it in the same repository as the agent metadata and review changes to it in pull requests.
When the agent changes on purpose, the tests may need to change too. Use this rule:
- If the agent's new behavior is correct, update the test expectation.
- If the agent's new behavior is wrong, fix the agent.
- Never loosen an expectation only to make a failure disappear.
Add a new test case whenever you fix a bug. The case should use the utterance that exposed the bug. This keeps the bug from returning unnoticed.
Keep the names in your spec in sync with the agent. If you rename a topic or action, update expectedTopic and expectedActions. A stale name fails the case even when the agent behaves correctly.
Updating a deployed suite
To change a suite that already exists in the org, edit the YAML and run create again. Add --force-overwrite to replace the existing definition.
sf agent test create --spec specs/order-support-tests.yaml --api-name Order_Support_Agent_Tests --force-overwrite --target-org my-orgAutomating runs in CI
A continuous integration (CI) job can run the suite on every pull request. Use --result-format json so the output is easy to parse, and --wait so the job blocks until the run finishes.
name: Agent regression tests
on:
pull_request:
paths:
- "force-app/**"
- "specs/**"
jobs:
agent-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Salesforce CLI
run: npm install --global @salesforce/cli
- name: Authenticate to the test org
run: |
echo "${{ secrets.SFDX_AUTH_URL }}" > auth.txt
sf org login sfdx-url --sfdx-url-file auth.txt --alias my-org
rm auth.txt
- name: Deploy the agent
run: sf project deploy start --target-org my-org
- name: Create and run the suite
run: |
sf agent test create --spec specs/order-support-tests.yaml --api-name Order_Support_Agent_Tests --force-overwrite --target-org my-org
sf agent test run --api-name Order_Support_Agent_Tests --wait 10 --result-format json --target-org my-orgThe job fails when the CLI returns a nonzero exit code. Confirm this against your CLI version by running a deliberately failing case once.
Handling nondeterminism in automation
Agent responses can vary between runs. A case that fails once and passes on rerun is a flaky test, and it erodes trust in the pipeline. Before you block merges on a case, run it several times. If it varies, tighten the utterance or make the expected outcome describe the result rather than exact wording. Judge the topic and action checks as the stable core of the suite.
Exercise
A teammate renames the topic Order_Status to Order_Tracking in the agent. The next CI run fails the "Where is my order 1001?" case, although the agent still answers correctly. What is the likely cause, and what should you change?
Show answer
The spec still says expectedTopic: Order_Status, which no longer matches the agent's topic name. Update expectedTopic to Order_Tracking, recreate the suite with --force-overwrite, and rerun it.
Exercise
A case passes on four of five CI runs. Name two things you can do before letting it block merges.
Show answer
Rewrite the expected outcome to describe the result instead of exact wording, and make the utterance less ambiguous. Then rerun the case several times to confirm it is stable.