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 F# 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 F#:
// Using Xunit + FsUnit-style assertions
open Xunit
let add x y = x + y
[<Fact>]
let ``add returns sum`` () =
Assert.Equal(5, add 2 3)
[<Theory>]
[<InlineData(1, 2, 3)>]
[<InlineData(-1, 1, 0)>]
let ``add is correct for cases`` (a, b, expected) =
Assert.Equal(expected, add a b)
F# note: F# lets you name tests with double-backtick identifiers (` add returns sum `) for readable reports, and property-based testing via FsCheck is especially idiomatic given how easy pure functions are to test.
Try it yourself
Exercise: In F#, write three tests for a function that reverses a string.
Recap
You now understand why and what to test and can apply it in F#. Mark this lesson complete and continue to the next one.
