What is Unit Testing and Why to use it?
Unit testing is a way to test each piece of your code which is called as unit. The idea behind the unit testing is to ensure the quality of each smaller unit. It verify the functional behavior of each unit in response to correct and incorrect cases of input data and verify any assumptions made by the code. Unit test cases are usually written by the developer who has written that code unit.
Developers can also use test frameworks as per their programming languages or frameworks of code, to make writing and running unit tests easier.
For example, .NET developers use NUnit, XUnit and third-party mock-object libraries such as Rhino mocks, Type mocks or NMock to write and running unit tests.
Visual Studio Test Explorer
You can download these third party testing extensions from
Visual Studio gallery.
Test Explorer views display all of your tests - passed, failed, not run yet, or skipped. You can also do filter or search for a specific test in Test Explorer view at the global level or by selecting one of the pre-defined filters.
Why Unit Testing?
Unit tests reduce number of bugs, provide accurate documentation, and improve design. There are following reasons to choose unit testing.
Allow you to make big changes into code quickly since you know it's tests are working as expected and when you make the changes you need to write and run tests again to get it work.
Reduce the number of bugs in production code.
Help you to understand the design of the code since to write tests you need to outline all the conditions with respect to your code and what outputs you are expecting from the code.
Allow refactoring of code at any time without fear of breaking existing code. In this way it make the coding process more agile.
Ensure functional behavior of your code since all the tests have been passed.
What do you think?
I hope you will enjoy Unit Testing for writing a better code. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.