Prompt
How can I automate smoke tests for my API after deploys?
Latest observation
A good way to automate API smoke tests after deploys is to keep them small, fast, and deployment-gated: after a new build is deployed, run a handful of “does the API basically work?” checks against the deployed environment and fail the pipeline if any fail.
1) Decide what a smoke test should cover
Smoke tests should verify only the most critical paths, for example:
- Service is up:
GET /healthorGET /status - Auth works: obtain token or use a test token
- One or two core endpoints respond correctly
- A minimal write/read flow if your API is stateful
- Basic dependency checks if relevant (DB-backed endpoint, cache, etc.)
Keep them short enough to run in seconds, not minutes.
2) Run them against the deployed environment
The key is to run tests after deployment and point them to the actual deployed base URL.
Typical environments:
staging- ephemeral preview environment
- production canary or post-deploy verification endpoint
Use environment variables like:
API_BASE_URL=https://staging.example.comAPI_TOKEN=...
3) Implement them in a simple test runner
Common choices:
- Postman/Newman
- pytest + requests
- JUnit/TestNG
- Playwright/API testing
- k6 for lightweight HTTP checks
- Bash + curl for very simple setups
Example with pytest
import os
import requests
BASE_URL = os.environ["API_BASE_URL"]
TOKEN = os.environ["API_TOKEN"]
def auth_headers():
return {"Authorization": f"Bearer {TOKEN}"}
def test_health():
r = requests.get(f"{BASE_URL}/health", timeout=5)
assert r.status_code == 200
assert r.json()["status"] == "ok"
def test_list_items():
r = requests.get(f"{BASE_URL}/items", headers=auth_headers(), timeout=5)
assert r.status_code == 200
assert isinstance(r.json(), list)
4) Wire it into your CI/CD pipeline
After the deploy step, add a smoke-test job.
Example GitHub Actions
name: Deploy and Smoke Test
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Deploy
run: ./deploy.sh
smoke-test:
runs-on: ubuntu-latest
needs: deploy
steps:
- uses: actions/checkout@v4
- name: Install deps
run: pip install -r requirements.txt
- name: Run smoke tests
env:
API_BASE_URL: https://staging.example.com
API_TOKEN: ${{ secrets.API_TOKEN }}
run: pytest tests/smoke -q
If deploy and smoke test are in the same workflow, the pipeline can fail fast and alert you immediately.
5) Use deployment-specific gate behavior
Depending on your deployment strategy:
- Blue/green: run smoke tests on the new environment before switching traffic
- Canary: deploy to a small slice, then run smoke tests before full rollout
- Rolling: run smoke tests once rollout completes
- Preview environments: run smoke tests on every PR deployment
This is especially useful to catch config or migration issues before users see them.
6) Make tests reliable
Smoke tests should be stable and low-noise:
- Use test data that won’t collide with real users
- Prefer idempotent operations
- Add timeouts and retries only where appropriate
- Avoid brittle assertions on dynamic values
- Clean up any resources you create
- Don’t depend on external services unless necessary
7) Report failures clearly
If a smoke test fails, you want immediate, actionable feedback:
- Which endpoint failed
- HTTP status code
- Response body snippet
- Correlation/request ID if available
- Which deployment version was tested
This helps distinguish app issues from deployment issues quickly.
8) Optional: add post-deploy monitoring checks too
Smoke tests are great, but combine them with:
- synthetic monitoring
- uptime checks
- alerting on error rates/latency
- log/trace inspection after rollout
Smoke tests verify “it works now”; monitoring tells you “it keeps working.”
Recommended minimal setup
If you want the simplest practical solution:
- Add a
/healthendpoint - Create 3–5 API smoke tests with
pytestor Newman - Store base URL and tokens as CI secrets/env vars
- Run the suite automatically after deploy
- Fail the deployment pipeline if tests fail
If you want, I can show you:
- a GitHub Actions example,
- a GitLab CI example,
- a Postman/Newman setup,
- or a pytest-based smoke test template for your API.