Sub topic 12
Running agent tests in a CI pipeline
A continuous integration (CI) pipeline runs the same checks on every pull request, so a prompt or action change that breaks an agent is caught before it reaches an org people use. This chapter wires the sf agent test run command into such a pipeline.
This chapter assumes you have:
- A test spec deployed as a test definition, as in the previous chapters.
- Agent metadata tracked in a Salesforce DX project under Git.
- A disposable org for CI, such as a sandbox or scratch org. Agent tests call a real org, so they need one.
The shape of the pipeline
A useful pipeline has four steps. It authenticates without a browser, deploys the project, runs the tests, and fails the build if any test case fails.
Authenticate without a browser
sf org login web needs a person at a browser, so it does not work in CI. Use one of these instead:
- JWT bearer flow (
sf org login jwt) uses a connected app, a certificate, and a private key. Teams often choose it for long-lived pipelines. - SFDX auth URL (
sf org login sfdx-url) uses a URL you export once withsf org display --verbose --json. It is simpler to set up.
Store either credential as a secret in your CI system. Never commit it.
A GitHub Actions example
This workflow uses the auth URL approach. The job name, secret name, and paths are examples you can change.
name: agent-tests
on:
pull_request:
paths:
- "force-app/**"
- "specs/**"
jobs:
test-agent:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Salesforce CLI
run: npm install --global @salesforce/cli
- name: Authenticate to CI org
run: |
echo "${{ secrets.SF_AUTH_URL }}" > auth-url.txt
sf org login sfdx-url --sfdx-url-file auth-url.txt --alias ci-org
rm auth-url.txt
- name: Deploy metadata
run: sf project deploy start --source-dir force-app --target-org ci-org
- name: Run agent tests
run: |
sf agent test run \
--api-name Case_Helper_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-resultsThree details matter here:
- The
--wait 10flag makes the command block for up to 10 minutes. Without it, the command returns once the run starts, and the pipeline would not see the outcome. - The
junitformat produces XML that most CI systems display as a test report. Thejsonformat is better when a script processes the results. if: always()on the upload step keeps the results even when the test step fails, which is when you need them most.
Making failures meaningful
Agent responses come from a language model, so the same utterance may not produce identical wording each time. Write expectations that tolerate this:
- Assert on the topic and actions, which are stable, before asserting on wording.
- Keep the expected outcome short and about meaning, such as "tells the user the case status".
- Rerun a failing test locally with
sf agent previewbefore changing the spec. The agent may be wrong, or the test may be too strict.
If the test step reports success when you know a case failed, check the exit code on your own runner. Run the command once against a deliberately wrong expectedTopic and confirm the build turns red. Do this once when you set up the pipeline.
Keeping the pipeline fast and safe
- Run on pull requests that touch agent files, as the
pathsfilter does. - Use a separate CI org so test conversations and test data stay away from real users.
- Run deployment and tests in the same job so the tests see the metadata you just deployed.
- Do not activate the agent from CI on pull requests. Leave activation to a gated release job.
Exercise
Your pipeline runs sf agent test run --api-name Case_Helper_Tests --result-format junit and the job finishes in seconds with no test report. What is the most likely missing flag, and why does it matter?
Show answer
The --wait flag. Without it, the command returns as soon as the run is queued, so the pipeline never receives results. Adding --wait 10 makes the command block until the run finishes or ten minutes pass. Adding --output-dir also gives the junit file somewhere to be written.