xUnit, NUnit, and MSTest run automated tests with dotnet test. Tests live in separate projects referencing your class library—same discipline as JUnit in Java.
xUnit example
public class CalculatorTests {
[Fact]
public void Add_returns_sum() {
Assert.Equal(5, Calculator.Add(2, 3));
}
}
Running tests
dotnet new xunit -n MyApp.Tests
dotnet test --filter "FullyQualifiedName~Add_returns_sum"
Use Arrange-Act-Assert structure; keep tests deterministic—no hidden network unless marked integration.
Important interview questions and answers
- Q: Fact vs Theory?
A:[Fact]is a single case;[Theory]with[InlineData]parametrizes multiple inputs. - Q: Mocking?
A: Libraries like Moq or NSubstitute fake interfaces for isolated unit tests—avoid mocking concrete sealed types.
Self-check
- What CLI command runs tests?
- Why separate test projects?
Tip: Name tests clearly: MethodName_Scenario_ExpectedResult—xUnit, NUnit, and MSTest all integrate with dotnet test.
Interview prep
- xUnit vs NUnit?
Both integrate with
dotnet test—xUnit uses parameterless constructors and[Fact]; NUnit uses[Test]and setup attributes.- Arrange-Act-Assert?
Structure tests: set up data, invoke the method, assert the outcome—readable and consistent.