Doing Unit test in Clojure Is Easy


While refactoring the live reload feature of Clay, I realized I'd better break long functions into smaller and functional ones (as many as I can), which is also a common practice in the clojure community.

Small pure functions not only are easy to verify on the development process (using a REPL), but also are easy to test. And unit tests are easy to write in clojure, just use deftest from clojure.test, a very basic one looks like this:

(deftest
  (is (= 3 (my-sum 1 2))))

To run the test, we can just run it with cider. After reloading the code (C-c C-k cider-load-buffer), we can use the command cider-test-run-test (C-c C-t t) to run the test at point. It's really handy!

/img/2024-12-07-cider-test-run-test.png
Run the test at point using cider

And we would also like to use some test runners in the CI/CD pipeline, here there are some dedicated solutions:

Clay uses the later one, you can check it out at build.clj if you're interested.


See also

comments powered by Disqus