Pcornel
Browse topics
Options
Recent
Favorites
Automation · Technical reference

Mocks

Direct answer

Mocks replace external dependencies with controlled test doubles so a unit or narrow component can exercise specified interactions, errors and edge cases deterministically.

When to use this

Use it when designing, executing or reviewing automation tests around Mocks. Keep requirement/risk, test oracle, fixture/environment identity, exercised scope, raw observations and target/workload outcome as separate proof boundaries.

First 30 seconds

1. Mocks replace external systems in tests.

2. Mock at boundaries (APIs, SSH).

3. Recorded responses for realism.

4. Don't over-mock logic.

5. Contract tests to keep mocks honest.

Operational verification

test → mock dependency → assertions

Context & applicabilityCommon mistakes · platform/version boundaries
Platform / version applicability
automation testing
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

Platformautomation testing · Mocks owns generic test-double discipline; language frameworks and integration branches own concrete mocking libraries and live-contract checks.
Scope / prerequisiteUse it when designing, executing or reviewing automation tests around Mocks. Keep requirement/risk, test oracle, fixtur…
What next?3 suggested next steps
Evidence1
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

4 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

Capture the concrete output, symptom, packet, log or counter before changing anything.

Step 1 Operational evidence path Verify boundary

unit/component → mocked boundary contract → controlled responses/failures → interaction/output assertions → contract verification against real dependency

Healthy evidence: mocks are narrow, contract-aligned and complemented by real integration tests for important external behavior.

Step 2 Verification path Verify boundary

When testing Mocks, include the cases that usually go wrong: shared mutable fixtures, flaky timing, hidden dependencies, assertion-free smoke checks, production-only assumptions and treating a passing fixture as universal correctness.

Step 3 Failure modes and next checks Narrow the fault

Suspicious evidence: mocks reproduce implementation assumptions rather than the external contract, allowing tests to pass while real integrations fail.

Step 4 Common mistakes Narrow the fault
  • Mocks that hide real behaviour.
Evidence references

Evidence · 1 on this Topic

Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.

observation · 1
Key points
  • Mock contract ≠ real dependency behavior.
  • Mock call expectation met ≠ downstream side effect occurred.
  • Detailed mock interactions ≠ user-visible behavior correct.
  • Simulated failure ≠ real timeout/retry semantics reproduced exactly.
Key checks
  • python3 -c 'from unittest import mock' — mocking library
  • Mock in Pester — PowerShell
  • Recorded API responses (vcr/responses) — replay
ReferenceWhat it is1 section · depth on demand

Core model

Mock only the boundary not under test, model the external contract rather than internal implementation, assert meaningful requests/interactions, simulate representative failures, and back critical mock assumptions with real contract or integration tests.

ReferenceImportant terms & boundaries2 sections · depth on demand

What this shows and what it does not

Mocks show local behavior against the simulated contract only; they cannot show real protocol quirks, timing, authorization or side effects.

In depth

Mocks replace real dependencies — APIs, devices, databases — with test doubles that return controlled responses. They make unit tests fast and deterministic and let you test error handling (timeouts, 500 errors) that is hard to trigger for real.

import responses, requests

@responses.activate
def test_handles_rate_limit():
    responses.get("https://api.example.com/devices", status=429, headers={"Retry-After": "1"})
    responses.get("https://api.example.com/devices", json={"results": []})
    assert fetch_devices() == []

Mocks only prove your code handles the responses you imagined; combine them with integration tests against the real API.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Technical verification history

Verification history & basis

When and against what this Topic was checked. Later text edits do not count as a new check.

2026-09-14 · vendor docautomation testing current

Authoritative/upstream documentation reviewed for bounded claims in Testing > Mocks. Documentation verification only; this is not observed lab or production Evidence.

Basis: https://docs.pytest.org/en/stable/ · confidence 94%
Terminology layer

Terms & aliases

Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.

Aliases that resolve here
mocks automation testingtest doubles automationmock external dependency
Embedded reference data

Quick lookup tables

Mocks testing proof-boundary map

Fast criteria/scope, execution and effective-outcome proof boundaries for Mocks.

BoundaryHealthy evidenceSuspicious evidence
Scope / oracleRequirement, case, expected result and exercised scope are explicitA generic green status exists without a trustworthy oracle or coverage boundary
Fixture / environmentData, dependencies and environment versions are reproducibleShared state, floating versions or stale fixtures make results non-reproducible
Effective outcomeAuthoritative state/read-back and service-relevant evidence match the expected resultTool/test/validator success substitutes for effective target or workload proof
Limits / next actionCoverage limits, unknowns and failures drive the next narrower checkUnknown evidence becomes pass, retries hide failures or broad success is inferred from one check
Confirmation

Confirm action

Continue?