Unit Testing has become an important part of software development no matter what language you’re developing with. Java Programmers use JUnit. PHP developers who use symfony are well familiar with the concept of unit testing and lime, although PHPUnit library is also available for PHP developers. .Net developers have a lot of choices for unit testing; NUnit and MSTest are some packages from the long list. In this article I am going to talk about goals of Unit Testing and cases you should test with Unit Testing, if you want to learn Unit Testing in c# read this article.
Testing has been and will be a part of software development. Testing the final product should be only for the purpose of Integration Testing and other methods of automatic testing such as Unit testing and UI testing should be invoked during the development. During the development, development team should insure the accuracy of every single component and part i.e. a single function, a single package or library or a User Interface should behave correctly. After the development of each part, integration testing will be run to insure the accuracy of system when all components are working together.
In order to achieve accuracy for a single module when other parts are not ready and you cannot run the application you need Unit Testing. Consider Model, View, Controller (MVC) architecture and a team of two that are working concurrently on Model and View. Well in this case, the Model section developer is writing queries and business logic while the UI developer is working on GUI. None of them can test the application because yet no complete application is ready for testing but if they just write codes and do not test it, the time for the final debugging will be a lot more comparing to a case where these developers write Unit Tests and insure the accuracy of their developed parts. In addition to the concurrent development with a good precision Unit Testing reduces from testing time after some changes. Changing a query to satisfy another condition, removing an input from a UI or etc. can impose bugs on other developed parts of an application and if you have no Automatic Testing in place you should manually run all the tests you previously did whereas with Unit Testing in place you easily may detect bugs which are results of changes.
The concept behind Unit-Testing is easy to comprehend; see if a single section works as expected. For example you want to write a query to select last services, guests have used in a hotel. Well if one asks you what you expect in return of this query you probably know; you can manually select those services from a list of 10 records in an excel file. OK Unit Testing is based on such test case i.e. you provide those 10 records for the method (which is written to fetch required Data) and tell the Unit Testing method to inspect if the written method returns those Data we expect.
As a rule of thumb, Unit Testing states that you don’t need to test third party packages and libraries just focus on what you have added. This means if your work is completing another teammate’s you don’t need to worry about the accuracy what he or she has developed. This is a crucial rule for Unit Testing because we expect unit testing to clarify exactly which unit is defected! You MUST write Unit Tests as independent as possible but this should not mislead you! Some people think using third party packages or frameworks insure the accuracy of what they develop and they do not need to write Unit Test at all. This is what happened when I asked one of my developers for unit tests and in response he told me I am using “Entity Framework” and I am sure Microsoft has tested it! “Entity Framework” is working fine but what we write is not and we should write Unit Tests to insure the accuracy of our codes.
Sometimes figuring what part of the code needs Unit Testing and decoupling unit test may be tricky. I tried to provide samples and points about what to unit test when you develop with C# ASP.NET MVC and use Entity Framework and I hope by reading that article you get and overview of what to Unit-Test in an ASP.NET developed application.