Test Integration

Cursive supports running tests with immediate feedback in the editor. This allows a tight feedback loop when developing tests using the REPL.

Currently the test integration only supports the default clojure.test.

You can run the tests by starting a REPL and then using Tools→REPL→Run Tests in Current NS in REPL - you can assign a shortcut to this action, of course. This will load the namespace containing the tests and any of its dependent namespaces (see here) in the REPL and then execute the tests from that namespace. You will see a summary of the test results printed to the REPL, and you'll see the test forms in the editor annotated with the results.

Each assertion will get a gutter icon showing the status of the test, and test errors or failures will also be shown in the error stripe margin at the right of the editor. You can get the details for a test failure from the tooltip on the error stripe or on the red underline in the editor.

If the test is a comparison failure between two strings or data structures, the tooltip will contain a link to open a diff view of the differences. You can also click directly on the gutter icon to see the diff.

If you're just working on a focused part of your test code, you can use Tools→REPL→Run Test Under Caret in REPL, which as the name implies will just run a single test instead of the whole namespace.

You can use Tools→REPL→Clear All Test Markers to clear all the test annotations from your editor. You can also use Tools→REPL→Re-Run Last Test Action in REPL to re-execute the last test action you executed. You don't have to be in the same namespace for this to work, so for a TDD-style workflow you can write your tests then re-run them while editing the code under test. Obviously you won't see the test annotations with this unless you have the test code open in another editor pane.

IntelliJ allows you to navigate from your source code to your tests and vice versa. The command for this is Ctrl+Shift+T (Navigate→Go to Test). Unfortunately in Clojure there isn't always a clear mapping from one to the other, so Cursive allows you to customise the mapping as follows.

The mapping between test and source namespaces and vars is defined in the settings under Settings→Languages and Frameworks→Clojure→Test Integration. There you can define a regular expression matching the namespace name, and a replacement string which will be used to create the test namespace from the source one. There’s a corresponding field for creation of test var names from source var names too. Here are some examples:

; This is the default mapping, matching CIDER's default
; Maps myapp.db.utils to myapp.db.utils-test

Match regex: .+
Replacement: $0-test
; Maps myapp.db.utils to myapp.db.test-utils

Match regex: ((.*)\.)?([^.]+)
Replacement: $1test-$3
; Maps myapp.db.utils to myapp.test.db.utils

Match regex: ([^.]+)(.*)
Replacement: $1.test$2

The navigation will try to operate at var level, and if not it will fall back to namespace level. So if you’re navigating to a test from within a defn, it will navigate to the corresponding deftest. If you’re not under a defn or some other type of var, it will navigate to the test namespace instead.

If you try to navigate to a test from source which doesn’t have a test defined (or at least, one Cursive can find using the above mapping) then Cursive will allow you to create a test. If you’re trying to navigate from within a var (e.g. a defn), it will create a test namespace if required or use an existing one, and then create a deftest within that. If you’re not within a var but are somewhere in the top-level of your file, it will just create a test namespace with no deftest.

Both the navigation and the test creation work for CLJ, CLJS and CLJC, and navigate to and create the corresponding file types as required.

Cursive uses IntelliJ's file templates to create test files when using this functionality. The file templates for new namespaces are found in the settings under Settings→Editor→File and Code Templates, and can be edited if you need to. There are templates for test namespaces, and when creating new namespaces under test roots these will be used instead of the standard namespace templates. There are separate templates for CLJ, CLJS and CLJC files for the two types.


Formatting Macro Support