Unit Testing with xUnit
Prerequisites: Async and await
xUnit is one of the most widely used C# testing frameworks —
a test method is any method annotated [Fact]:
public class CalculatorTests {
[Fact]
public void AddsTwoNumbers() {
var calc = new Calculator();
Assert.Equal(5, calc.Add(2, 3));
}
[Fact]
public void DivisionByZeroThrows() {
var calc = new Calculator();
Assert.Throws<DivideByZeroException>(() => calc.Divide(1, 0));
}
}
dotnet test
Directly parallel to Java's JUnit @Test — dotnet test (like
mvn test) discovers and runs every [Fact]-annotated method
automatically.