Unit Testing in .NET MVC Web Application
In this article, we will explore Unit testing and its importance with demo,
When you are developing any software that software goes through different types of testing after completing all types of testing only software is going to run in the live environment. A lot of confusion between people on when to start testing software? The right way is to start testing software when you are developing it and the type of testing is called Unit Testing. Now next question may be what is unit testing,
Unit testing breaks the program down into the smallest part of code, usually function-level, and ensures that the function returns the value one expected by the developer. By using a unit testing framework, the unit tests become a separate entity of solution project which can then run automated tests on the program as it is being built.
Different Types of Unit Test Framework,
MSTest
NUnit
MbUnit
xUnit.net
MSTest is integrated with the visual studio so you don’t need to install any third-party package.
Benefits of Unit Testing:
1) Helps is finding the bugs early as we are running this test whenever we are developing any small functionality. If the Unit test is not there in the place we deploy the software into the Quality environment were tester may or may not able to find that bug and if we move this into production and end-user found that bug then resolving this bug will take a lot of time and efforts.
2) If we are following Unit testing in software it will help to reduce production bugs.
3) Unit test make complex code easy to understand
4) Its form of documentation because you defined it for each scenario
5) Easier to change in the future and builds confidence in the developer.
Example:
I have created one MVC application in .NET Framework which is used to track the result based on Name, DOB and Exam No now we will verify this function if its returning correct value or not as shown below,
The logic is defined for fetching details from the database as shown below,
Next step is to add the unit test project in an existing solution,
Right-click on solution -> Add -> New Project -> Select Unit test framework(.NET framework) ->create.
Next step is to write a test case for GetResultDetails Method which includes comparing expected and actual output,
As you noticed we used Assert.AreEqual to compare to objects then the question may be what is Assert,
Assert is a collection of helper classes to test various conditions within unit tests. If the condition being tested is not met, an exception is thrown.
There are different methods defined inside assert class some of them listed below,
Assert.AreEqual() -: AreEqual() checks that objects has equal type and value. Equal objects can exist in two different places in memory.
Assert.AreSame() -: AreSame() checks that they are the exact same object — if reference indicate the same object in memory.
Assert.AreNotEqual() -: AreNotEqual() checks if object are not equal.
Assert.AreNotSame() -: AreNotSame() checks if objects are not referencing to same object in memory.
Also, you need to define your connection string in App.config file of your test project as shown below this is required because if you are not doing this then you might end up by getting error like “No connection string named ‘ResultTrackerEntities’ could be found in the application config file in test project”,
Next step is to open text explorer and execute the test,
For opening text explorer go to test menu in visual studio -> Windows -> Test Explorer -> Run Test
Thank You, See you in the next article !!
You can reach out to me here,
LinkedIn: https://www.linkedin.com/in/vaibhav-bhapkar
Email: vaibhavbhapkar.medium@gmail.com