Unit Testing & TDD Interview Questions and Answers
Unit testing interview questions for .NET developers — xUnit, NUnit and MSTest, mocking with Moq, the AAA pattern, test doubles, TDD, and integration testing in ASP.NET Core.
- 01 What is the difference between an integration test and an end-to-end test? An integration test exercises a slice of the system with some real components (the API + a real database, but no…
- 02 What is a unit test, and how does it differ from an integration test? A unit test verifies one small piece of logic (a method/class) in isolation, with dependencies replaced by test…
- 03 What is the Arrange-Act-Assert (AAA) pattern? AAA structures a test into three clear sections: Arrange — set up the object under test and its inputs; Act — call the…
- 04 What is the difference between a mock, a stub, a fake and a spy? All are test doubles. A stub returns canned answers to calls. A mock also has expectations you verify (“was Send…
- 05 What is Test-Driven Development (TDD)? TDD is writing a failing test before the code that makes it pass, in a tight loop: Red (write a failing test), Green…
- 06 What makes a good unit test (FIRST principles)? Good tests are FIRST: Fast (milliseconds), Isolated (no shared state, no order dependence), Repeatable (same result…
- 07 What are the differences between xUnit, NUnit and MSTest? All three are mature .NET test frameworks. xUnit is the modern default for new projects — no [SetUp]/[TearDown]…
- 08 How do you write a parameterized (data-driven) test? In xUnit, use [Theory] with [InlineData(...)] for simple constant cases or [MemberData(nameof(Cases))] / [ClassData]…
- 09 How do you use Moq to fake a dependency? Create a mock of the interface, configure its behaviour with Setup(...).Returns(...) (or ReturnsAsync), pass…
- 10 How do you test code that depends on DateTime.Now or randomness? Don't call DateTime.Now / Random / Guid.NewGuid() directly — inject them. Use an IClock abstraction (or .NET 8's…
- 11 What is WebApplicationFactory, and how do you write an integration test for a Web API? WebApplicationFactory<TEntryPoint> spins up your whole ASP.NET Core app in memory (real pipeline, real DI) and gives…
- 12 What is code coverage, and is 100% a good target? Code coverage measures which lines/branches run during the test suite. It tells you what's definitely not tested, but…
- 13 How do you test a private method? You generally don't test private methods directly — you test them through the public method that calls them. If a…
- 14 What is a flaky test, and how do you deal with it? A flaky test passes and fails without the code changing. Common causes: dependence on timing/Thread.Sleep, shared…
- 15 What is the difference between black-box and white-box testing? Black-box testing checks behaviour against the spec without looking at the code — you pick inputs from the…
- 16 What is a smoke test, and what is a regression test? A smoke test is a quick check that the critical paths work at all — “does the app start, can I log in, does the…
- 17 How do you keep integration tests isolated when they share a database? Options: wrap each test in a transaction and roll it back; use a library like Respawn to delete all data between…
- 18 What is Behaviour-Driven Development (BDD)? BDD describes behaviour in a structured natural language (Gherkin: Given… When… Then…) that both business and…
- 19 Should you use the EF Core in-memory provider for tests? The EF Core in-memory provider is not a relational database — it doesn't enforce constraints, doesn't do referential…
No questions match those filters
Try removing a filter or .
About these Unit Testing & TDD questions
Almost every mid-level .NET interview asks about testing, because it reveals how you actually write code. Interviewers want to hear that you know the difference between a unit test and an integration test, when to use a mock versus a stub versus a real object, how to structure a test (Arrange-Act-Assert), and how dependency injection makes code testable in the first place.
The .NET-specific parts are the frameworks (xUnit is the default for new projects, NUnit and MSTest are common in existing ones), Moq / NSubstitute for test doubles, and WebApplicationFactory for spinning up the app in-memory for integration tests.