Workflow File: .github/workflows/generate-tests.yml
This workflow analyzes commits for missing unit test coverage and automatically creates issues for Copilot to write the tests.
The Generate Tests workflow uses GitHub Copilot CLI to examine code changes and identify when new or modified code lacks corresponding unit tests. By automating test coverage analysis, teams can maintain high code quality without manual review overhead.
flowchart TD
A[Push to Repository] --> B{Source files changed?}
B -->|No| C[Skip workflow]
B -->|Yes| D[Install Copilot CLI]
D --> E[Load analyze-tests prompt]
E --> F[Copilot analyzes code changes]
F --> G{Tests missing?}
G -->|No| H[Exit - Coverage adequate]
G -->|Yes| I[Create GitHub Issue]
I --> J[Assign Copilot Coding Agent]
J --> K[Agent writes missing tests]
K --> L[PR created with tests]
- Triggers on every push (excluding non-source files)
- Installs Copilot CLI in the GitHub Actions runner
- Loads the analyze-for-tests prompt from
.github/prompts/analyze-for-tests.prompt.md - Copilot checks if new code has corresponding tests
- If tests are missing → Creates a GitHub issue and assigns Copilot
- Copilot Coding Agent then writes the missing tests
| Change Type | Example |
|---|---|
| New Functions/Methods | calculateDiscount(), validateOrder() |
| New Classes | WarehouseService, OrderProcessor |
| Modified Business Logic | Changed calculation formulas, updated validation |
| New API Endpoints | REST routes, GraphQL resolvers |
| Error Handling Paths | Try/catch blocks, error conditions |
| Change Type | Example |
|---|---|
| Configuration Files | tsconfig.json, package.json |
| Type Definitions Only | Interface declarations, type aliases |
| Test Files Themselves | *.test.ts, *.spec.ts |
| Documentation | README, comments, JSDoc |
| Static Assets | Images, fonts, CSS |
The workflow focuses on source code changes:
on:
push:
paths:
- 'api/src/**/*.ts'
- 'frontend/src/**/*.ts'
- 'frontend/src/**/*.tsx'
paths-ignore:
- '**/*.test.ts'
- '**/*.spec.ts'
- '**/*.d.ts'| Secret | Description |
|---|---|
COPILOT_CLI_TOKEN |
Personal Access Token with Copilot permissions |
The workflow uses a specialized prompt to guide Copilot's analysis:
Location: .github/prompts/analyze-for-tests.prompt.md
This prompt instructs Copilot to:
- Analyze the git diff for testable code changes
- Identify functions, classes, and methods without test coverage
- Evaluate if existing tests cover the modified code paths
- Create a detailed issue listing specific tests to write
When the workflow detects missing tests, it creates an issue like:
## 🧪 Unit Tests Needed
**Commit**: def5678
**Author**: @developer
### Code Requiring Tests
#### `api/src/routes/warehouse.ts`
- [ ] `POST /` - Create warehouse endpoint
- [ ] `GET /` - Get all warehouses
- [ ] `GET /:id` - Get warehouse by ID
- [ ] `GET /branch/:branchId` - Get warehouses by branch
- [ ] `PUT /:id` - Update warehouse
- [ ] `DELETE /:id` - Delete warehouse
### Test File Location
`api/src/routes/warehouse.test.ts`
### Testing Framework
Vitest with supertest for API testing
/assign @copilotThe Copilot Coding Agent follows established testing patterns in the codebase
import { describe, it, expect, beforeEach } from 'vitest';
import request from 'supertest';
import app from '../index';
describe('Warehouse Routes', () => {
describe('GET /api/warehouses', () => {
it('should return all warehouses', async () => {
const response = await request(app).get('/api/warehouses');
expect(response.status).toBe(200);
expect(Array.isArray(response.body)).toBe(true);
});
});
describe('GET /api/warehouses/:id', () => {
it('should return 404 for non-existent warehouse', async () => {
const response = await request(app).get('/api/warehouses/99999');
expect(response.status).toBe(404);
});
});
});- Verify the push includes files matching the
pathspatterns - Ensure changes are not exclusively in
paths-ignorepatterns - Check that the workflow file exists in the default branch
- Review the generate-tests prompt for coverage criteria
- Ensure the code changes are substantial enough to warrant tests
- Check workflow logs for the analysis output
- Verify existing test patterns in the codebase are consistent
- Check that the testing framework is correctly configured
- Review the prompt for framework-specific instructions
- Run tests locally to identify issues
- Check for missing imports or dependencies
- Verify mock data matches the expected schema