Running unit tests in Cider
Run tests from the buffer containing the test source code buffer, as this ensures all test code is loaded and the latest version of the tests are run.
SPC p a switches between source and test buffers.
||load buffer and run tests|
Tests can be run from the source code buffer, however, only test code that has been evaluated in the REPL will run.
, to invoke tests commands from the REPL buffer
Auto Unit Test Mode
, T tto toggle auto test mode
e b` will automatically run tests.
by re-evaluating the buffer with auto test mode on it to automatically run tests whenever that namespace is loaded.
Cider test results buffer
Tests can be run from the
|g||Run test at point.|
|n||Run tests for current namespace.|
|l||Run tests for all loaded namespaces.|
|p||Run tests for all project namespaces. This loads the additional namespaces.|
|f||Re-run test failures/errors.|
|M-p||Move point to previous test.|
|M-n||Move point to next test.|
|t or M-.||Jump to test definition.|
|d||Display diff of actual vs expected.|
|e||Display test error cause and stacktrace info.|
Re-write for Spacemacs
From cider documentation: running tests
Certain aspects of the test execution behavior are configurable:
If your tests are not following the
some.ns-testnaming convention you can customize the variable
cider-test-infer-test-ns. It should be bound to a function that takes the current namespace and returns the matching test namespace (which may be the same as the current namespace).
If your individual tests are not defined by
defspec, CIDER will not recognize them when searching for a test at point in
cider-test-run-test. You can customize the variable
cider-test-defining-formsto add additional forms for CIDER to recognize as individual test definitions.
If you want to view the test report regardless of whether the tests have passed or failed:
(setq cider-test-show-report-on-success t)
Running tests automatically (test-driven development)
CIDER provides a minor-mode that automatically runs all tests for a namespace
whenever you load a file (with C-c C-k). You can toggle it
manually with M-x
cider-auto-test-mode, or you can use:
This is completely equivalent to manually typing C-c C-t C-n every time you load a Clojure buffer. Also, as described above before, CIDER is smart enough to figure out the namespace containing the tests.
Using cider-test with alternative test libraries
clojure.test machinery is designed to be pluggable. Any test library
can implement it if so desired, and therefore leverage
instance, test.check does this, and
defspec just like
As a test framework author, supporting the built-in
cider-test) is pretty straightforward:
- Assoc each test fn as
:testmetadata on some var. These are what get run.
- Implement the
clojure.test/reportmultimethod to capture the test results.
test.check library is a good example here. It was also designed completely
clojure.test. It just adds compatibility in this