Interview frequency: …
Short answer
[Fact] is one test that runs once with fixed conditions, while [Theory] accepts parameters and runs multiple times for each [InlineData] or [MemberData] row.
Explanation
To test an email validator with ten different inputs, writing ten [Fact] methods is unnecessary duplication. Use [Theory] and [InlineData('[email protected]', true)] to pass valid and invalid values to the same test and check edge cases in one concise method.
Code example
[Theory]
[InlineData(2, true)]
[InlineData(3, false)]
[InlineData(4, true)]
public void IsEven_ReturnsCorrectResult(int number, bool expected) {
Assert.Equal(expected, MathUtils.IsEven(number));
}Common mistakes
- Passing complex, non-constant objects to [InlineData]; it accepts only constants, so use [MemberData] or [ClassData] for complex objects.