Unit Testing is a method of testing the smallest piece of code called a unit. The main goal is to validate that each unit of the software performs as expected.
The most important point in Unit Testing, is not to create test cases for everything but to focus on the tests that affect the behavior of the system.
What are the Benefits of Unit Testing?
- Improves the quality of code.
- Detects software bugs early.
- Provides more reliable and reusable code.
- Reduces cost and development time.
- Facilitates changes so speeds up the coding process.
Unit Test Frameworks
Writing a Sample Unit Test
Let’s write a sample unit test with Kotlin. I have two class in this demo, MyClass and SampleUnitTest.
MyClass: includes a function that adds two numbers.
SampleUnitTest: includes methods to perform testing.
The assertEquals() method does the comparison. Checks whether the values given to it are equal or not.
Below is a output in which the methods of this class are tested.