@Test fun `adding 2 and 3 should return 5`() { val result = Calculator().add(2, 3) assertEquals(5, result) // Or even nicer: assertNotNull(result) } }
If you are reading this, you probably already love Kotlin for its conciseness and power. You use data classes , when expressions, and extension functions daily. But when it comes to testing that code, do you feel like you are still living in the past? Are you writing tests that look like Java 6? curso de testing kotlin
Use backticks to write sentences as test names. Your test reports will read like documentation. Module 2: The Game Changer – Kotest If you take only one thing from this curso , let it be Kotest . It is the flagship testing framework for Kotlin, replacing JUnit with a radically different syntax. Why Kotest? It supports Spec styles (BehaviorSpec, StringSpec, FreeSpec) and Property Testing out of the box. Example: The Behavior Spec class UserServiceTest : BehaviorSpec({ val service = UserService() given("A user with a valid email") { val email = "test@example.com" `when`("I call register") { val result = service.register(email) then("It should return a success message") { result shouldBe "User created" } and("The user should be stored in the DB") { service.exists(email) shouldBe true } } } }) Assertions: shouldBe vs shouldNotBe Kotest replaces assertEquals with infix functions: @Test fun `adding 2 and 3 should return