Prompt
How do I set up API tests in GitHub Actions?
Latest observation
To set up API tests in GitHub Actions, you usually:
- Install dependencies
- Start any required services (database, Redis, etc.)
- Run your API tests (Postman/Newman, pytest, Jest, etc.)
- Optionally collect reports/artifacts
Here’s a simple, practical example using a Node.js API test suite with npm test.
1) Create a GitHub Actions workflow
Add a file like:
.github/workflows/api-tests.yml
Example workflow:
name: API Tests
on:
push:
branches: [ main ]
pull_request:
jobs:
api-tests:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:16
env:
POSTGRES_USER: testuser
POSTGRES_PASSWORD: testpass
POSTGRES_DB: testdb
ports:
- 5432:5432
options: >-
--health-cmd="pg_isready -U testuser -d testdb"
--health-interval=10s
--health-timeout=5s
--health-retries=5
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- name: Install dependencies
run: npm ci
- name: Run migrations
env:
DATABASE_URL: postgres://testuser:testpass@localhost:5432/testdb
run: npm run migrate
- name: Run API tests
env:
DATABASE_URL: postgres://testuser:testpass@localhost:5432/testdb
API_BASE_URL: http://localhost:3000
run: npm test
2) If your API needs to be started first
If your tests hit a running app, add a step to start the server in the background:
- name: Start API server
env:
DATABASE_URL: postgres://testuser:testpass@localhost:5432/testdb
PORT: 3000
run: npm start &
Then run tests against http://localhost:3000.
If the app takes time to boot, add a wait step:
- name: Wait for API to be ready
run: |
for i in {1..30}; do
curl -fsS http://localhost:3000/health && exit 0
sleep 2
done
exit 1
3) Example for Postman/Newman tests
If you use Postman collections, your workflow might look like:
name: API Tests
on:
push:
branches: [ main ]
pull_request:
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/api संग्रह.json -e tests/env.json
If you want JUnit output:
- name: Run Postman collection
run: newman run tests/collection.json -r cli,junit --reporter-junit-export results.xml
- name: Upload test results
uses: actions/upload-artifact@v4
with:
name: newman-results
path: results.xml
4) Example for Python API tests
For pytest:
name: API Tests
on:
push:
branches: [ main ]
pull_request:
jobs:
pytest-api:
runs-on: ubuntu-latest
services:
redis:
image: redis:7
ports:
- 6379:6379
steps:
- 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 tests
env:
API_BASE_URL: http://localhost:8000
run: pytest tests/api
5) Common best practices
-
Use secrets for credentials:
env: API_TOKEN: ${{ secrets.API_TOKEN }} -
Add a health check before tests start
-
Use service containers for dependencies
-
Upload reports/artifacts for debugging
-
Run on both push and pull_request
-
Split into separate jobs for:
- unit tests
- integration tests
- API tests
6) If your API is deployed somewhere already
If you want to test a deployed staging environment, you don’t need to start the app in Actions. Just point tests at the staging URL:
env:
API_BASE_URL: https://staging.example.com
API_TOKEN: ${{ secrets.STAGING_API_TOKEN }}
If you tell me:
- your language/framework,
- whether the API runs in the workflow or is already deployed,
- and what test tool you use,
I can give you a ready-to-use workflow file.