Skip to content
| Marketplace
Sign in
Visual Studio Code>Machine Learning>Copilot Unit Test Generator (Training)New to Visual Studio Code? Get it now.
Copilot Unit Test Generator (Training)

Copilot Unit Test Generator (Training)

GenTC's123

|
3 installs
| (0) | Free
Multi-language unit test generator using GitHub Copilot Language Model API - supports 25+ languages
Installation
Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter.
Copied to clipboard
More Info

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:

  1. Node.js (v18 or higher) - Download here
  2. Visual Studio Code (v1.85.0 or higher)
  3. GitHub Copilot subscription and extension installed
  4. 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)

  1. Open the extension folder in VS Code
  2. Press F5 or go to Run > Start Debugging
  3. A new VS Code window will open with your extension loaded
  4. Open any code file and select some code
  5. Right-click and choose "Generate Unit Tests with Copilot"

Method 2: Using Command Palette

  1. In the Extension Development Host window
  2. Press Ctrl+Shift+P (or Cmd+Shift+P on Mac)
  3. Type: Generate Unit Tests with Copilot
  4. 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 };
  1. Select the functions
  2. Right-click → Generate Unit Tests with Copilot
  3. 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:

  1. Open VS Code
  2. Go to Extensions (Ctrl+Shift+X)
  3. Click the "..." menu at the top
  4. Choose "Install from VSIX..."
  5. Select your .vsix file

Publishing to VS Code Marketplace

Step 1: Create a Publisher Account

  1. Go to Visual Studio Marketplace Publisher Management
  2. Sign in with your Microsoft account
  3. Create a publisher (e.g., "mycompany")

Step 2: Get a Personal Access Token (PAT)

  1. Go to Azure DevOps
  2. Click on User Settings (top right) → Personal Access Tokens
  3. Create new token with:
    • Name: VS Code Extensions
    • Organization: All accessible organizations
    • Scopes: Marketplace → Manage
    • Expiration: 90 days (or custom)
  4. 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

  1. Command Registration (extension.js):

    • Registers copilot-test-generator.generateTests command
    • Adds context menu item for selected code
  2. Copilot API Integration:

    • Uses vscode.lm.selectChatModels() to access Copilot
    • Sends structured prompts for test generation
    • Handles streaming responses
  3. 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:

  1. Add Configuration Settings:

    • Test framework selection
    • Custom test file location
    • Coverage requirements
  2. Support More Languages:

    • Add language detection
    • Framework-specific templates
  3. Enhance Prompts:

    • Include project context
    • Add specific test patterns
    • Request specific coverage
  4. 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

  • VS Code Extension API
  • Language Model API
  • Publishing Extensions
  • Extension Samples

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

  1. ✅ Test the extension locally (F5)
  2. ✅ Try with different programming languages
  3. ✅ Package the extension (.vsix)
  4. ✅ Publish to marketplace (optional)
  5. 🚀 Enhance with your own features!

Happy Coding! 🎉

Built with ❤️ using GitHub Copilot's Language Model API

  • Contact us
  • Jobs
  • Privacy
  • Manage cookies
  • Terms of use
  • Trademarks
© 2025 Microsoft