Write and improve C#/.NET unit and integration tests with proven patterns.
Copy the install command and let the AI configure it · recommended for beginners
Please install the "csharp-testing" skill from askskill: 1. Download https://raw.githubusercontent.com/affaan-m/ECC/main/skills/csharp-testing/SKILL.md 2. Save it as ~/.claude/skills/csharp-testing/SKILL.md 3. Reload skills and tell me it's ready
Write xUnit unit tests for this C# OrderService using Arrange-Act-Assert and FluentAssertions. Cover both successful order placement and failure when the item list is empty.
A clear set of xUnit test cases with assertions for both success and failure scenarios.
Rewrite this email validation test as an xUnit Theory using InlineData to cover valid and invalid email inputs.
A parameterized test example using Theory and InlineData.
Review this .NET test suite and suggest improvements for readability, coverage, mock usage, and test organization.
A list of improvements for test structure, assertions, coverage gaps, and organization practices.
Developers can use it to quickly create xUnit tests, assertion patterns, and baseline test structure for new .NET business logic. It helps standardize common testing practices.
When a team needs to assess whether existing tests are clear, complete, or maintainable, it can review test organization, parameterized tests, and mock usage.
For ASP.NET Core projects or systems that depend on real infrastructure, it can guide integration test setup using tools like WebApplicationFactory and Testcontainers.
The document outlines testing patterns for .NET applications, centered on a stack that includes xUnit, FluentAssertions, and NSubstitute or Moq. It covers Arrange-Act-Assert unit test structure, parameterized tests with Theory, and broader guidance on integration testing, test infrastructure setup, test quality review, and debugging slow or flaky tests.
Comprehensive testing patterns for .NET applications using xUnit, FluentAssertions, and modern testing practices.
| Tool | Purpose |
|---|---|
| xUnit | Test framework (preferred for .NET) |
| FluentAssertions | Readable assertion syntax |
| NSubstitute or Moq | Mocking dependencies |
| Testcontainers | Real infrastructure in integration tests |
| WebApplicationFactory | ASP.NET Core integration tests |
| Bogus | Realistic test data generation |
public sealed class OrderServiceTests
{
private readonly IOrderRepository _repository = Substitute.For<IOrderRepository>();
private readonly ILogger<OrderService> _logger = Substitute.For<ILogger<OrderService>>();
private readonly OrderService _sut;
public OrderServiceTests()
{
_sut = new OrderService(_repository, _logger);
}
[Fact]
public async Task PlaceOrderAsync_ReturnsSuccess_WhenRequestIsValid()
{
// Arrange
var request = new CreateOrderRequest
{
CustomerId = "cust-123",
Items = [new OrderItem("SKU-001", 2, 29.99m)]
};
// Act
var result = await _sut.PlaceOrderAsync(request, CancellationToken.None);
// Assert
result.IsSuccess.Should().BeTrue();
result.Value.Should().NotBeNull();
result.Value!.CustomerId.Should().Be("cust-123");
}
[Fact]
public async Task PlaceOrderAsync_ReturnsFailure_WhenNoItems()
{
// Arrange
var request = new CreateOrderRequest
{
CustomerId = "cust-123",
Items = []
};
// Act
var result = await _sut.PlaceOrderAsync(request, CancellationToken.None);
// Assert
result.IsSuccess.Should().BeFalse();
result.Error.Should().Contain("at least one item");
}
}
[Theory]
[InlineData("", false)]
[InlineData("a", false)]
[InlineData("[email protected]", false)]
[InlineData("[email protected]", true)]
[InlineData("[email protected]", true)]
public void IsValidEmail_ReturnsExpected(string email, bool expected)
{
EmailValidator.IsValid(email).Should().Be(expected);
}
[Theory]
[MemberData(nameof(InvalidOrderCases))]
public async Task PlaceOrderAsync_RejectsInvalidOrders(CreateOrderRequest request, string expectedError)
{
var result = await _sut.PlaceOrderAsync(request, CancellationToken.None);
result.IsSuccess.Should().BeFalse();
result.Error.Should().Contain(expectedError);
}
public static TheoryData<CreateOrderRequest, string> InvalidOrderCases => new()
{
{ new() { CustomerId = "", Items = [ValidItem()] }, "CustomerId" },
{ new() { CustomerId = "c1", Items = [] }, "at least one item" },
{ new() { CustomerId = "c1", Items = [new("", 1, 10m)] }, "SKU" },
};
[Fact]
public async Task GetOrderAsync_ReturnsNull_WhenNotFound()
{
// Arrange
var orderId = Guid.NewGuid();
_repository.FindByIdAsync(orderId, Arg.Any<CancellationToken>())
.Returns((Order?)null);
// Act
var result = await _sut.GetOrderAsync(orderId, CancellationToken.None);
// Assert
result.Should().BeNull();
}
[Fact]
public async Task PlaceOrderAsync_PersistsOrder()
{
// Arrange
var request = ValidOrderRequest();
// Act
await _sut.PlaceOrderAsync(request, CancellationToken.None);
// Assert — verify the repository was called
await _repository.Received(1).AddAsync(
Arg.Is<Order>(o => o.CustomerId == request.CustomerId),
Arg.Any<CancellationToken>());
}
…
It is for C# and .NET testing practices, including xUnit, FluentAssertions, mocking, integration tests, and test organization best practices. It is suitable for writing new tests, reviewing test quality, and debugging flaky tests.
The excerpt mentions xUnit, FluentAssertions, NSubstitute or Moq, Testcontainers, WebApplicationFactory, and Bogus. For exact configuration details, see the source repository.
Yes. The excerpt explicitly mentions using Testcontainers for real-infrastructure integration tests and WebApplicationFactory for ASP.NET Core integration tests.
Learn robust error-handling patterns across TypeScript, Python, and Go applications.
Manage carrier portfolios, negotiate freight rates, and evaluate carrier performance.
Design and implement robust F# unit, property, and integration tests
Apply test-driven development to Quarkus 3.x features, fixes, and refactors.
Generate images, videos, and audio with one unified AI media workflow.
Run a pre-release verification loop for Quarkus builds, tests, scans, and reviews.
Get idiomatic C# and .NET guidance for architecture, async, and dependency injection.
Learn Rust testing patterns and TDD to improve code quality and reliability.
Build high-quality Kotlin tests with Kotest, MockK, coroutines, and coverage.
Write idiomatic Go tests, benchmarks, fuzz tests, and improve coverage.
Reusable skills that help AI coding agents work better with .NET and C#.
Helps developers avoid common testing anti-patterns and write more reliable tests.