Lesson 7 of 40
Testing
Intermediate
40 min
Unit Testing with xUnit 3 and Copilot
Write comprehensive tests faster using xUnit 3's new features, test generators, and AI-assisted test creation in VS 2026.
Part 1: xUnit 3 New Features
xUnit 3 brings significant improvements:
// Theory with ClassData
[Theory]
[ClassData(typeof(OrderTestData))]
public async Task ProcessOrder_ValidatesTotal(Order order, decimal expected)
{
var result = await _service.ProcessAsync(order);
Assert.Equal(expected, result.Total);
}
[Theory]
[ClassData(typeof(OrderTestData))]
public async Task ProcessOrder_ValidatesTotal(Order order, decimal expected)
{
var result = await _service.ProcessAsync(order);
Assert.Equal(expected, result.Total);
}
Part 2: AI Test Generation
In VS 2026, right-click any method → Generate Tests with Copilot. Copilot creates:
- Happy path tests
- Edge case tests (null, empty, boundary values)
- Exception tests
- Async/cancellation tests
Part 3: Mocking with NSubstitute
var repo = Substitute.For<IOrderRepository>();
repo.GetByIdAsync(Arg.Any<int>())
.Returns(Task.FromResult(new Order { Id = 1, Total = 99.99m }));
// Verify it was called
await repo.Received(1).GetByIdAsync(1);
repo.GetByIdAsync(Arg.Any<int>())
.Returns(Task.FromResult(new Order { Id = 1, Total = 99.99m }));
// Verify it was called
await repo.Received(1).GetByIdAsync(1);
Part 4: Code Coverage with Hot Coverage
VS 2026 includes Hot Coverage — live coverage indicators in the gutter as you type:
- Green gutter = covered line
- Red gutter = uncovered line
- Run Test → Analyze Code Coverage for full report
- Coverage data persists between test runs