Free preview.You're sampling one lesson — enroll free to unlock all 8 lessons and track your progress.
Enroll free lesson
Why and What to Test
Why and What to Test
In this lesson — part of Testing Fundamentals — you'll learn why and what to test in Scala and why it matters in real work.
Why it matters
Tests prove your code works and keep it working as you change it.
Key ideas
- What to test
- Arrange-Act-Assert
- Running a test suite
- Good vs. brittle tests
In practice
Here's how it looks in idiomatic Scala:
import org.scalatest.funsuite.AnyFunSuite
class MathSuite extends AnyFunSuite:
test("addition works") {
assert(1 + 1 == 2)
}
test("list sums") {
assertResult(6)(List(1, 2, 3).sum)
}
test("throws on empty") {
assertThrows[NoSuchElementException](List.empty[Int].head)
}
Scala note: ScalaTest's assert is a macro that rewrites the expression so a failure prints the actual operand values (e.g. 1 == 2 shows both sides), giving rich diagnostics without a special matcher.
Try it yourself
Exercise: In Scala, write three tests for a function that reverses a string.
Recap
You now understand why and what to test and can apply it in Scala. Mark this lesson complete and continue to the next one.
