Testing any product system becomes necessary to verify its usability and validate its intended purpose. If this testing procedure can be executed on every modification of the product system as a sanity check, it becomes a measure to alleviate chances of any discrepancy introduced.
Thus, Unit testing in software development plays a important role to verify intend and pacify chances of bug instigation on the very basic unit of software. Unit testing is a level of software testing where individual units of software are tested against logical flaws. In object oriented programming, the smallest unit is usually a ‘method‘.
How to start writing Unit Tests
- Identify logical unit, usually a method or a property which needs to be tested.
- Create setup in the unit test class to invoke that method.
- Initiate the call/trigger to the method in focus from your test method.
- And the last step, compare the actual results/outcome of the call to the expected result/outcome.
We can start with a simple implementation of a calculator and its unit tests.
Lets take basic methods of a calculator, Add and Subtract. I will show you how we can create multiple tests cases on these simple methods.
Now, we will see the unit tests corresponding to these two methods.
As you can see, these test cases are logical scenario based, it is not enough to test the method with only positive scenario as our implementation usually breaks for negative and borderline scenario, so it becomes necessary for a effective unit test to verify these corner cases.
Also, as a better design practice, try not to club two or more ‘Assert‘ in one test method, one assertion in one method makes easier to identify and pin-point the failure scenario when tests fail.
Further, to increase readability few things should be kept in mind, first, unit test method name should preferably include name of method under test, scenario and expected result; second, follow AAA pattern i.e. divide your test method in Arrange-Act-Assert sections.
Arrange – creating instances, Act – invoking the method under focus and Assert – comparing expected and actual results.
Here unit tests are written using one of the most popular and intuitive unit testing framework in .NET – MsTest. NUnit is also a good option but MsTest is preferred because of its seamless integration with Visual Studio IDE.
Find complete project here.

Please update the post soon!
LikeLike