Search⌘ K
AI Features

Writing Empirical Tests

Explore how to write empirical tests in Kotlin to verify that methods meet expected behavior and maintain functionality over time. This lesson guides you through defining test properties, creating simple to complex tests, and using tests to drive code design. Understand how to handle edge cases effectively and improve code quality through systematic testing.

We'll cover the following...

Empirical tests

Empirical tests are common in unit testing—you call a method and verify that it did what you expected. Such tests help us to verify that, as code evolves, the expectations are still met and the code continues to work as intended.

Empirical tests are useful for functions and methods that are deterministic and don’t have any dependencies that hold state. We’ll create a few empirical tests for the Airport class now.

First, let’s create some properties in the test suite using the yet-to-be-written Airport class. For this, within the AirportTest class, let’s define a few sample properties before the init() function:

// AirportTest.kt
val iah = Airport("IAH", "Houston", true)
val iad = Airport("IAD", "Dulles", false)
val ord = Airport("ORD", "Chicago O'Hare", true)

We can write a new test, right after the canary test, to exercise the properties of Airport, like so:

Kotlin
"create Airport" {
iah.code shouldBe "IAH"
iad.name shouldBe "Dulles"
ord.delay shouldBe true
}

Try running the build, and you’ll notice it fails because the Airport class doesn’t exist yet. Let’s define the ...