What is Unit Testing?

Emine İNAN
HardwareAndro
Published in
2 min readJul 11, 2020

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?

  1. Improves the quality of code.
  2. Detects software bugs early.
  3. Provides more reliable and reusable code.
  4. Reduces cost and development time.
  5. Facilitates changes so speeds up the coding process.

Unit Test Frameworks

Unit Test frameworks allow unit tests to be written quickly and easily. Some of the popular unit testing frameworks can be listed as: JUnit, NUnit, TestNG, Jasmin, Mocha.

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.

Test Output

--

--

Emine İNAN
HardwareAndro

Android Developer by day, Android Developer by night. @Huawei