Unit Testing in C# WebAPI
Software applications have changed drastically over the years. Our expectations haven’t though. We expect them to run smoothly, meet our requirements, and have no bugs. If you haven’t worked in developing a product, it is difficult for you to imagine the efforts that go into making the product bug-free. And the biggest asset that the product developers have to achieve this is testing.
Proper testing of applications is necessary before launching it. The most basic type of software testing is unit testing. So what’s the difference between testing and unit testing and how is it done, let’s all find out in this post.
We are also going to understand unit testing in C# Web APIs. By the end of this post, you will have a firm grasp of software testing, unit testing, and how to write a code for a unit test.
Table of Content:
- What is software testing?
- What are the types of software testing?
- What is unit testing?
- What are the expectations from a unit test?
- How to write a unit test?
- Unit Testing in C# WebAPI?
- Wrapping Up
What is software testing?
Software testing is the process by which the functions of the software product are tested to check whether the software meets the expected requirements. It is also done to create a software product that has no errors. Automated or manual tools are used to test the key features of the software product. The main aim of software testing is to detect gaps, errors, or requirements that are missing but are expected from the software product.
What are the types of software testing?
Different types of software testing
Software testing can be broadly classified as:
Functional testing is the process of checking and validating the functional requirements expected from the software product. There are eight types of functional testing:
- Unit Testing
- Component Testing
- Integration Testing
- Smoke Testing
- Sanity Testing
- Regression Testing
- System Testing
- User Acceptance Testing (UAT)
In this post, will discuss in detail about unit testing and how unit testing can be done using C# programming platform.
Non-functional Testing or Performance Testing
Non-functional testing is done to check for non-functional attributes of the software product like performance, reliability, security, efficiency, readiness, etc.
After the successful launch of a software product it will need maintenance to keep running smoothly for a long time. Maintenance testing is done on the already deployed software product during its up-gradation or movement to other hardware.
What is unit testing?
In a complex and lengthy software application code, if the entire code is tested all at once, numerous errors will be shown together and it will become difficult to debug all of them. An easier approach is to test the code by parts i.e. to test every part or each unit of the code separately and fix the errors for each unit or part. Unit testing is testing every unit or part of the code developed in a component and verifying the results or output of each unit. Unit testing is one of the many functional testing methods that are done on any application code while creating the code or for the maintenance check on the code.
Nowadays unit testing can be done using the same programming language in which the code for the system under test (SUT) is written. So if the code of the application is written in C# then the unit tests can also be written by a programmer in C#.
Unit testing only checks for the errors in respective units of the code individually. Unit testing cannot check for any errors from dependencies of one unit on another. This is done by integration testing, that too only after unit testing is completed successfully.
What are the expectations from a unit test?
A unit test is the most basic functional test that is done on a software application code. There are certain expectations that a unit test code needs to fulfil:
- Readability: Unit tests should be readable to be easily understandable. This can be ensured by proper naming of the unit test so that the name can suggest the purpose of a unit test and a tester need not go into the unit test code to find out its purpose.
- Speed: The unit tests should execute fast so that less time is consumed in testing and more tests can be done in less time.
- Maintainability: Unit tests should be easily maintainable. A unit test should be written in a versatile way such that small changes in the code do not demand changing of the unit tests.
- Independency: Unit tests should be able to run independently so that anyone can run the tests without the need to access any database or external system.
How to write a unit test?
Now that we know what is unit testing, let us try to understand how to structure a unit test. The AAA (Arrange Act Assert) principal is used for writing a unit test:
- Arrange: This is where all the necessary arrangements for the test is done. All the objects needed for the unit test are created here and the assignment of these newly created objects is also done here.
- Act: This is where the unit of the system under test is executed. The output of the unit test is obtained here.
- Asset: This is where the output of the unit test is compared to the desired or predefined results. This part helps us to analyze the success of a unit test.
How Unit Testing is done in C# WebAPI?
Let us first add a class to a ‘Demo’ application. The class “First” has a string stored in the variable “Name” which will be tested.
Next, we make changes to the demo.aspx file to display the text stored in variable “Name” on the server.
Finally, we update the UnitTest.cs file to compare the value stored in variable “Name”. An object of the class First is created before comparing the strings. The below code successfully passes all test cases as both the strings match. Hence you can create your custom test cases using C# for testing your software.
By now you would have understood what is unit testing and its importance in the life cycle of a product. We have also seen how unit testing in C# WebAPI is done in this article. In this era where software applications have penetrated our lives significantly and play a major role in may of our day to day activities, it becomes very important to have a bug-free application. So go ahead and use C# WebAPI for unit testing and make your application bug free and help your users to enjoy a seamless experience with your application.