Testing
Test pure logic with JUnit and coroutines with runTest.
What you will be able to do
- Test pure functions without Android.
- Use runTest for suspend functions.
- Use descriptive test names.
- Test the failure path too.
In plain English
Keep logic in pure functions so tests stay fast. Use JUnit assertions and kotlinx-coroutines-test for suspend code. Test behaviour, not implementation details.
import kotlin.test.Test
import kotlin.test.assertEquals
class LessonTest {
@Test
fun classifiesShortLessons() {
assertEquals("short", classify(9))
}
}Your turn
EasyAssert equality
Write a test assertion that classify(9) equals "short".
KOTLIN · starter
assertEquals()Check your understanding before moving on.
Knowledge checkQuestion 1
Which helper runs suspend functions in tests?
Select an answer to continue
Study tools
Make it yours.
Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.
Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign inFinished reading?
Lock in what you just learned.
0%
Mark this lesson complete to save your place and move one step closer to the certificate.