Copilot Unit Test Generator Extension (Training Version)
A simple VS Code extension that generates unit tests using GitHub Copilot's Language Model API. Perfect for learning how to build VS Code extensions with AI capabilities.
Features
- 🤖 AI-Powered Test Generation: Uses GitHub Copilot's GPT-4 model to generate comprehensive unit tests
- 🌐 25+ Languages Supported: JavaScript, TypeScript, Python, Java, C#, Go, Ruby, PHP, Rust, C/C++, Swift, Kotlin, Scala, R, Dart, Elixir, Haskell, Julia, Perl, Groovy, Lua, Clojure, and more!
- 🧪 Framework Auto-Detection: Automatically selects the right test framework (Jest, pytest, JUnit, xUnit, RSpec, PHPUnit, etc.)
- 🎯 Context Menu Integration: Right-click on selected code to generate tests
- 📂 Smart File Creation: Automatically creates test files with language-specific naming conventions
- ⚡ Simple & Focused: Minimal implementation perfect for learning
Prerequisites
Before you begin, ensure you have:
- Node.js (v18 or higher) - Download here
- Visual Studio Code (v1.85.0 or higher)
- GitHub Copilot subscription and extension installed
- npm or yarn package manager
Installation & Setup
Step 1: Install Dependencies
Open a terminal in the extension directory and run:
npm install
This installs the required VS Code API types.
Step 2: Install VSCE (VS Code Extension Manager)
npm install -g @vscode/vsce
Testing the Extension Locally
Method 1: Press F5 (Recommended for Development)
- Open the extension folder in VS Code
- Press F5 or go to Run > Start Debugging
- A new VS Code window will open with your extension loaded
- Open any code file and select some code
- Right-click and choose "Generate Unit Tests with Copilot"
Method 2: Using Command Palette
- In the Extension Development Host window
- Press Ctrl+Shift+P (or Cmd+Shift+P on Mac)
- Type:
Generate Unit Tests with Copilot
- Press Enter
Test with Sample Code
Create a test file sample.js:
function add(a, b) {
return a + b;
}
function multiply(a, b) {
return a * b;
}
function divide(a, b) {
if (b === 0) {
throw new Error('Cannot divide by zero');
}
return a / b;
}
module.exports = { add, multiply, divide };
- Select the functions
- Right-click → Generate Unit Tests with Copilot
- Test file
sample.test.js will be created automatically
Packaging the Extension
Create a .vsix Package
vsce package
This creates a .vsix file (e.g., copilot-test-generator-simple-0.1.0.vsix)
Install the .vsix Locally
code --install-extension copilot-test-generator-simple-0.1.0.vsix
Or manually:
- Open VS Code
- Go to Extensions (Ctrl+Shift+X)
- Click the "..." menu at the top
- Choose "Install from VSIX..."
- Select your
.vsix file
Publishing to VS Code Marketplace
Step 1: Create a Publisher Account
- Go to Visual Studio Marketplace Publisher Management
- Sign in with your Microsoft account
- Create a publisher (e.g., "mycompany")
Step 2: Get a Personal Access Token (PAT)
- Go to Azure DevOps
- Click on User Settings (top right) → Personal Access Tokens
- Create new token with:
- Name: VS Code Extensions
- Organization: All accessible organizations
- Scopes: Marketplace → Manage
- Expiration: 90 days (or custom)
- Copy the token immediately (you won't see it again!)
Step 3: Login to VSCE
vsce login your-publisher-name
Enter your Personal Access Token when prompted.
Step 4: Update package.json Publisher
In package.json, change:
"publisher": "your-actual-publisher-name"
Step 5: Publish
vsce publish
Or publish with version bump:
vsce publish minor # 0.1.0 → 0.2.0
vsce publish patch # 0.1.0 → 0.1.1
vsce publish major # 0.1.0 → 1.0.0
Step 6: Verify Publication
Visit: https://marketplace.visualstudio.com/items?itemName=your-publisher-name.copilot-test-generator-simple
Supported Languages & Frameworks
| Language |
Test Framework |
Test File Pattern |
| JavaScript/TypeScript |
Jest |
.test.js, .test.ts |
| React (JSX/TSX) |
Jest + React Testing Library |
.test.jsx, .test.tsx |
| Python |
pytest |
_test.py |
| Java |
JUnit 5 |
Test.java |
| C# |
xUnit |
Tests.cs |
| Go |
testing + testify |
_test.go |
| Ruby |
RSpec |
_spec.rb |
| PHP |
PHPUnit |
Test.php |
| Rust |
Built-in test framework |
.rs |
| C/C++ |
Google Test |
_test.cpp |
| Swift |
XCTest |
Tests.swift |
| Kotlin |
JUnit 5 |
Test.kt |
| Scala |
ScalaTest |
Spec.scala |
| R |
testthat |
_test.R |
| Dart/Flutter |
flutter_test |
_test.dart |
| Elixir |
ExUnit |
_test.exs |
| Haskell |
HSpec |
Spec.hs |
| Julia |
Test.jl |
_test.jl |
| Perl |
Test::More |
.t |
| Groovy |
Spock |
Test.groovy |
| Lua |
busted |
_test.lua |
| Clojure |
clojure.test |
_test.clj |
How It Works
Architecture Overview
User selects code
↓
Right-click menu
↓
Extension activates
↓
Detects language & framework
↓
Calls Copilot Language Model API (vscode.lm)
↓
Generates test code with proper imports
↓
Creates test file (language-specific naming)
↓
Opens in editor
Key Code Components
Command Registration (extension.js):
- Registers
copilot-test-generator.generateTests command
- Adds context menu item for selected code
Copilot API Integration:
- Uses
vscode.lm.selectChatModels() to access Copilot
- Sends structured prompts for test generation
- Handles streaming responses
Test File Creation:
- Determines test file naming based on language
- Creates files in the same directory as source
- Auto-opens generated tests
Customization Ideas
Want to enhance this extension? Try:
Add Configuration Settings:
- Test framework selection
- Custom test file location
- Coverage requirements
Support More Languages:
- Add language detection
- Framework-specific templates
Enhance Prompts:
- Include project context
- Add specific test patterns
- Request specific coverage
Add Commands:
- Generate tests for entire file
- Update existing tests
- Generate mocks
Troubleshooting
"No Copilot model available"
- Ensure GitHub Copilot extension is installed and active
- Check your Copilot subscription is valid
- Sign in to GitHub in VS Code
Extension doesn't activate
- Check VS Code version (must be 1.85.0+)
- Verify
activationEvents in package.json
- Check Output panel (Help → Toggle Developer Tools → Console)
Tests not generating
- Check VS Code Output panel for errors
- Ensure you have code selected
- Verify language is supported
VSCE publish fails
- Check publisher name matches your account
- Verify PAT token has Marketplace→Manage scope
- Ensure version number increased
Learning Resources
Project Structure
unit_test_extension/
├── extension.js # Main extension logic
├── package.json # Extension manifest
├── README.md # This file
└── .vscodeignore # Files to exclude from package
License
MIT - Feel free to use this for learning and training purposes!
Next Steps
- ✅ Test the extension locally (F5)
- ✅ Try with different programming languages
- ✅ Package the extension (.vsix)
- ✅ Publish to marketplace (optional)
- 🚀 Enhance with your own features!
Happy Coding! 🎉
Built with ❤️ using GitHub Copilot's Language Model API