Skip to content

Commit 708ac0b

Browse files
Add pre-existing TinyShop.Tests project with reference test for Part 11
Co-authored-by: jamesmontemagno <1676321+jamesmontemagno@users.noreply.github.com>
1 parent 4fe3040 commit 708ac0b

4 files changed

Lines changed: 112 additions & 65 deletions

File tree

lab/part11-reusable-prompts.md

Lines changed: 59 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
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.
44

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.
5+
In this part, you'll create a reusable prompt file for generating unit tests and use it to add tests to the existing TinyShop.Tests project.
66

77
## Understanding Prompt Files
88

@@ -12,9 +12,17 @@ Prompt files are markdown files stored in the `.github/prompts` folder of your r
1212
- Can include placeholders for dynamic content
1313
- Help standardize common development tasks
1414

15+
## Exploring the Test Project
16+
17+
The solution already includes a **TinyShop.Tests** project with MSTest configured. Let's take a look at what's there.
18+
19+
1. [] In **Solution Explorer**, expand the **TinyShop.Tests** project.
20+
1. [] Open **ProductTests.cs** to see the existing reference test.
21+
1. [] Notice the test follows the Arrange-Act-Assert pattern and verifies default values for a new Product instance.
22+
1523
## Creating a Unit Test Prompt File
1624

17-
Let's create a prompt file that helps generate unit tests using MSTest.
25+
Now let's create a prompt file that helps generate additional unit tests using MSTest.
1826

1927
1. [] In **Solution Explorer**, right-click on the solution and select **Add -> New Folder** and name it `.github` if it doesn't already exist.
2028
1. [] Right-click on the `.github` folder and select **Add -> New Folder** and name it `prompts`.
@@ -54,94 +62,80 @@ Let's create a prompt file that helps generate unit tests using MSTest.
5462

5563
1. [] Save the file.
5664

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-
6565
## Using the Reusable Prompt
6666

67-
Now let's use our new prompt file to generate unit tests for the Product class.
67+
Now let's use our new prompt file to generate additional unit tests for the Product class.
6868

6969
1. [] In Copilot Chat, type `/` to see available prompt files.
7070
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)`
71+
1. [] When prompted for input, type: `the Product class in DataEntities, including tests for setting and getting each property, and tests using DataRow for multiple values`
7272

7373
![Using a prompt file](./images/11-prompt-file.png)
7474

7575
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
76+
- Tests for each property (Name, Description, Price, ImageUrl)
77+
- Tests using DataRow for parameterized testing
78+
- Proper test method naming following the pattern
79+
- Comments explaining test purposes
8080

8181
## Example Generated Tests
8282

8383
The generated tests should look similar to:
8484

8585
```csharp
86-
using DataEntities;
87-
using Microsoft.VisualStudio.TestTools.UnitTesting;
86+
[TestMethod]
87+
public void Name_SetValue_ReturnsExpectedValue()
88+
{
89+
// Arrange
90+
var product = new Product();
91+
var expectedName = "Test Product";
92+
93+
// Act
94+
product.Name = expectedName;
8895

89-
namespace TinyShop.Tests;
96+
// Assert
97+
Assert.AreEqual(expectedName, product.Name);
98+
}
99+
100+
[TestMethod]
101+
[DataRow(19.99)]
102+
[DataRow(0)]
103+
[DataRow(999.99)]
104+
public void Price_SetValue_ReturnsExpectedValue(double price)
105+
{
106+
// Arrange
107+
var product = new Product();
108+
var expectedPrice = (decimal)price;
109+
110+
// Act
111+
product.Price = expectedPrice;
90112

91-
[TestClass]
92-
public class ProductTests
113+
// Assert
114+
Assert.AreEqual(expectedPrice, product.Price);
115+
}
116+
117+
[TestMethod]
118+
[DataRow("product1.png")]
119+
[DataRow("product2.png")]
120+
[DataRow("")]
121+
public void ImageUrl_SetValue_ReturnsExpectedValue(string imageUrl)
93122
{
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-
}
123+
// Arrange
124+
var product = new Product();
125+
126+
// Act
127+
product.ImageUrl = imageUrl;
128+
129+
// Assert
130+
Assert.AreEqual(imageUrl, product.ImageUrl);
137131
}
138132
```
139133

140134
## Running the Tests
141135

142136
1. [] Open **Test Explorer** from **Test -> Test Explorer**.
143137
1. [] Build the solution to discover the tests.
144-
1. [] Click **Run All** to run the generated tests.
138+
1. [] Click **Run All** to run all tests including the new generated tests.
145139
1. [] Verify that all tests pass.
146140

147141
**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.

src/TinyShop.Tests/ProductTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using DataEntities;
2+
3+
namespace TinyShop.Tests;
4+
5+
[TestClass]
6+
public class ProductTests
7+
{
8+
[TestMethod]
9+
public void Product_NewInstance_HasDefaultValues()
10+
{
11+
// Arrange & Act
12+
var product = new Product();
13+
14+
// Assert
15+
Assert.AreEqual(0, product.Id);
16+
Assert.IsNull(product.Name);
17+
Assert.IsNull(product.Description);
18+
Assert.AreEqual(0m, product.Price);
19+
Assert.IsNull(product.ImageUrl);
20+
}
21+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net10.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<IsPackable>false</IsPackable>
8+
<IsTestProject>true</IsTestProject>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="coverlet.collector" Version="6.0.2" />
13+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
14+
<PackageReference Include="MSTest.TestAdapter" Version="3.6.3" />
15+
<PackageReference Include="MSTest.TestFramework" Version="3.6.3" />
16+
</ItemGroup>
17+
18+
<ItemGroup>
19+
<ProjectReference Include="..\DataEntities\DataEntities.csproj" />
20+
</ItemGroup>
21+
22+
<ItemGroup>
23+
<Using Include="Microsoft.VisualStudio.TestTools.UnitTesting" />
24+
</ItemGroup>
25+
26+
</Project>

src/TinyShop.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TinyShop.AppHost", "TinySho
3232
EndProject
3333
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TinyShop.ServiceDefaults", "TinyShop.ServiceDefaults\TinyShop.ServiceDefaults.csproj", "{3F03DD6D-9282-4E09-BF05-DFCB0D7F1375}"
3434
EndProject
35+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TinyShop.Tests", "TinyShop.Tests\TinyShop.Tests.csproj", "{A1B2C3D4-E5F6-7890-ABCD-EF1234567890}"
36+
EndProject
3537
Global
3638
GlobalSection(SolutionConfigurationPlatforms) = preSolution
3739
Debug|Any CPU = Debug|Any CPU
@@ -58,6 +60,10 @@ Global
5860
{3F03DD6D-9282-4E09-BF05-DFCB0D7F1375}.Debug|Any CPU.Build.0 = Debug|Any CPU
5961
{3F03DD6D-9282-4E09-BF05-DFCB0D7F1375}.Release|Any CPU.ActiveCfg = Release|Any CPU
6062
{3F03DD6D-9282-4E09-BF05-DFCB0D7F1375}.Release|Any CPU.Build.0 = Release|Any CPU
63+
{A1B2C3D4-E5F6-7890-ABCD-EF1234567890}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
64+
{A1B2C3D4-E5F6-7890-ABCD-EF1234567890}.Debug|Any CPU.Build.0 = Debug|Any CPU
65+
{A1B2C3D4-E5F6-7890-ABCD-EF1234567890}.Release|Any CPU.ActiveCfg = Release|Any CPU
66+
{A1B2C3D4-E5F6-7890-ABCD-EF1234567890}.Release|Any CPU.Build.0 = Release|Any CPU
6167
EndGlobalSection
6268
GlobalSection(SolutionProperties) = preSolution
6369
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)