Goal:
By the end of this session, you will learn how you can use GitHub Copilot Edits (preview) and other features to enhance test projects by creating new unit and integration test cases.
You will create unit tests for the BasketItem class in the Basket.UnitTests, as well as cover additional scenario in Catalog.FunctionalTests projects.
By the end of this lab, you will have enhanced two test projects in the solution and gained practical experience with Copilot Edits (preview) and other features.
Estimated Duration:
30-45 minutes
Audience:
Developers, QA testers, DevOps engineers, and Technical Writers.
Prerequisites:
- Completed Lesson 1: Installing and Configuring GitHub Copilot, or
- Access to GitHub Copilot (subscription),
- Visual Studio Code,
- GitHub Copilot extension to VS Code.
- Understand how to leverage GitHub Copilot to generate and execute unit and integration tests.
- Learn how incremental test improvements can be achieved.
- GitHub Copilot enabled on your GitHub account.
- Visual Studio Code with GitHub Copilot extension installed.
- Code repository for the lab: GitHub Copilot.
-
Clone the repository:
git clone https://github.com/neudesic/learning-github-copilot.git cd learning-github-copilot/samples/eShop
-
Open the project in your IDE:
code .
Description:
In this step, you will create a new unit tests class with some simple test cases for BasketItem class.
Instructions:
-
In VS Code, open file src/Basket.API/Model/BasketItem.cs
-
Open Copilot Edits by pressing Ctrl-Shift-I.
-
Verify BasketItem.cs file is included in the Working Set, and click + add Basket.UnitTests.csproj file.
-
Ensure model o1-mini (Preview) is selected (right corner of the Copilot Edits command bar down below).
-
Enter following request
Generate unit tests for BasketItem methods and save in the Basket.UnitTests.csproj. Include edge cases.
and press Enter.
In a few moments, unit tests code will be generated in a new file BasketItemTests.cs that will show as unsaved file in the editor.. -
Observe the generated code.
-
(Optional) If you find that further improvements can be made, you can ask Copilot Edits and iterate prompting until desired qualities are met.
-
Click Accept to accept the generated code.
-
Save the file. (If asked to confirm you want to save code generated by GitHub Copilot, confirm).
-
At the Terminal prompt navigate to the Basket.UnitTests project and run the tests:
cd tests/Basket.UnitTests dotnet test
-
Click Done to complete Copilot Edits session.
Expected Outcome:
The existing and new unit tests should pass. If some test cases fail, you can use GitHub Copilot to fix the code or the failing unit tests.
NOTE: Generated code may not be correct as GitHub Copilot context grasp is limited and may use incorrect assumptions. In general, it will correctly identify the testing framework for the project.
Use your skills and GitHub Copilot to resolve the issue by varying your prompts and focus in an iterative manner. If you need to clear context and reset focus by using /clear
command.
Description:
In this step, you will work with GitHub Copilot to add a test case to the existing Catalog.API integration tests suite.
Instructions:
-
In VS Code, open file tests/Catalog.FunctionalTests/CatalogApiTests.cs
-
Open Copilot Edits by pressing Ctrl-Shift-I.
-
Verify CatalogApiTests.cs file is included in the Working Set
-
Ensure model o1-mini (Preview) is selected (right corner of the Copilot Edits command bar down below).
-
In the CatalogApiTests.cs file, scroll down and select test case GetCatalogItemWithId().
-
In Copilot Edits prompt field, enter
Consider selected test case. It expects record with the given id. Can you make a test case where no item is returned with the given id?
and press Enter.
In a few moments, the requested tests case will be generated in CatalogApiTests.cs. -
Observe the generated code. The changes are highlighted.
-
(Optional) You may further ask about reasoning why no items will be returned, which can lead into discussion on where 400 or 404 is more appropriate!
-
Click Accept to accept the generated code.
-
Save the changes made to the file.
-
At the Terminal prompt navigate to the Catalog.FunctionalTests project and run the tests:
cd tests/Catalog.FunctionalTests dotnet test
-
Click Done to complete Copilot Edits session.
Expected Outcome:
The existing and new test case should pass. This being integration tests, it may take a few moments for test run to complete. If some test cases fail, you can use GitHub Copilot to fix the code or the failing unit tests.
NOTE: Generated code may not be correct as GitHub Copilot context grasp is limited and may use incorrect assumptions. In general, it will correctly identify the testing framework for the project.
Use your skills and GitHub Copilot to resolve the issue by varying your prompts and focus in an iterative manner.
NOTE: Generated code may not be correct as GitHub Copilot context grasp is limited and may use incorrect assumptions. In general, it will correctly identify the testing framework and mocking library for the project.
Use your skills and GitHub Copilot to resolve the issue by varying your prompts and focus in an iterative manner.
- Suggested command/script fails: GitHub Copilot is great, but is not perfect.
Solution: Use your skills and GitHub Copilot to help resolve the issue in an iterative manner. This is extra challenge and learning opportunity!
Summary:
In this lab you should have learned and gained practical experience with Copilot Edits (preview) and other features you can use with different types of testing cases.
Next Steps: