Fact is an xUnit attribute that marks a method as one individual test case. The test runner discovers the method and reports its assertions or failures. section 35.2 section 35.7
A Fact test method must follow these xUnit rules:
- Place the [Fact] attribute on the method. section 35.2
- Declare the method public and parameterless. section 35.2
- Return void or, for asynchronous tests, Task. section 35.2
- Place it inside a public, nonstatic class. section 35.2
A simple Fact test
[Fact] public void ConvertToGbp_ConvertsCorrectly() { var converter = new CurrencyConverter(); var actual = converter.ConvertToGbp(3, 1.5m, 4); Assert.Equal(2, actual); } Use Fact when one fixed set of inputs represents one test case. Replace it with Theory when the method needs parameters and multiple data sets. section 35.5 section 35.5
Listing 35.2 An example xUnit unit test, created by the default template
public class UnitTest1 #1
{
[Fact] #2
public void Test1() #3
{
}
}
Shows the default xUnit test shape: a public test class, a Fact attribute, and a parameterless test method. section 35.2
