Unit testing

Unit testing assumes that a program is logically divided into the smallest testable parts (units). The main purpose here is to check that each unit of an application works as it was designed. In object-oriented programming, a unit may be a method that belongs to a public interface of a class.

This approach increases the developer's confidence with regards to the fact that the code works in the right way while the codebase is changing. If unit tests are written correctly and we execute them every time code is changed, we are able to detect a defect. Unit testing also forces us to write reusable code because it can be modular to be tested. Another benefit of this approach is that a tester isn't needed in order to test code.

The following example shows how to test the additionOfThreeAndTwoShouldReturnFive method of the Calculator class:

class Tests {
@Test
fun additionOfThreeAndTwoShouldReturnFive() {
val calculator = Calculator()
assertEquals(5, calculator.addition(3, 2))
}
}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.148.144.228