|
| 1 | +# Part 11: Reusable Prompt Files |
| 2 | + |
| 3 | +Prompt files are a powerful way to create standardized, reusable prompts that can be shared across your team. They help ensure consistency in how you interact with GitHub Copilot and can encode best practices for common tasks like code generation, testing, and documentation. |
| 4 | + |
| 5 | +In this part, you'll create a reusable prompt file for generating unit tests and use it to test the Product class in the TinyShop application. |
| 6 | + |
| 7 | +## Understanding Prompt Files |
| 8 | + |
| 9 | +Prompt files are markdown files stored in the `.github/prompts` folder of your repository. They: |
| 10 | +- Can be invoked by name in Copilot Chat |
| 11 | +- Are shared with your entire team through source control |
| 12 | +- Can include placeholders for dynamic content |
| 13 | +- Help standardize common development tasks |
| 14 | + |
| 15 | +## Creating a Unit Test Prompt File |
| 16 | + |
| 17 | +Let's create a prompt file that helps generate unit tests using MSTest. |
| 18 | + |
| 19 | +1. [] In **Solution Explorer**, right-click on the solution and select **Add -> New Folder** and name it `.github` if it doesn't already exist. |
| 20 | +1. [] Right-click on the `.github` folder and select **Add -> New Folder** and name it `prompts`. |
| 21 | +1. [] Right-click on the `prompts` folder and select **Add -> New Item**. |
| 22 | +1. [] Create a new file named `unit-test.prompt.md` with the following content: |
| 23 | + |
| 24 | + ```markdown |
| 25 | + --- |
| 26 | + mode: agent |
| 27 | + description: Generate comprehensive unit tests using MSTest |
| 28 | + --- |
| 29 | + |
| 30 | + # Unit Test Generator |
| 31 | + |
| 32 | + Generate unit tests for the selected code using the MSTest framework. |
| 33 | + |
| 34 | + ## Requirements |
| 35 | + |
| 36 | + - Use MSTest attributes ([TestClass], [TestMethod], [DataRow]) |
| 37 | + - Follow the Arrange-Act-Assert pattern |
| 38 | + - Include both positive and negative test cases |
| 39 | + - Test edge cases and boundary conditions |
| 40 | + - Use descriptive test method names that explain what is being tested |
| 41 | + - Mock dependencies where appropriate |
| 42 | + |
| 43 | + ## Test Structure |
| 44 | + |
| 45 | + Each test should: |
| 46 | + 1. Have a clear name following the pattern: `MethodName_Scenario_ExpectedBehavior` |
| 47 | + 2. Include a brief comment explaining the test purpose |
| 48 | + 3. Use proper assertions with meaningful failure messages |
| 49 | + |
| 50 | + ## Context |
| 51 | + |
| 52 | + Generate tests for: ${input:Describe what you want to test} |
| 53 | + ``` |
| 54 | + |
| 55 | +1. [] Save the file. |
| 56 | + |
| 57 | +## Setting Up a Test Project |
| 58 | + |
| 59 | +Before we can use our prompt, we need a test project. Let's create one using Copilot Agent. |
| 60 | + |
| 61 | +1. [] Open Copilot Chat and switch to **Agent** mode. |
| 62 | +1. [] Type: `Create a new MSTest project called TinyShop.Tests in the src folder and add it to the solution. Add a reference to the DataEntities project.` |
| 63 | +1. [] Accept the changes proposed by Copilot. |
| 64 | + |
| 65 | +## Using the Reusable Prompt |
| 66 | + |
| 67 | +Now let's use our new prompt file to generate unit tests for the Product class. |
| 68 | + |
| 69 | +1. [] In Copilot Chat, type `/` to see available prompt files. |
| 70 | +1. [] Select `unit-test` from the list of available prompts. |
| 71 | +1. [] When prompted for input, type: `the Product class in DataEntities, including tests for property getters/setters and validation of the Price property (should be non-negative)` |
| 72 | + |
| 73 | +  |
| 74 | + |
| 75 | +1. [] Review the generated tests. They should include: |
| 76 | + - Tests for each property |
| 77 | + - Tests for edge cases (empty strings, null values) |
| 78 | + - Tests for price validation |
| 79 | + - Proper test method naming |
| 80 | + |
| 81 | +## Example Generated Tests |
| 82 | + |
| 83 | +The generated tests should look similar to: |
| 84 | + |
| 85 | +```csharp |
| 86 | +using DataEntities; |
| 87 | +using Microsoft.VisualStudio.TestTools.UnitTesting; |
| 88 | + |
| 89 | +namespace TinyShop.Tests; |
| 90 | + |
| 91 | +[TestClass] |
| 92 | +public class ProductTests |
| 93 | +{ |
| 94 | + [TestMethod] |
| 95 | + public void Name_SetValue_ReturnsExpectedValue() |
| 96 | + { |
| 97 | + // Arrange |
| 98 | + var product = new Product(); |
| 99 | + var expectedName = "Test Product"; |
| 100 | + |
| 101 | + // Act |
| 102 | + product.Name = expectedName; |
| 103 | + |
| 104 | + // Assert |
| 105 | + Assert.AreEqual(expectedName, product.Name); |
| 106 | + } |
| 107 | + |
| 108 | + [TestMethod] |
| 109 | + public void Price_SetPositiveValue_ReturnsExpectedValue() |
| 110 | + { |
| 111 | + // Arrange |
| 112 | + var product = new Product(); |
| 113 | + var expectedPrice = 29.99m; |
| 114 | + |
| 115 | + // Act |
| 116 | + product.Price = expectedPrice; |
| 117 | + |
| 118 | + // Assert |
| 119 | + Assert.AreEqual(expectedPrice, product.Price); |
| 120 | + } |
| 121 | + |
| 122 | + [TestMethod] |
| 123 | + [DataRow("product1.png")] |
| 124 | + [DataRow("product2.png")] |
| 125 | + [DataRow("")] |
| 126 | + public void ImageUrl_SetValue_ReturnsExpectedValue(string imageUrl) |
| 127 | + { |
| 128 | + // Arrange |
| 129 | + var product = new Product(); |
| 130 | + |
| 131 | + // Act |
| 132 | + product.ImageUrl = imageUrl; |
| 133 | + |
| 134 | + // Assert |
| 135 | + Assert.AreEqual(imageUrl, product.ImageUrl); |
| 136 | + } |
| 137 | +} |
| 138 | +``` |
| 139 | + |
| 140 | +## Running the Tests |
| 141 | + |
| 142 | +1. [] Open **Test Explorer** from **Test -> Test Explorer**. |
| 143 | +1. [] Build the solution to discover the tests. |
| 144 | +1. [] Click **Run All** to run the generated tests. |
| 145 | +1. [] Verify that all tests pass. |
| 146 | + |
| 147 | +**Key Takeaway**: Reusable prompt files help standardize how your team uses GitHub Copilot. By creating prompts for common tasks like unit testing, you ensure consistency and encode best practices that everyone on the team can benefit from. |
0 commit comments