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 Clojure 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 Clojure:
(ns myapp.core-test
(:require [clojure.test :refer [deftest is testing are]]))
(deftest arithmetic
(testing "addition"
(is (= 4 (+ 2 2)))
(is (thrown? ArithmeticException (/ 1 0))))
(are [x y] (= x y) ; tabular assertions
2 (+ 1 1)
6 (* 2 3)))
Clojure note: clojure.test ships in core; is wraps any boolean expression (with special forms like (thrown? ...)), and are expands a table of cases into multiple is assertions to cut boilerplate.
Try it yourself
Exercise: In Clojure, write three tests for a function that reverses a string.
Recap
You now understand why and what to test and can apply it in Clojure. Mark this lesson complete and continue to the next one.
