Unit testing
The motivation for writing unit tests shouldn't just be a simple way to test existing code, there are more benefits to consider:
The fundamental idea behind unit tests is to write isolated testable code. To achieve this, it makes developers write smaller single-purpose methods and therefore avoid writing long spaghetti-like code.
Refactoring existing code is easier because you can rewrite and test code in smaller portions. Refactored code should pass the same tests as the original code.
Tests themselves can be considered as documentation and examples of expected behavior.
For this example, we'll write a console application that finds all numbers that are prime numbers, and all its digits are used only once. This is, for example, the number 941, because it's a prime number and contains each digit only once. On the other hand, the number 1,217 is a prime number but contains the digit 1 twice, so that's not what we're looking for.
Then, we'll extend our example with another method that...