Prompt
How do I run API tests from GitHub Actions?
Latest observation
Oct 6, 2026
OpenAI APIWeb search: off
A common way is to run your API tests as part of a GitHub Actions workflow on every push or pull request.
1) Put your tests in the repo
Typically you’ll have:
- API test code in a folder like
tests/ - a test runner like:
pytestfor Pythonjest/supertestfor Node.jsnewmanfor Postman collectionscurl+ shell scripts for simple checks
2) Create a workflow file
Add a file like:
.github/workflows/api-tests.yml
3) Example: Python + pytest
If your API tests are in Python:
name: API Tests
on:
push:
branches: [main]
pull_request:
jobs:
api-tests:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:16
env:
POSTGRES_USER: test
POSTGRES_PASSWORD: test
POSTGRES_DB: testdb
ports:
- 5432:5432
options: >-
--health-cmd="pg_isready -U test"
--health-interval=10s
--health-timeout=5s
--health-retries=5
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run API tests
env:
API_BASE_URL: http://localhost:8000
run: |
pytest tests/api
4) Example: Node.js + Jest
If your API tests are Node-based:
name: API Tests
on:
push:
branches: [main]
pull_request:
jobs:
api-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
- run: npm ci
- name: Run API tests
env:
API_BASE_URL: https://staging.example.com
run: npm test
5) If your API needs to be started in CI
If the API is part of the same repo, you may need to start it before tests:
- name: Start API
run: |
npm run start &
sleep 10
Better is to wait for a health endpoint rather than using a fixed sleep.
Example:
- name: Wait for API
run: |
until curl -sf http://localhost:3000/health; do
echo "Waiting for API..."
sleep 2
done
6) Use secrets for auth tokens
If your tests need credentials, store them in GitHub Secrets and reference them:
env:
API_TOKEN: ${{ secrets.API_TOKEN }}
7) Postman/Newman example
If you use Postman collections:
name: API Tests
on:
push:
branches: [main]
jobs:
newman-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Newman
run: npm install -g newman
- name: Run Postman collection
run: newman run tests/postman/collection.json
Best practices
- Run tests on
pushandpull_request - Use environment variables for base URLs and secrets
- Prefer a health check over arbitrary
sleep - If possible, run tests against a disposable test environment
- Upload test reports/artifacts if you want CI visibility
If you want, I can give you a workflow tailored to your stack (Python, Node, Postman, Java, etc.) and whether your API runs inside the workflow or against a deployed staging server.