dotnet-testinglisted
Install: claude install-skill AllThingsSmitty/agent-skills
# .NET Testing
xUnit is the modern standard for .NET — it's what ASP.NET Core itself uses internally. Its constructor-based setup (no `[SetUp]` attribute) encourages smaller, more focused test classes. Use it for new projects.
## xUnit basics
```csharp
public class UserServiceTests
{
private readonly UserService _sut;
private readonly IUserRepository _repository;
// Constructor runs before each test — replaces [SetUp]
public UserServiceTests()
{
_repository = Substitute.For<IUserRepository>(); // NSubstitute
_sut = new UserService(_repository);
}
[Fact]
public async Task GetUser_WhenUserExists_ReturnsUser()
{
// Arrange
var userId = "user-1";
_repository.FindByIdAsync(userId, Arg.Any<CancellationToken>())
.Returns(new User(userId, "Alice"));
// Act
var result = await _sut.GetUserAsync(userId);
// Assert
result.Should().NotBeNull();
result!.Name.Should().Be("Alice");
}
}
```
**Test naming**: `MethodName_Scenario_ExpectedResult` is readable and searchable. Keep it consistent across the project.
## Parametrized tests with `[Theory]`
Replace repeated `[Fact]` methods with `[Theory]` + `[InlineData]`:
```csharp
[Theory]
[InlineData("user@example.com", true)]
[InlineData("user+tag@example.com", true)]
[InlineData("not-an-email", false)]
[InlineData("", false)]
public void ValidateEmail_ReturnsExpectedResult(string email, bool expected)
{