How to Write Test Cases using C# and VS Tool?
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: 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.