ワンクリックで
writing-unit-tests-csharp
Guidelines and patterns for writing unit tests in C# using MSTest SDK.
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
メニュー
Guidelines and patterns for writing unit tests in C# using MSTest SDK.
Codex または Claude でインストール この Prompt をコピーして Codex、Claude、または他のアシスタントに貼り付けると、Skill ページを確認してインストールできます。
SOC 職業分類に基づく
| name | writing-unit-tests-csharp |
| description | Guidelines and patterns for writing unit tests in C# using MSTest SDK. |
This skill covers writing unit tests for the backend using MSTest SDK. The project uses a lean, zero-config approach that leverages Microsoft's official SDK-style testing.
backend/
├── WebApp.sln
├── WebApp.Api/
│ ├── Models/ # DTOs and request/response models
│ ├── Services/ # Business logic and agent integration
│ └── Program.cs # Minimal API endpoints
└── WebApp.Api.Tests/
├── WebApp.Api.Tests.csproj
└── [TestClass].cs # Test files organized by class under test
The test project uses MSTest SDK which eliminates the need for explicit package references:
See backend/WebApp.Api.Tests/WebApp.Api.Tests.csproj for current configuration. The project uses MSTest SDK which eliminates the need for explicit package references — just set Sdk="MSTest.Sdk/{version}" in the project element.
using WebApp.Api.Models;
namespace WebApp.Api.Tests;
[TestClass]
public class ErrorResponseFactoryTests
{
[TestMethod]
public void CreateFromException_ReturnsCorrectStatusCode()
{
// Arrange
var exception = new InvalidOperationException("Test");
// Act
var result = ErrorResponseFactory.CreateFromException(exception, 500, isDevelopment: false);
// Assert
Assert.AreEqual(500, result.Status);
}
[TestMethod]
[DataRow(400, "Bad Request")]
[DataRow(401, "Unauthorized")]
[DataRow(500, "Internal Server Error")]
public void CreateFromException_MapsStatusToTitle(int status, string expectedTitle)
{
// Parameterized test using DataRow
}
}
# Run all backend tests
cd backend
dotnet test
# Run with verbose output
dotnet test --verbosity normal
# Run specific test class
dotnet test --filter "FullyQualifiedName~ErrorResponseFactoryTests"
# Run with coverage (requires coverlet)
dotnet test --collect:"XPlat Code Coverage"
| Class | What to Test |
|---|---|
ErrorResponseFactory | Status code mapping, dev vs prod mode, exception detail hiding |
ChatRequest | Validation logic if any |
StreamChunk | Serialization/deserialization |
AnnotationInfo | Property mapping |
| Class | Testing Approach |
|---|---|
AgentFrameworkService | Use validating-ui-features skill with Playwright for integration tests |
Use descriptive names following: MethodName_Scenario_ExpectedBehavior
[TestMethod]
public void CreateFromException_WhenDevelopmentMode_IncludesStackTrace() { }
[TestMethod]
public void CreateFromException_WhenProductionMode_HidesStackTrace() { }
MSTest provides these assertion methods:
// Equality
Assert.AreEqual(expected, actual);
Assert.AreNotEqual(notExpected, actual);
// Nullability
Assert.IsNull(value);
Assert.IsNotNull(value);
// Boolean
Assert.IsTrue(condition);
Assert.IsFalse(condition);
// Type
Assert.IsInstanceOfType(obj, typeof(ExpectedType));
// Collections
CollectionAssert.Contains(collection, element);
CollectionAssert.AreEqual(expected, actual);
// Exceptions
Assert.ThrowsException<InvalidOperationException>(() => MethodThatThrows());
Use the validating-ui-features skill and Playwright when:
| Command | Purpose |
|---|---|
dotnet test | Run all tests |
dotnet test --filter "Name~Test" | Run filtered tests |
dotnet build | Build without running |
dotnet test --list-tests | List all tests |
Provides deployment commands and troubleshooting for Azure Container Apps. Use when running azd commands, deploying containers, debugging deployment failures, or updating infrastructure in this repository.
Provides SSE streaming patterns for the chat API and frontend. Use when implementing or modifying chat streaming, handling SSE events, or troubleshooting message flow between frontend and backend.
Provides research patterns for Foundry Agent Service SDK. Use when implementing agent features, looking up SDK methods, finding code samples, or troubleshooting Azure.AI.Projects API usage.
Provides architecture overview with state machines, SSE event flow, and file mappings. Use when understanding system design, debugging state issues, or maintaining ARCHITECTURE-FLOW.md.
Provides C# and ASP.NET Core coding standards for this repository. Use when writing or modifying C# code, implementing API endpoints, configuring middleware, or working with authentication in the backend.
Diagnose and fix incomplete local development setup. Use when dev servers fail to start, env vars are missing, authentication errors occur, or before running any dev commands for the first time.