Posted in by Tudipat 04:05Comments Off on How to Write Test Cases using C# and VS Tool?
How to Write Test Cases using C# and VS Tool?
Tudip
28 May 2019
Basics of Unit Testing
With the emergence of maximum programming, test-driven development and alternative agile ways, unit testing have become a very important part of nearly each and every development effort. At the same time, many applications provide application programming interfaces (APIs) to allow code-level access to functionality. These APIs, rather like the other interface into the merchandise, should be tested before they’re discharged to the end-users.
There are two ways to do the testing at code level they are API-testing and unit-testing and both target the code-level. In this blog, we are going to discuss unit-testing. Unit testing activities are owned by the development team. Developers are expected that they make unit tests for every of their code modules (these are typically categories, functions, stored procedures, or some other ‘atomic’ unit of code), and to ensure that every module passes its unit tests before the code becomes part of the build.
As a result, it helps the developers solidify their code. Many a time, this effort needs debugging and bug-fixing in the period.
Why Unit Testing?
The real-world application has the complex business rule, this means application code is complex and also we there may have more than one developer working on the project. The point I want to make clear is that there may be sections of the code in the application that is not developed by other developers and we do not fully understand the original purpose of the code. If we are making changes in that code or refactoring the code then we may introduce bugs. If we have unit tests and if we change something that is aiming to break the present functionality the unit test designed around that functionality can fail in real time. So looking at those test cases we can clearly figure out which scenarios we are breaking with the changes we have made. After adding massive changes you run test cases and if none of the test cases failed after your addition you can confidently say that I have not broken anything.
Benefits of Unit Testing
It is conducted by the development team hence easier to resolve the errors and saves development time.
It is a white box testing as the developers know about the functionality to be tested hence significantly reduces production bugs.
Makes the work more effective as bugs are caught in the early stages of development and reduce tester’s workload.
Provides documentation because it is written around the different scenarios of the implemented functionality so it makes complex code easy to understand.
Testing using Visual Studio Built in Tools
Introduction Visual studio has inbuilt testing framework powered by Microsoft. Microsoft has been providing these features since 2005. It is the simplest of all the framework presented by Microsoft.
It provides a simple method attribute structure where we use [TestClass] and [TestMethod] for performing the unit testing. To test the project we need to add a unit test project to your solution. Decorate the class with TestClass attribute which contains test methods and decorates the test methods with TestMethod attribute.
Steps to create a unit test project
Right click on the solution
Click Add
Click new project
Click Test under Visual C#
Select Unit Test Project
Give the name of a test project same as the project you are going to test
Click OK
It will create a new project in the current solution with Test extension.
Steps to add a reference to the main project in the test project
Click References under the test project.
Click Add Reference
Select the Project section
Mark the checkbox under the project name
Click OK
We need to add a reference to the main project in the test project so that we can test the functionality.
Naming convention we need to follow
Name the test class by adding the Tests as a prefix in the name of the actual class. For e.g., if the name of your actual class is Addition then the name of the test class will be AditionTests.
Name test methods in such a manner that one can easily understand what test method is doing. The naming pattern will be following, UnitOfWork_StateUnderTest_ExpectedBehavior.
UnitOfWOrk: Name of the test method being tested.
StateUnderTest: Represent the input values used for the test method.
Expected Behavior: the return value of the method. For e.g., Addition_PositiveNumbers_ReturnsPositiveNumber, Addition_NegativeNumbers_ReturnsNegativeNumber
The return type of test method is always void.
Add Summary on the top of each test method.
The pattern we follow to implement test method
The AAA(Arrange, Act, Assert) pattern is the most common pattern used for testing.
Arrange: In this section, we initialize the objects and sets the values of the variables that we need to pass to the method being tested.
Act: During this section we invoke the method being tested.
Assert: This section verifies that the method has behaved as expected.
Step by Step Guide to implementing the Test Cases
Create a Normal class for testing Demo class example:
Create a sample unit test project
Create a test class
Right-click inside the test method to run the test method
We can check the result in the test explorer
As we discussed in this blog about the basics of unit testing. We get to know how unit testing is an integral part of the software development lifecycle which saves development time and leads us to build a robust application. If we build such kind of application then only we can earn the client’s trust and put a smile on client’s and end user’s face.
All content provided on this blog is for informational purposes only. Tudip Technologies provides no endorsement and makes no representations as to accuracy, reliability, completeness, suitability or validity of any information or content on, distributed through or linked, downloaded or accessed from this site. Tudip Technologies will not be liable for any errors, omissions, or delays in this information or any losses, injuries, or damages arising from its display or use of the information on this site. All information is provided on an as-is basis without any obligation to make improvements or to correct errors or omissions. This site may contain links to other websites. Tudip Technologies makes no guarantees or promises regarding these websites and does not necessarily endorse or approve of their content. You may not modify any part of the blog. The inclusion of any part of this blog in another work, whether in printed or electronic or other form, or inclusion of any part of the blog in another website by linking, framing or otherwise without the express permission of Tudip Technologies is prohibited. This site may not be used for any illegal or illicit purpose and Tudip Technologies reserves the right, at its sole discretion and without notice of any kind, to remove anything posted to this site. By using this site, you hereby acknowledge that any reliance upon any materials shall be at your sole risk.