Step-by-step guide
🤖 GitHub Copilot
GitHub
📘 Step-by-step guide 📘 GitHub · GitHub Copilotbeginner 💼 Business

How GitHub Copilot Can Write Unit Tests for Your Code

A step‑by‑step beginner guide that shows you how to let Copilot generate test snippets, so you can automatically cover new functions with confidence.

Hook: By the end of this guide you’ll be able to ask Copilot to spin up a unit test for any new function you write, giving you fast safety‑net code without needing a testing specialist. It’s perfect for busy professionals who want reliable code quickly.

✅ Before you start
  • GitHub account – you’ll need to sign in to the Copilot service.
  • Visual Studio Code (VS Code) – the free editor where Copilot lives.
  • Copilot subscription or trial – the first 30 days are free for most users.

If you have these three items, you’re ready to go.

1

Install and enable Copilot in VS Code

  1. Open VS Code.
  2. In the left‑hand Extensions pane (the square‑icon view), search for “GitHub Copilot”. Click Install.
  3. After installation, a pop‑up will ask you to sign in to GitHub – follow the link, log in, and authorise the extension.
  4. When the sign‑in is complete, you’ll see a small Copilot icon in the status bar (bottom‑right). Click it and toggle the switch to Enabled.
💬 Try typing this*Example:* You should now see “Copilot: On” in the corner of the editor window.
2

Write the function you want to protect

  1. Create a new file (e.g., utils.py for Python or utils.js for JavaScript).
  2. Write a simple function. For illustration, let’s add a function that returns the sum of two numbers.
💬 Try typing this*Python example:*
def add(a: int, b: int) -> int:
    return a + b
💬 Try typing this*JavaScript example:*
function add(a, b) {
    return a + b;
}

Why? Copilot learns from the code right in front of it; the clearer the function, the better the test suggestion.

3

Prompt Copilot to generate a unit test

  1. Place the cursor on a new line below the function.
  2. Type a comment that tells Copilot what you need, such as # Write a unit test for add() (Python) or // Write a Jest test for add() (JavaScript).
  3. Press Enter. Copilot will instantly suggest a test block. The suggestion appears as greyed‑out text; press Tab to accept it, or Esc to reject and try another prompt.
💬 Try typing this*Typical suggestion (Python):*
import unittest
from utils import add

class TestAdd(unittest.TestCase):
    def test_positive_numbers(self):
        self.assertEqual(add(2, 3), 5)

    def test_negative_numbers(self):
        self.assertEqual(add(-1, -4), -5)

if __name__ == '__main__':
    unittest.main()

What’s happening? A unit test is a tiny piece of code that checks one small part of your program – like a quality‑control checkpoint for a single function.

4

Run the test and see it pass

  1. Open a terminal inside VS Code (Ctrl+ `).
  2. For Python, run: python -m unittest utils_test.py (or whatever you named the test file).
    For JavaScript with Jest, run: npm test (make sure Jest is installed; you can add it with npm i -D jest).
  3. If everything is green, Copilot’s suggestion worked! If a test fails, read the error message – it will tell you which expectation didn’t match.
💬 Try typing this*Example output (Python):*
..
----------------------------------------------------------------------
Ran 2 tests in 0.001s

OK
5

Tweak or add more test cases

  1. Look at the generated test file. You can add extra scenarios (e.g., testing with zero or large numbers) by copying an existing test method and changing the inputs.
  2. Save the file and re‑run the command from Step 4 to confirm the new cases also pass.
💬 Try typing this*Extra case example (Python):*
def test_zero(self):
    self.assertEqual(add(0, 0), 0)

Why tweak? Copilot gives a solid starter, but you may need edge‑case coverage that matches your real‑world data.

⚠️ Common mistakes
  • Forgetting to save the file before asking for a suggestion. Copilot only sees the latest saved content.
  • Running the test before installing the test runner (e.g., unittest is built‑in for Python, but Jest must be added for JavaScript).
  • Accepting a test that uses undefined imports. If Copilot suggests import pytest but you don’t have it, install it with pip install pytest or adjust the import.
🚀 Try it now

Open VS Code, create a file called add.py, paste the add function from Step 2, add the comment # Write a unit test for add(), hit Enter, and then press Tab to accept Copilot’s test suggestion. You’ll have a complete test file in less than 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