. ├── deps.edn └── src └── your_group └── your_project └── core.clj
{:deps {org.clojure/clojure {:mvn/version "1.10.1"}} :paths ["src" "resources"]}
. ├── deps.edn ├── src │ └── your_group │ └── your_project │ └── core.clj └── test └── your_group └── your_project └── core_test.clj
(ns your-group.your-project.core-test (:require [clojure.test :refer [deftest is]])) (deftest core-test (is (= 2 (+ 1 1))))
:test
alias to deps.edn
{:deps {org.clojure/clojure {:mvn/version "1.10.1"}} :paths ["src" "resources"] :aliases {:test {:extra-paths ["test"]}}} ;; <- test alias
. ├── deps.edn ├── .dir-locals.el <-- added └── src/..
((clojure-mode . ((cider-clojure-cli-global-options . "-M:test"))))
C-c C-t l
or =M-x cider-test-run-loaded-tests=
Based on Clojure development with Spacemacs & Cider, How to setup deps.edn project with cider and some further resources, this is my summary to setup your tests.
One thing I like about deps.edn is that creating a new project does not need a bootstrapping command, you just create a deps.edn file and your source files.
Hint: To avoid learning the deps.edn file structure by heart, you can use a snippet. After the creation of the deps.edn file run
M-x cider-jack-in-clj
and thenM-x cljr-add-project-dependency
to add any dependencies.
. ├── deps.edn └── src └── your_group └── your_project └── core.clj
{:deps {org.clojure/clojure {:mvn/version "1.10.1"}} :paths ["src" "resources"]}
To add a new test, create your test folders and test file according to this structure:
. ├── deps.edn ├── src │ └── your_group │ └── your_project │ └── core.clj └── test └── your_group └── your_project └── core_test.clj
This follows the advice from The Clojure Style Guide - Testing to have a separate test
directory.
(ns your-group.your-project.core-test (:require [clojure.test :refer [deftest is]])) (deftest core-test (is (= 2 (+ 1 1))))
clojure.test is part of Clojure, you do not need any additional dependency.
:test
alias to deps.edn
{:deps {org.clojure/clojure {:mvn/version "1.10.1"}} :paths ["src" "resources"] :aliases {:test {:extra-paths ["test"]}}} ;; <- test alias
This adds an alias to your project. An alias is a shortcut to adjust your deps.edn file. In this example we add extra-paths
to our classpath
. Since we add extra-paths
for our tests we can exclude them from deployments. There is nothing special about the added path test
, it is just a folder we want to be able to condtionally add or remove from our classpath
.
During development we want to make sure that our tests are available so we want to use our alias to include the test
directory. When using Cider you typically run a project with cider-jack-in
. It is possible to add a default alias for all calls to cider-jack-in
by setting cider-clojure-cli-global-options
. You probably want to have different settings per project and you can use Directory Local Variables to achieve that. You create a Directory Local Variable by creating a .dir-locals.el
file in your project root.
. ├── deps.edn ├── .dir-locals.el <-- added └── src/..
This file will set the cider-clojure-cli-global-options
variable whenever clojure-mode
is active:
((clojure-mode . ((cider-clojure-cli-global-options . "-M:test"))))
Hint: A Directory Local Variable is only read when you revoke your buffer, so make sure to revoke your buffer manually with
M-x revert-buffer
if it was already open when you created.dir-locals.el
. Otherwise the variable will not be read.
In the documententation of Cider you can find several fine-grained commands to control which test case should run. To run all loaded tests in your project you can run M-x cider-test-run-loaded-tests
or use the shortcut C-c C-t l
.
Setting up tests with Cider & deps.edn involves quite many steps due to the flexibility this setup offers. I hope with this blog post, you can configure your first tests faster than I did. You can find the source of this minimal example on github.