Designing a Testing Strategy for Nondeterministic Agents
This lesson describes the layers of an agent testing strategy and where each runs, since agents combine deterministic code with nondeterministic model behaviour and need both conventional tests and evaluations that judge outputs statistically.
Learning outcomes
After completing this lesson, you will be able to:
- Distinguish unit and integration tests for agents.
- Design regression tests and automated evaluations.
- Apply techniques that keep tests stable despite nondeterminism.
Unit and integration tests
Unit tests cover deterministic components: tool functions, validators, middleware, routing rules, prompt template rendering and state transitions in workflows. Model calls are replaced with fakes or recorded responses, so unit tests are fast, cheap and repeatable. Integration tests run components against real services in a test environment: an agent calls real tools and models with test identities, workflows run end to end, MCP servers are called over their real transport, and authentication, network paths and contracts between agents are verified.
Unit test or integration test?
Regression tests and automated evaluations
Regression tests replay a fixed dataset of representative and previously failing cases and check that behaviour has not degraded. Because outputs vary, checks use assertions where possible (the right tool was called, the output matches a schema, the refund amount is correct) and evaluator scores otherwise.
Automated evaluations run evaluators over a dataset and compare aggregate scores and pass rates with thresholds: task adherence, tool call accuracy, groundedness, safety and custom judges. A test passes when, for example, at least 95% of cases pass groundedness and no safety case fails. Foundry evaluations can run from pipelines through the SDK or the Foundry evaluation GitHub Action and Azure DevOps task, with results recorded in the project for comparison.
The following table shows where each layer runs.
| Test layer | Runs on | Example check |
|---|---|---|
| Unit tests | Every commit and pull request | Refund tool rejects amounts above the limit |
| Fast evaluation (small dataset) | Every pull request | Task adherence pass rate at least 90% on 50 cases |
| Integration tests | After deployment to test | Agent authenticates to the HR API with OBO |
| Full regression and evaluation | Before promotion to acceptance and production | No regression beyond tolerance on 1,000 cases |
| Red-teaming scan | Before promotion; scheduled | Attack success rate below threshold |
Where each test layer runs
Handling nondeterminism
Nondeterministic outputs make naive assertions flaky. Practical techniques include asserting on structure and tool calls rather than wording, running each evaluation case more than once for critical paths, comparing aggregate scores with tolerances rather than exact values, using low temperature or fixed seeds where supported for test runs, and keeping the judge model and version fixed so that score changes come from the system under test.
Taming nondeterminism
Flaky tests
A team's CI pipeline compares each agent response with an expected answer string. About a third of runs fail even when nothing changed, and developers have started ignoring failures.
Exact string comparison is unsuitable for model output. The tests should assert on tool calls and structured fields, and use evaluators with thresholds over a dataset for wording and quality. Unit tests with fake model responses cover the deterministic code. This restores trust in the pipeline while still catching real regressions.
How are tests made reliable when model outputs vary?
Show answer
Assert on structure and tool calls rather than exact text, and compare aggregate evaluation scores with a tolerance.
Exam Tips
Remember for the exam
- Unit tests with fake models for deterministic code; integration tests against real services.
- Regression datasets plus evaluator thresholds act as quality gates.
- Assert on structure and tool calls; compare aggregate scores with tolerance.