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 Haskell 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 Haskell:
-- Property-based testing checks a law over generated inputs
import Test.QuickCheck
prop_reverseInvolutive :: [Int] -> Bool
prop_reverseInvolutive xs = reverse (reverse xs) == xs
main :: IO ()
main = quickCheck prop_reverseInvolutive -- runs ~100 random cases
Haskell note: Haskell's signature testing style is property-based (QuickCheck): instead of hand-picking cases you state an invariant and the library generates inputs and shrinks any counterexample to a minimal failing case.
Try it yourself
Exercise: In Haskell, write three tests for a function that reverses a string.
Recap
You now understand why and what to test and can apply it in Haskell. Mark this lesson complete and continue to the next one.
