Junior — Middle
Could you explain the principles and purpose of unit testing in software development?
sobes.tech AI
Answer from AI
Unit testing is the process of verifying individual parts of the code (modules) for correctness. The main principles:
- Tests are isolated and verify only one functionality.
- Automation of testing for quick and repeatable runs.
- Rapid detection of errors at early stages of development.
Purpose of unit testing:
- Ensure code quality and stability during changes.
- Simplify debugging and maintenance.
- Increase confidence in the correctness of the program.
Example of a simple unit test in C# using NUnit:
[Test]
public void Sum_TwoNumbers_ReturnsCorrectResult()
{
var calculator = new Calculator();
int result = calculator.Sum(2, 3);
Assert.AreEqual(5, result);
}