What is unit testing?
π‘ Concept: Unit Testing
Unit testing involves testing individual units or components of software to ensure they work as intended.
π Quick Intro
It helps identify bugs early, improves code quality, and supports refactoring.
π§ Analogy
Like testing each ingredient before baking a cake to ensure quality of the final product.
π§ Technical Explanation
- Unit tests focus on small pieces of code, typically methods or classes.
- Common frameworks include MSTest, NUnit, and XUnit.
- Tests are automated and run frequently.
- Tests should be independent, repeatable, and fast.
- Supports Test-Driven Development (TDD) practices.
π― Use Cases
- β Verifying business logic correctness.
- β Preventing regressions during development.
- β Supporting continuous integration.
- β Facilitating safe refactoring.
π» Code Example
// Example using MSTest
[TestClass]
public class CalculatorTests {
[TestMethod]
public void Add_ReturnsSum() {
var calc = new Calculator();
var result = calc.Add(2, 3);
Assert.AreEqual(5, result);
}
}
public class Calculator {
public int Add(int a, int b) => a + b;
}

β Interview Q&A
Q1: What is unit testing?
A: Testing individual components of software.
Q2: Name some unit testing frameworks.
A: MSTest, NUnit, XUnit.
Q3: Why is unit testing important?
A: Catches bugs early and supports refactoring.
Q4: What is Test-Driven Development?
A: Writing tests before code implementation.
Q5: What qualities should unit tests have?
A: Independent, repeatable, fast.
Q6: Can unit tests be automated?
A: Yes.
Q7: What is mocking?
A: Simulating dependencies in tests.
Q8: Should unit tests depend on external resources?
A: No.
Q9: How does unit testing help continuous integration?
A: By verifying changes donβt break builds.
Q10: What is a test fixture?
A: Setup for tests.
π MCQs
Q1. What is unit testing?
- Testing complete application
- Testing individual components
- Testing UI
- Testing database
Q2. Name a unit testing framework.
- JUnit
- MSTest
- Selenium
- Postman
Q3. Why is unit testing important?
- Slows development
- Catches bugs early
- Requires manual testing
- For UI only
Q4. What is TDD?
- Writing docs before code
- Writing tests before code
- Writing code before tests
- Skipping tests
Q5. Should unit tests be independent?
- No
- Yes
- Sometimes
- Rarely
Q6. Can unit tests be automated?
- No
- Yes
- Sometimes
- Rarely
Q7. What is mocking?
- Database connection
- Simulating dependencies
- UI testing
- Performance testing
Q8. Should unit tests depend on external resources?
- Yes
- No
- Sometimes
- Always
Q9. How does unit testing help CI?
- Slows CI
- Prevents build breaks
- No effect
- For deployment only
Q10. What is test fixture?
- Cleanup code
- Test runner
- Setup for tests
- Test report
π‘ Bonus Insight
Unit testing is a cornerstone for quality assurance and maintainable software development.
π PDF Download
Need a handy summary for your notes? Download this topic as a PDF!