How GitHub Copilot Can Write Unit Tests for Your Code
A step-by-step beginner guide that shows you how to ask Copilot to generate test snippets, so you can automatically cover new functions with confidence.
By the end of this guide, you’ll be able to type a simple comment and let Copilot spin up a ready-to-run unit test for any function you create. A unit test is like a tiny, automatic quality check for a small piece of your code, ensuring it works as expected. This guide is aimed at anyone who writes a little bit of code – no testing specialist required.
- GitHub account – you'll need to sign in so Copilot can link to your editor.
- Visual Studio Code (VS Code) – this is a free, cross-platform program where you write code. It's often called a 'code editor'.
- GitHub Copilot subscription or free trial – the first 30 days are free for most users. A paid plan may be required for unlimited suggestions after this period.
- Internet connection – Copilot contacts GitHub’s servers to generate suggestions, so you'll need to be online.
- Node.js and npm (for JavaScript) – If you plan to test JavaScript code, you'll need Node.js (a program that runs JavaScript outside of a web browser) and its package manager, npm (a tool to install extra software 'packages' for Node.js). You can download them from the official Node.js website.
- Rough time – about 10-15 minutes from start to a passing test.
If all these items are ready, you’re good to go.
Install and enable Copilot in VS Code
Open Visual Studio Code (VS Code) on your computer. Look for the Extensions view, which is usually a square-shaped icon made of four smaller squares in the left-hand toolbar (it looks a bit like puzzle pieces). Click this icon. In the search box that appears, type "GitHub Copilot" and click the Install button next to the official GitHub Copilot entry. After the download finishes, a pop-up might ask you to sign in to GitHub. Click the link, log in with your GitHub account, and grant the permission the extension requests. When the sign-in completes, you'll see a tiny Copilot logo in the lower-right status bar (this is the strip at the bottom that shows things like file name and line number). Click that icon and toggle the switch so it reads "Enabled".
You'll know it worked when the Copilot icon in the status bar turns blue and, if you hover your mouse over it, the tooltip reads "GitHub Copilot enabled".

Write the function you want to protect
Create a new file for the programming language you’re working with. For example, you might choose utils.py for Python or utils.js for JavaScript. To do this, go to the top menu, select File > New File, then save the file with the appropriate extension (e.g., .py or .js). In the main editor area, type a simple function; keep it short for this example so Copilot can see the whole definition clearly. A function is a block of code designed to perform a specific task.
def add(a: int, b: int) -> int: return a + b
<div class="g-prompt"><span class="g-prompt__lbl">💬 Example</span><span class="g-prompt__box">(JavaScript): ```javascript</span></div>
function add(a, b) {
return a + b;
}
If it looks different: If your code doesn't get proper colouring, check the language mode in the bottom-right status bar of VS Code (it might say "Plain Text"). Click it and select the correct language (e.g., "Python" or "JavaScript"). You’ll know it worked when the function appears with proper syntax colouring (keywords in blue, strings in green, etc.) and no red squiggles indicating syntax errors.

Prompt Copilot to generate a unit test
Place your text cursor on a brand-new, empty line directly below the function you just wrote. Type a comment that tells Copilot what you need. A comment is a line of text in code that is ignored by the computer but helps humans understand what's happening. For Python, use # Write a unit test for add(); for JavaScript, use // Write a Jest test for add(). After typing your comment, press Enter. Copilot will instantly show a greyed-out suggestion that looks like code, but isn't yet part of your file. This is Copilot offering to help!
If it looks different: If no suggestion appears, wait a moment. Sometimes Copilot takes a second to think. Ensure Copilot is enabled (blue icon in status bar). If you still see nothing, try rephrasing your comment slightly, or saving the file (Ctrl+S).
You’ll know it worked when a block of test code appears in the editor as a grey suggestion. If the suggestion looks correct, press Tab to accept it; otherwise, press Esc to dismiss it and try a slightly different comment.

Run the test and see it pass
Now that you have your test code, it's time to run it. Open VS Code’s integrated terminal by selecting View > Terminal from the top menu (or press Ctrl+ on Windows/Linux, orCmd+`` on Mac). The terminal is a command-line interface where you can type commands for your computer. In the terminal, type the command that runs the test file you just generated. For Python, if your test file is namedtest_utils.py, run: python -m unittest test_utils.py. For JavaScript with Jest (a popular *test runner* or tool for running JavaScript tests), type npm test`.
If it looks different: If npm test doesn't work for JavaScript, it's probably because Jest isn't installed in your project yet. In the terminal, run npm install --save-dev jest (which means "install Jest and save it as a development dependency"), then retry npm test. For Python, if unittest isn't found, ensure Python is correctly installed and added to your system's PATH.
You’ll know it worked when the terminal prints a line that says OK (or shows only dots without a "FAIL" label), indicating all tests passed.

Tweak or add more test cases
Take a look at the test file Copilot produced. You'll likely see a test method or test case like test_positive_numbers inside a class (for Python) or a test() block (for JavaScript). To cover extra situations for your add function (for example, handling zero values, negative numbers, or very large numbers), copy an existing test method or block, paste it below, and change the input numbers and the expected result. A test case is a specific scenario you want to check. Save the file (Ctrl+S), then rerun the command from Step 4 in the terminal to confirm the new cases also pass.
def test_zero(self): self.assertEqual(add(0, 0), 0)
If it looks different: If you add new tests and the terminal still says the same number of tests passed, ensure you saved the file after making changes (`Ctrl+S`). The test runner only sees the saved version of your code.
You’ll know it worked when the terminal again prints `OK` (or all green dots) and the total number of tests reported matches the total number of test methods you now have in your file. Copilot can draft tests, but it can't run them or interpret the results for you.

- Forgetting to save your function before prompting Copilot. Copilot only reads the saved file, so unsaved changes won’t be considered. Fix: Press
Ctrl+Safter writing your function and before typing the comment for Copilot. - Running the JavaScript test without Jest installed. Python’s
unittestis built-in, but JavaScript needs Jest (or another test runner) installed first. Fix: Runnpm install --save-dev jestin your terminal, then retry thenpm testcommand. - Accepting a test that imports a library you don’t have. Copilot sometimes suggests
import pytestfor Python even if you haven’t installed it. Fix: Either install the missing library (e.g.,pip install pytestfor Python) or edit the import line to use the built-inunittestmodule instead.
Open VS Code, create a new file called add.py, paste the add function from Step 2, add the comment # Write a unit test for add(), hit Enter, then press Tab to accept Copilot’s suggestion. You’ll have a complete test file ready to run in under two minutes. Happy testing!
✦ Original step-by-step guide by AI World Co.'s AI editorial team. Written in plain language, reviewed for accuracy.
← Back to all stories