Project 2 — Unit-Test a Function You Didn't Write
Pick an existing function, generate tests, extend for edge cases.
What You Will Learn
- Pick an existing untested function.
- Generate a starter test suite.
- Extend with edge cases.
- Verify behavior via tests.
- Measure coverage gain.
Why This Matters
Adding tests to legacy code is the highest-leverage way to reduce future bugs. Copilot can scaffold the suite; you extend and verify. This project builds the test-extension habit that pays off forever.
Concept Explained
Pick a function in your codebase with no tests. Generate a starter suite with /tests. Extend with edge cases Copilot suggests. Run; fix any tests that fail due to wrong assumptions.
How It Works
/tests generates a starter suite covering happy path and basic edge cases. You run them, fix wrong assertions, then ask Copilot for missed edge cases and add tests for each. Coverage goes from 0% to 60–80% in minutes.
Step-by-Step Tutorial
1. Pick untested function
Find a function in your codebase with no tests.2. Run /tests
Highlight the function. Type /tests. Review the generated suite.3. Run tests
Run them. Some may fail — /tests may have guessed at behavior. Fix assertions.4. Extend edge cases
Ask Copilot: 'what edge cases did these tests miss?' Add tests for each.5. Measure coverage
Run coverage tool. Document the before/after percentage.Real-World Example
A developer picked a date-parsing utility with no tests. /tests generated 5 tests, 2 failed (wrong date format assumed). Fixed the assertions. Asked Copilot for missed edge cases — got: leap years, timezones, milliseconds, year 9999. Added 4 more tests. Coverage went from 0% to 85% in 15 minutes.
Example Prompts / Commands / Code
# After /tests, ask:
What edge cases did these tests miss? List them, then add tests for each.
# Copilot might suggest:
- Leap year dates (Feb 29)
- Timezone transitions
- Very old or future dates (year 1, year 9999)
- Millisecond precision
- Invalid month (13) or day (32)
- Negative timestamps
Common Mistakes
- Trusting /tests assertions without running — some are wrong.
- Not extending for edge cases — /tests covers happy path.
- Adding tests without verifying the function's actual behavior.
- Skipping coverage measurement — no data on improvement.
Best Practices
- Always run generated tests; fix failures before extending.
- Ask Copilot to list missed edge cases; add tests for each.
- Verify the function's behavior (read the code) before writing tests.
- Measure coverage before and after to quantify improvement.
- Use TDD with Copilot for higher quality (IN-33).
Troubleshooting
| Problem | How to Fix |
|---|---|
| Tests fail because behavior is wrong | The function may have bugs. Write tests documenting actual behavior, then fix bugs. |
| Coverage plateaus | Some branches may be unreachable. Refactor for testability. |
Practical Exercise
Your Turn
This IS the exercise. Pick an untested function, generate tests, extend, measure coverage. Document the before/after.
Professional Challenge
After unit tests pass, add an integration test that calls the function with real data from your application. Document what the integration test caught that unit tests missed.
Key Takeaways
- /tests generates a starter suite in seconds.
- Always run and fix before extending.
- Ask Copilot for missed edge cases.
- Verify function behavior before writing tests.
- Measure coverage before/after.
Frequently Asked Questions
What if /tests can't detect the framework?
What coverage should I aim for?
Further Reading
Official References
SEO Metadata
SEO title: Project 2 — Unit-Test a Function You Didn't Write
Meta description: Pick an existing function, generate tests, extend for edge cases.
Primary keyword: project 2
Secondary keywords: project 2 — unit-test a function you didn't write
Search intent: Informational
URL slug: /project-unit-test-existing-function-copilot
Categories: AI Tools, GitHub Copilot
Tags: GitHub Copilot, Beginner, Project, Unit Tests, Testing, IMCSEIAN, Tutorial, IMCSEIAN
Featured image concept: IMCSEIAN lesson card for Project 2 — Unit-Test a Function You Didn't Write
Comments
Comments
Post a Comment