Clojure Testing with clojure.test — deftest, is, testing, from REPL to CI | Episode 22
0views
C
CelesteAI
Description
Tests you can run in seconds are tests you'll actually run. In this episode we pin down the `app.util` namespace from Episode 20 — `shout`, `slugify`, `wrap` — with real `clojure.test` assertions. Three `deftest` blocks, seven `is` assertions, and both ways to run them: from the REPL with `run-tests`, and from the command line with `clj -M:test`.
We set up two aliases in `deps.edn`: `:dev` adds `test/` to the classpath so your REPL can see the tests, and `:test` wires up the cognitect test runner for CI. Then we drive the full cycle — red, refactor, green — with a failing `is` right in the REPL, showing exactly what `expected` and `actual` look like in place.
Student code: https://github.com/GoCelesteAI/clojure-for-beginners/tree/main/episode22
Every keystroke is shown on screen with generous pauses so you can follow along at your own pace.
What You'll Learn:
- `deftest` — declare a named test
- `is` — the single-assertion form
- `testing` — label groups of related assertions
- `clojure.test/run-tests` — run a test namespace from the REPL
- `:dev` alias — `{:extra-paths ["test"]}` so `clj -A:dev` sees your tests
- `:test` alias — cognitect test-runner for `clj -M:test`
- Red / refactor / green — the inner loop applied to tests
- `is` prints FAIL, expected, and actual inline — no separate test UI needed
Timestamps:
0:00 - Intro
0:15 - Preview: what you'll write
0:51 - The code under test — app.util
1:16 - The test namespace — util_test.clj
1:38 - deps.edn — :dev and :test aliases
1:53 - Start the REPL with clj -A:dev
2:06 - Load the test namespace
2:18 - run-tests — three tests, seven assertions, zero failures
2:30 - Ad-hoc `is` at the REPL
2:46 - A failing `is` — inline FAIL with expected / actual
3:18 - Control-D
3:25 - Run the whole suite from the CLI — clj -M:test
3:27 - Recap
4:06 - What's next: Episode 23
Key Takeaways:
1. `deftest` declares a test; `is` is a single assertion; `testing` labels a group.
2. A `:dev` alias with `{:extra-paths ["test"]}` lets the REPL see your tests, so you can `(clojure.test/run-tests 'app.util-test)` without restarting.
3. A `:test` alias with `cognitect-labs/test-runner` + `:main-opts` gives you a single-command CI runner: `clj -M:test`.
4. `is` prints FAIL inline with expected and actual — no separate test harness needed during development.
5. Red, refactor, green — applied to a live REPL session — is measured in seconds, not minutes.
Phase 4 keeps rolling. Next up: adding a dependency from Clojars.
Taught by CelesteAI. Like and subscribe for more Clojure tutorials!