Getting Started with GitHub Copilot in VS Code
Use Copilot to write a basket-total function, generate tests, and inspect changes in VS Code.
Overview
For learners who want to complete this practical exercise with inspectable sample data and explicit checks.
- Connect the correct Copilot account in VS Code.
- Generate a small function from an input/output contract.
- Run tests rather than assuming suggestions are correct.
You will build: basket.py and test_basket.py passing three basket-total tests.
Prerequisites
- VS Code, a GitHub account with Copilot access, and Python 3.10+.
- Know how to create files and run Python in a terminal.
Copilot access and limits depend on the account, including a free option where eligible. This exercise requires no specific paid plan or agent mode.
Step-by-step tutorial
Step 1. Sign in and open a practice folder
Use VS Code’s Copilot icon or Accounts menu to sign in with GitHub, following the official setup guide. Complete any requested Copilot extension setup. Open a new copilot-lab folder and create basket.py; do not expose an unapproved workplace repository.
Step 2. Specify the contract before accepting code
In basket.py, implement total_cents(prices). prices is a list of non-negative integer cents; an empty basket returns 0, and negative or non-int values raise ValueError. Reject bool even though Python treats it as an int. Use no network or dependencies. Briefly explain input validation.Send the request in Copilot Chat with the file open. If using inline suggestions, read each line before accepting it. The output may differ from the example below; the contract and tests determine acceptance.
def total_cents(prices):
if not isinstance(prices, list):
raise ValueError("prices must be a list")
if any(type(p) is not int or p < 0 for p in prices):
raise ValueError("invalid price")
return sum(prices)Step 3. Create tests independently of the response
Save this as test_basket.py beside basket.py. The True case catches overly permissive isinstance(p, int) validation. These are minimum checks, not exhaustive coverage.
import unittest
from basket import total_cents
class BasketTests(unittest.TestCase):
def test_total(self):
self.assertEqual(total_cents([125, 250]), 375)
def test_empty(self):
self.assertEqual(total_cents([]), 0)
def test_invalid(self):
for value in [-1, "5", True]:
with self.assertRaises(ValueError):
total_cents([value])
if __name__ == "__main__":
unittest.main()python3 -m unittest -vStep 4. Review the diff and one small change
Ask Copilot for a test covering non-list input, rerun the tests yourself, and inspect the diff. Check that no unrelated dependencies, configuration, or files were added. Do not automatically commit or submit code in this exercise.
Verification
- The original three tests pass, and an additional test covers non-list input.
- You can explain why True must be rejected.
Troubleshooting
Copilot reports no access
Cause: VS Code is using the wrong account or the account has reached a limit.
Fix: Check Accounts and Copilot status; follow official setup without putting tokens in code.
Tests cannot import basket
Cause: The terminal is in the wrong directory.
Fix: Place both files together and run python3 -m unittest -v in that directory.
Exercises & extensions
- Add zero-price and larger-basket tests.
- Continue to debugging/refactoring to practice finding AI-generated bugs.
Integer cents avoid floating-point errors here; this is not a payment system or a tax policy. Generated code still needs independent review.
