Key xUnit features
In xUnit, the [Fact]
attribute is the way to create unique test cases, while the [Theory]
attribute is the way to make data-driven test cases. Let’s start with facts, the simplest way to write a test case.
Facts
Any method with no parameter can become a test method by decorating it with a [Fact]
attribute, like this:
public class FactTest
{
[Fact]
public void Should_be_equal()
{
var expectedValue = 2;
var actualValue = 2;
Assert.Equal(expectedValue, actualValue);
}
}
You can also decorate asynchronous methods with the fact attribute when the code under test needs it:
public class AsyncFactTest
{
[Fact]
public async Task Should_be_equal()
{
var expectedValue = 2;
var actualValue = 2;
await Task.Yield();
Assert.Equal(expectedValue, actualValue);
}
}
In the preceding code, the highlighted line conceptually represents an asynchronous operation and does nothing more than allow...