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.
- 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.
Install and enable Copilot in VS Code
- Open VS Code.
- In the left‑hand Extensions pane (the square‑icon view), search for “GitHub Copilot”. Click Install.
- After installation, a pop‑up will ask you to sign in to GitHub – follow the link, log in, and authorise the extension.
- 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.
Write the function you want to protect
- Create a new file (e.g.,
utils.pyfor Python orutils.jsfor JavaScript). - Write a simple function. For illustration, let’s add a function that returns the sum of two numbers.
def add(a: int, b: int) -> int:
return a + b
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.
Prompt Copilot to generate a unit test
- Place the cursor on a new line below the function.
- 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). - 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.
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.
Run the test and see it pass
- Open a terminal inside VS Code (
Ctrl+`). - 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 withnpm i -D jest). - 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.
..
----------------------------------------------------------------------
Ran 2 tests in 0.001s
OK
Tweak or add more test cases
- 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.
- Save the file and re‑run the command from Step 4 to confirm the new cases also pass.
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.
- 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.,
unittestis built‑in for Python, but Jest must be added for JavaScript). - Accepting a test that uses undefined imports. If Copilot suggests
import pytestbut you don’t have it, install it withpip install pytestor adjust the import.
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.
← Vissza a hírekhez