How to Set Up GitHub Actions for Automatic Testing
Overview
GitHub Actions provides built-in CI/CD capabilities for automating your testing workflow. This guide walks you through setting up automatic test execution on every push and pull request.
Prerequisites
- GitHub repository
- Existing test suite (npm test, pytest, etc.)
- Basic familiarity with YAML
Step 1: Create Workflow Directory
In your repository root, create the workflows directory:
mkdir -p .github/workflows
This is where GitHub Actions looks for workflow definitions.
Step 2: Create Workflow File
Create .github/workflows/test.yml:
name: Run Tests
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main ]
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18.x, 20.x]
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Run tests
run: npm test
- name: Upload coverage reports
uses: codecov/codecov-action@v3
if: matrix.node-version == '20.x'
with:
file: ./coverage/coverage-final.json
Step 3: Customize for Your Tech Stack
For Python Projects
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Run tests
run: pytest
For Go Projects
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.21'
- name: Run tests
run: go test -v ./...
Step 4: Add Status Badge to README
Add this markdown to your README.md:

Replace USERNAME and REPO with your values.
Step 5: Commit and Push
git add .github/workflows/test.yml
git commit -m "Add GitHub Actions testing workflow"
git push origin main
Step 6: Verify Execution
- Navigate to your repository on GitHub
- Click the Actions tab
- You should see your workflow running
- Click on the workflow run to see details
Advanced Configuration
Run on Schedule
Add to the on: section:
schedule:
- cron: '0 2 * * *' # Run at 2 AM daily
Cache Dependencies
Already included in the example above with:
cache: 'npm'
This speeds up workflow runs significantly.
Parallel Jobs
Test multiple configurations simultaneously using matrix strategy (shown in example).
Troubleshooting
Tests pass locally but fail in Actions
Common causes:
- Environment variables not set
- Different Node.js/Python versions
- OS-specific dependencies
Solution: Add debugging steps:
- name: Debug environment
run: |
node --version
npm --version
echo $PATH
Workflow doesn’t trigger
Check:
- Workflow file is in
.github/workflows/ - YAML syntax is valid (use a validator)
- Branch names match your
on:configuration
Best Practices
- Use
npm ciinstead ofnpm installfor faster, more reliable installs - Cache dependencies to reduce build time
- Set up branch protection requiring tests to pass before merge
- Upload artifacts for test reports and coverage
- Use secrets for API keys and credentials
Next Steps
- Add code coverage reporting (Codecov, Coveralls)
- Set up deployment workflows
- Add linting and formatting checks
- Implement matrix testing across OS platforms
Resources
Advertisement