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!

πŸ” Navigation

πŸ’¬ Feedback
πŸš€ Start Learning
Share:

Tags: