Master technical and career interviews with structured answers—short definition, real examples, pitfalls, and how to answer in 60–90 seconds.
Short answer: MSTest is Microsoft's official unit testing framework for .NET. It’s tightly integrated with Visual Studio and is a good choice for teams using Microsoft tooling and wanting a straightforward, supported tes…
Short answer: [TestClass] public class CalculatorTests { [TestMethod] public void Add_ReturnsSum() Example code { var calculator = new Calculator(); var result = calculator.Add(2, 3); Assert.AreEqual(5, result); } } Real…
Short answer: MSTest is Microsoft's official framework; NUnit and xUnit are community-driven. MSTest uses [TestClass] and [TestMethod] attributes; xUnit uses [Fact], NUnit uses [Test]. MSTest has less flexibility in data…
Short answer: ttributes? [TestInitialize] runs code before each test method to set up prerequisites. [TestCleanup] runs after each test method to clean up resources. [TestInitialize] public void Setup() { /* setup code *…
Short answer: [TestInitialize] runs code before each test method to set up prerequisites. [TestCleanup] runs after each test method to clean up resources. [TestInitialize] public void Setup() { /* setup code */ } [TestCl…
Short answer: [TestMethod] [ExpectedException(typeof(DivideByZeroException))] public void Divide_ByZero_ThrowsException() Example code { var calculator = new Calculator(); calculator.Divide(10, 0); } Real-world example (…
Short answer: How? Yes, MSTest supports data-driven tests using [DataTestMethod] and [DataRow] attributes: [DataTestMethod] [DataRow(2, 3, 5)] [DataRow(10, 20, 30)] public void Add_ReturnsSum(int a, int b, int expected)…
Short answer: Yes, MSTest supports data-driven tests using [DataTestMethod] and [DataRow] ttributes: [DataTestMethod] [DataRow(2, 3, 5)] [DataRow(10, 20, 30)] public void Add_ReturnsSum(int a, int b, int expected) { var…
Short answer: Use the [TestCategory("CategoryName")] attribute to group tests for filtering. [TestMethod] [TestCategory("Integration")] public void IntegrationTest() { } Real-world example (ShopNest)…
Short answer: Use the [Ignore("Reason")] attribute on the test method or class. [Ignore("Test is temporarily disabled")] [TestMethod] public void SkippedTest() { } Real-world example (ShopNest) ShopNe…
Short answer: Use dotnet test CLI for .NET Core MSTest projects: dotnet test YourTestProject.csproj In Azure DevOps, use the Visual Studio Test task or the DotNetCoreCLI task to run tests during builds and releases. Real…
Short answer: MSTest itself does not provide mocking capabilities. Use mocking libraries like Moq or NSubstitute alongside MSTest to mock dependencies. Example with Moq: var mockService = new Mock<IService>(); Exam…
Short answer: Mocking is creating fake objects that simulate the behavior of real dependencies. It’s important because it isolates the unit under test, avoids reliance on external systems, improves test speed, and allows…
Short answer: Moq is a popular, open-source mocking library for .NET that enables developers to create mock objects, set expectations, and verify interactions in unit tests, helping isolate dependencies easily. Real-worl…
Short answer: var mockService = new Mock<IService>(); var service = mockService.Object; // use this in your test Example code var mockService = new Mock<IService>(); var service = mockService.Object; // use t…
Short answer: mockService.Setup(s => s.GetData()).Returns("Mocked Data"); This configures the mock to return "Mocked Data" when GetData() is called. Example code mockService.Setup(s => s.GetData…
Short answer: mockService.Verify(s => s.Save(), Times.Once); This asserts that Save() was called exactly once during the test. Example code mockService.Verify(s => s.Save(), Times.Once); This asserts that Save() wa…
Short answer: mockService.SetupGet(s => s.Name).Returns("MockName"); This sets up a mocked property getter to return a specific value. Example code mockService.SetupGet(s => s.Name).Returns("MockName…
Short answer: How? mockService.Setup(s => s.GetData(It.IsAny<int>())).Returns("Data"); You can specify argument matchers like It.IsAny<T>() to mock methods with parameters. Example code How? mock…
Short answer: mockService.Setup(s => s.GetData(It.IsAny<int>())).Returns("Data"); You can specify argument matchers like It.IsAny<T>() to mock methods with parameters. Explain a bit more mockServ…
Short answer: mockService.Setup(s => s.GetDataAsync()).ReturnsAsync("Async Data"); Moq supports ReturnsAsync to mock async methods returning Task<T>. Example code mockService.Setup(s => s.GetDataAsy…
Short answer: Cannot mock non-virtual or sealed methods/classes without special tooling. Over-mocking can make tests fragile and hard to maintain. Complex mocks can hide design issues in the code. Mocks don’t guarantee r…
Short answer: Use tools like Microsoft Fakes or JustMock for advanced mocking of sealed classes or non-virtual methods. Alternatively, refactor code to depend on interfaces or make methods virtual for easier mocking. Tes…
Short answer: TDD is a software development approach where you write tests before writing the actual code. It follows a short, repetitive cycle of writing a failing test, implementing code to pass the test, and then refa…
Short answer: Ensures code is testable and well-designed. Helps catch bugs early. Provides a safety net for refactoring. Encourages simple, focused code. Improves documentation through tests. Real-world example (ShopNest…
Unit Testing C# Programming Tutorial · Testing
Short answer: MSTest is Microsoft's official unit testing framework for .NET. It’s tightly integrated with Visual Studio and is a good choice for teams using Microsoft tooling and wanting a straightforward, supported testing solution without external dependencies.
ShopNest unit tests cover pricing and discount rules so a bad coupon change fails in CI before customers see it.
Unit Testing C# Programming Tutorial · Testing
Short answer: [TestClass] public class CalculatorTests { [TestMethod] public void Add_ReturnsSum()
{
var calculator = new Calculator();
var result = calculator.Add(2, 3); Assert.AreEqual(5, result); }
}
ShopNest unit tests cover pricing and discount rules so a bad coupon change fails in CI before customers see it.
Unit Testing C# Programming Tutorial · Testing
Short answer: MSTest is Microsoft's official framework; NUnit and xUnit are community-driven. MSTest uses [TestClass] and [TestMethod] attributes; xUnit uses [Fact], NUnit uses [Test]. MSTest has less flexibility in data-driven tests compared to NUnit’s [TestCase] and xUnit’s [Theory]. xUnit promotes constructor-based setup, MSTest uses [TestInitialize].
ShopNest unit tests cover pricing and discount rules so a bad coupon change fails in CI before customers see it.
Unit Testing C# Programming Tutorial · Testing
Short answer: ttributes? [TestInitialize] runs code before each test method to set up prerequisites. [TestCleanup] runs after each test method to clean up resources. [TestInitialize] public void Setup() { /* setup code */ } [TestCleanup] public void Cleanup() { /* cleanup code */ }
ShopNest unit tests cover pricing and discount rules so a bad coupon change fails in CI before customers see it.
Unit Testing C# Programming Tutorial · Testing
Short answer: [TestInitialize] runs code before each test method to set up prerequisites. [TestCleanup] runs after each test method to clean up resources. [TestInitialize] public void Setup() { /* setup code */ } [TestCleanup] public void Cleanup() { /* cleanup code */ }
ShopNest unit tests cover pricing and discount rules so a bad coupon change fails in CI before customers see it.
Unit Testing C# Programming Tutorial · Testing
Short answer: [TestMethod] [ExpectedException(typeof(DivideByZeroException))] public void Divide_ByZero_ThrowsException()
{
var calculator = new Calculator(); calculator.Divide(10, 0); }
Arrange a cart with 2 items → Act Checkout() → Assert total and that payment was called once.
Unit Testing C# Programming Tutorial · Testing
Short answer: How? Yes, MSTest supports data-driven tests using [DataTestMethod] and [DataRow] attributes: [DataTestMethod] [DataRow(2, 3, 5)] [DataRow(10, 20, 30)] public void Add_ReturnsSum(int a, int b, int expected)
{
var calculator = new Calculator();
var result = calculator.Add(a, b); Assert.AreEqual(expected, result); }
ShopNest unit tests cover pricing and discount rules so a bad coupon change fails in CI before customers see it.
Unit Testing C# Programming Tutorial · Testing
Short answer: Yes, MSTest supports data-driven tests using [DataTestMethod] and [DataRow] ttributes: [DataTestMethod] [DataRow(2, 3, 5)] [DataRow(10, 20, 30)] public void Add_ReturnsSum(int a, int b, int expected) { var calculator = new Calculator(); var result = calculator.Add(a, b); ssert.AreEqual(expected, result); } Yes, MSTest supports data-driven tests using… [DataTestMethod] and [DataRow] ttributes: [DataTestMethod]…
[DataRow(2, 3, 5)] [DataRow(10, 20, 30)] public void Add_ReturnsSum(int a, int b, int expected) { var calculator = new Calculator(); var result = calculator.Add(a, b); ssert.AreEqual(expected, result); }
ShopNest unit tests cover pricing and discount rules so a bad coupon change fails in CI before customers see it.
Unit Testing C# Programming Tutorial · Testing
Short answer: Use the [TestCategory("CategoryName")] attribute to group tests for filtering. [TestMethod] [TestCategory("Integration")] public void IntegrationTest() { }
ShopNest unit tests cover pricing and discount rules so a bad coupon change fails in CI before customers see it.
Unit Testing C# Programming Tutorial · Testing
Short answer: Use the [Ignore("Reason")] attribute on the test method or class. [Ignore("Test is temporarily disabled")] [TestMethod] public void SkippedTest() { }
ShopNest unit tests cover pricing and discount rules so a bad coupon change fails in CI before customers see it.
Unit Testing C# Programming Tutorial · Testing
Short answer: Use dotnet test CLI for .NET Core MSTest projects: dotnet test YourTestProject.csproj In Azure DevOps, use the Visual Studio Test task or the DotNetCoreCLI task to run tests during builds and releases.
ShopNest unit tests cover pricing and discount rules so a bad coupon change fails in CI before customers see it.
Unit Testing C# Programming Tutorial · Testing
Short answer: MSTest itself does not provide mocking capabilities. Use mocking libraries like Moq or NSubstitute alongside MSTest to mock dependencies. Example with Moq: var mockService = new Mock<IService>();
mockService.Setup(s => s.GetData()).Returns("Test Data"); Moq Framework (Mocking)
When testing ShopNest’s order service, mock IPaymentGateway so tests do not call Razorpay.
Unit Testing C# Programming Tutorial · Testing
Short answer: Mocking is creating fake objects that simulate the behavior of real dependencies. It’s important because it isolates the unit under test, avoids reliance on external systems, improves test speed, and allows testing specific scenarios and edge cases.
Unit Testing C# Programming Tutorial · Testing
Short answer: Moq is a popular, open-source mocking library for .NET that enables developers to create mock objects, set expectations, and verify interactions in unit tests, helping isolate dependencies easily.
ShopNest unit tests cover pricing and discount rules so a bad coupon change fails in CI before customers see it.
Unit Testing C# Programming Tutorial · Testing
Short answer: var mockService = new Mock<IService>(); var service = mockService.Object; // use this in your test
var mockService = new Mock<IService>();
var service = mockService.Object; // use this in your test
When testing ShopNest’s order service, mock IPaymentGateway so tests do not call Razorpay.
Unit Testing C# Programming Tutorial · Testing
Short answer: mockService.Setup(s => s.GetData()).Returns("Mocked Data"); This configures the mock to return "Mocked Data" when GetData() is called.
mockService.Setup(s => s.GetData()).Returns("Mocked Data"); This configures the mock to return "Mocked Data" when GetData() is called.
ShopNest unit tests cover pricing and discount rules so a bad coupon change fails in CI before customers see it.
Unit Testing C# Programming Tutorial · Testing
Short answer: mockService.Verify(s => s.Save(), Times.Once); This asserts that Save() was called exactly once during the test.
mockService.Verify(s => s.Save(), Times.Once); This asserts that Save() was called exactly once during the test.
When testing ShopNest’s order service, mock IPaymentGateway so tests do not call Razorpay.
Unit Testing C# Programming Tutorial · Testing
Short answer: mockService.SetupGet(s => s.Name).Returns("MockName"); This sets up a mocked property getter to return a specific value.
mockService.SetupGet(s => s.Name).Returns("MockName"); This sets up a mocked property getter to return a specific value.
When testing ShopNest’s order service, mock IPaymentGateway so tests do not call Razorpay.
Unit Testing C# Programming Tutorial · Testing
Short answer: How? mockService.Setup(s => s.GetData(It.IsAny<int>())).Returns("Data"); You can specify argument matchers like It.IsAny<T>() to mock methods with parameters.
How? mockService.Setup(s => s.GetData(It.IsAny<int>())).Returns("Data"); You can specify argument matchers like It.IsAny<T>() to mock methods with parameters.
When testing ShopNest’s order service, mock IPaymentGateway so tests do not call Razorpay.
Unit Testing C# Programming Tutorial · Testing
Short answer: mockService.Setup(s => s.GetData(It.IsAny<int>())).Returns("Data"); You can specify argument matchers like It.IsAny<T>() to mock methods with parameters.
mockService.Setup(s => s.GetData(It.IsAny<int>())).Returns("Data"); You can specify argument matchers like It.IsAny<T>() to mock methods with parameters. mockService.Setup(s => s.GetData(It.IsAny<int>())).Returns("Data"); You can specify argument matchers like It.IsAny<T>() to mock methods with parameters. mockService.Setup(s => s.GetData(It.IsAny<int>())).Returns("Data"); You can specify argument matchers like It.IsAny<T>() to mock methods with parameters.
When testing ShopNest’s order service, mock IPaymentGateway so tests do not call Razorpay.
Unit Testing C# Programming Tutorial · Testing
Short answer: mockService.Setup(s => s.GetDataAsync()).ReturnsAsync("Async Data"); Moq supports ReturnsAsync to mock async methods returning Task<T>.
mockService.Setup(s => s.GetDataAsync()).ReturnsAsync("Async Data"); Moq supports ReturnsAsync to mock async methods returning Task<T>.
When testing ShopNest’s order service, mock IPaymentGateway so tests do not call Razorpay.
Unit Testing C# Programming Tutorial · Testing
Short answer: Cannot mock non-virtual or sealed methods/classes without special tooling. Over-mocking can make tests fragile and hard to maintain. Complex mocks can hide design issues in the code. Mocks don’t guarantee real-world integration correctness.
Unit Testing C# Programming Tutorial · Testing
Short answer: Use tools like Microsoft Fakes or JustMock for advanced mocking of sealed classes or non-virtual methods. Alternatively, refactor code to depend on interfaces or make methods virtual for easier mocking. Test Driven Development (TDD)
When testing ShopNest’s order service, mock IPaymentGateway so tests do not call Razorpay.
Unit Testing C# Programming Tutorial · Testing
Short answer: TDD is a software development approach where you write tests before writing the actual code. It follows a short, repetitive cycle of writing a failing test, implementing code to pass the test, and then refactoring.
ShopNest unit tests cover pricing and discount rules so a bad coupon change fails in CI before customers see it.
Unit Testing C# Programming Tutorial · Testing
Short answer: Ensures code is testable and well-designed. Helps catch bugs early. Provides a safety net for refactoring. Encourages simple, focused code. Improves documentation through tests.
ShopNest unit tests cover pricing and discount rules so a bad coupon change fails in CI before customers see it.