Third Party API Integration Test¶
Write Go integration tests for real third-party API calls using explicit run gates, predictable safeguards, and strong contract assertions.
Scope¶
- Validate external API integration end-to-end with real config and real client.
- Keep tests opt-in by default so normal CI/unit workflows are not blocked.
- Follow repository style:
testify/require, clear skip conditions, bounded timeout. - Apply to any third-party API integration.
- Treat vendor examples (MCS/USS/etc.) as templates, not scope limits.
Scope Validation Gate¶
Confirm the task targets a third-party vendor API: - Third-party HTTP or gRPC API client → proceed - Internal service/handler (own HTTP server) → redirect to $api-integration-test, STOP - Pure unit test → redirect to $unit-test, STOP - Full end-to-end browser journey → out of scope, inform user, STOP
Hard stop: If the target is not a third-party vendor API, the entire remaining workflow is skipped. Output only: (1) scope verdict, (2) recommended skill or approach, (3) reason. Do NOT generate test code, do NOT proceed to subsequent gates.
Required Pattern¶
- Keep file name as
<client>_integration_test.goin the same package directory. - Add both build constraints at file top (for backward compatibility):
//go:build integration(Go 1.17+)// +build integration(Go <1.17 compat)- Add explicit run gate env var (example:
THIRDPARTY_INTEGRATION=1or vendor-specific gate), otherwiset.Skip(...). - Validate required runtime env vars up front (for example:
ENV,CONFIG_DIR, target IDs/label IDs). - Block production by default:
- if
ENVresolves toprod/production,t.SkipunlessINTEGRATION_ALLOW_PROD=1. - Parse and validate env var payloads:
- Always
strings.TrimSpacebefore comparison or use - Use
strconv.ParseIntfor numeric IDs - Split comma-separated lists and validate each element
- Log parsed values at
t.Logflevel for debugging - Load runtime config via project config loader (
config.MustLoad()). - Build real third-party client with production code path.
- Use
context.WithTimeout(...)to prevent hanging requests. - Use bounded retry policy only when justified:
- default: no retry
- if enabled: max 1-2 retries, bounded backoff, no infinite loop
- Execute real API call and assert both:
- protocol-level contract (status/code/required response fields)
- business-level invariant (identifier consistency, semantic constraints)
- For expected failure paths, assert explicit error type/code (not only
require.Error). - Define test data lifecycle explicitly:
- setup source, idempotency key strategy, cleanup or safe reuse policy.
Configuration Gate (Mandatory)¶
Before generating or updating test code, apply the shared gate in: - references/common-integration-gate.md
Vendor-Specific Safety Additions¶
- Prefer idempotent endpoints for default integration flows.
- For mutation endpoints, require dedicated test tenant/account and explicit opt-in gate.
- Keep retry policy bounded; avoid hidden infinite retries in tests.
- Validate rate-limit behavior when relevant:
- Assert rate-limit response headers when available (
X-RateLimit-Remaining,Retry-After) - If test triggers rate-limit, classify failure as "rate-limit" not "contract"
- Never log secrets/tokens in assertions or failure output.
- Refuse production environment by default.
Safety Rules¶
- Use dedicated test tenant/account identifiers only.
- Prefer idempotent or low-risk API operations.
- Never hardcode secrets/tokens in test source.
- Skip test when required env vars are missing instead of failing local pipelines.
- Keep timeout strict (for example 10-30s) and avoid unbounded retries.
- Refuse
ENV=produnless explicit override is provided.
Execution¶
Use vendor-specific run commands documented in: - references/vendor-examples.md
Output Contract¶
Use the shared output contract in: - references/common-output-contract.md
References (Load Selectively)¶
- Always read:
references/common-integration-gate.md— gate design and skip message quality. Shared with$api-integration-test. - Always read:
references/common-output-contract.md— structured report format. Shared with$api-integration-test. - Read if authoring new tests or triaging failures:
references/checklists.md - Read if no vendor pattern exists in the repo:
references/vendor-examples.md— generic template and run commands