xunit
1
总安装量
1
周安装量
#55655
全站排名
安装命令
npx skills add https://github.com/g1joshi/agent-skills --skill xunit
Agent 安装分布
mcpjam
1
claude-code
1
replit
1
junie
1
zencoder
1
Skill 文档
xUnit.net
xUnit.net is the modern, open-source unit testing tool for .NET (C#, F#, VB). It is chosen by Microsoft/dotnet for their own repositories.
When to Use
- .NET Core / .NET 5+: The default template choice.
- Modern Practices: Enforces good habits (Isolation, Constructor Injection for fixtures).
Quick Start
using Xunit;
public class CalculatorTests
{
[Fact]
public void PassingTest()
{
Assert.Equal(4, Add(2, 2));
}
[Theory]
[InlineData(3)]
[InlineData(5)]
[InlineData(6)]
public void MyTheory(int value)
{
Assert.True(IsOdd(value));
}
}
Core Concepts
Facts vs Theories
[Fact]: A test that is always true. Invariant.[Theory]: A test that is true for a particular set of data (Parameterized).
Fixtures (IClassFixture)
xUnit creates a new instance of the Test Class for every test method (High isolation). To share context (like a DB connection), implement IClassFixture<T>.
Assert
Assert.Equal, Assert.Throws, Assert.Collection.
Best Practices (2025)
Do:
- Use
xunit.runner.visualstudio: To run tests in VS/VS Code. - Use FluentAssertions: xUnit’s asserts are okay, but
value.Should().Be(4)(FluentAssertions) is much more readable. - Constructor Injection: Use the constructor for Setup, and
Dispose()(IDisposable) for Teardown. xUnit abolished[SetUp]and[TearDown]attributes to force cleaner design.
Don’t:
- Don’t use
Console.WriteLine: UseITestOutputHelperinjected in the constructor to log outputs.