Test with GitHub Copilot
Writing and maintaining tests is a crucial but often time-consuming part of software development. GitHub Copilot streamlines this process by helping you write, debug, and fix tests more efficiently in Visual Studio Code. This article shows you how to leverage Copilot's testing capabilities to improve your testing workflow and increase test coverage in your projects.
Copilot can help with the following testing tasks:
- Set up testing frameworks: get help configuring the right testing framework and VS Code extensions for your project and language.
- Generate test code: create unit tests, integration tests, and end-to-end tests that cover your application code.
- Handle edge cases: generate comprehensive test suites to cover edge cases and error conditions.
- Fix failing tests: receive suggestions for fixing test failures.
- Maintain consistency: personalize Copilot to generate tests that follow your project's coding practices.
If you don't yet have a Copilot subscription, you can use Copilot for free by signing up for the Copilot Free plan and get a monthly limit of completions and chat interactions.
Set up your testing framework
To accelerate your testing workflow, Copilot can help set up the testing framework and VS Code extensions for your project. Copilot suggests appropriate testing frameworks based on your project type.
- Open the Chat view (โโI (Windows, Linux Ctrl+Alt+I)).
- Enter the
/setupTests
command in the chat input field. - Follow Copilot's guidance to configure your project.
Write tests with Copilot
Copilot can help you write tests for your application code by generating test code that covers your codebase. This includes unit tests, end-to-end tests, and tests for edge cases.
Use chat prompts
-
Open your application code file.
-
Open one of these views:
- Copilot Edits (โงโI (Windows Ctrl+Shift+I, Linux Ctrl+Shift+Alt+I))
- Chat view (โโI (Windows, Linux Ctrl+Alt+I))
- Inline Chat (โI (Windows, Linux Ctrl+I))
-
Enter a prompt like:
- "Generate tests for this code"
- "Write unit tests including edge cases"
- "Create integration tests for this module"
Get more guidance about using GitHub Copilot for writing tests in the GitHub documentation.
Use editor smart actions
To generate tests for your application code without writing a prompt, you can use the editor smart actions.
-
Open your application code file.
-
Optionally, select the code you want to test.
-
Right-click and select Copilot > Generate Tests.
Copilot generates test code in an existing test file, or creates a new test file if one doesn't exist.
-
Optionally, refine the generated tests by providing additional context in the Inline Chat prompt.
Fix failing tests
Copilot integrates with the Test Explorer in VS Code and can help with fixing failing tests.
- In the Test Explorer, hover over a failing test
- Select the Fix Test Failure button (sparkle icon)
- Review and apply Copilot's suggested fix
Alternatively, you can:
- Open the Chat view
- Enter the
/fixTestFailure
command - Follow Copilot's suggestions to fix the test
Copilot Edits agent mode (preview) monitors the test output when running tests, and automatically attempts to fix and rerun failing tests.
Personalize test generation
If your organization has specific testing requirements, you can customize how Copilot generates tests to ensure they meet your standards. You can personalize how Copilot generates tests by providing custom instructions. For example:
- Specify preferred testing frameworks
- Define naming conventions for tests
- Set code structure preferences
- Request specific test patterns or methodologies
Get more information about personalizing Copilot for generating tests.
Tips for better test generation
To get the best results when generating tests with Copilot, follow these tips:
- Provide context in your prompts about the testing framework you prefer
- Specify if you want particular types of tests (unit, integration, end-to-end)
- Ask for specific test cases or edge cases
- Request tests that follow your project's coding standards
Next steps
- Learn more about Copilot in VS Code.
- Explore general testing features in VS Code.
- Check out example prompts for generating unit tests