Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure - integration test organization

What is the typical way to structure integration and unit tests in Clojure? So far I have a test/ folder that follows the structure of my src/ directory. But I would like to split these into unit tests and integration tests.

Is there a recommended strategy? Annotations in tests? sub-folders test/unit and test/integration? Environment variables?

Note: I use boot instead of leiningen in case it matters

like image 393
nha Avatar asked Dec 10 '16 13:12

nha


1 Answers

The organization is up to you. You can either make a separate directory tree for the integration tests, have separate files in the same directory tree, or have unit tests and integration tests in the same test source files.

The only real difference between unit & integration tests is

  1. How long they take to run
  2. Extra setup with other pieces of your system (this also implies they are slower and/or more difficult to run).

So, either way, all of the integration tests should be labelled with ^:integration metadata on each function. This technique is also useful with "slow" unit tests.

A "slow" test can be marked like this one (which uses clojure.test.check generative testing):

(tst/defspec ^:slow round-trip-bytes 9999
  (prop/for-all [orig gen/bytes]
    (let [string-b64  (b64/encode-bytes->str  orig)
          result      (b64/decode-str->bytes  string-b64) ]
      (assert (every? b64/base64-chars (seq string-b64)))
      (assert (types/byte-array? result))
      (= (seq orig) (seq result)))))

Then in your project.clj (not boot, I know), specify:

:test-selectors { :default    (complement :slow)
                  :slow       :slow }

Then when you say lein test the tests marked with ^:slow will be skipped, and when you say lein test :all all of the tests (including the "slow" ones) will be run.

Note that there is nothing special about the keyword :slow. You can substitute any keyword you wish, such as :integration.

I have not used boot much but I assume a similar technique is available.

like image 51
Alan Thompson Avatar answered Nov 04 '22 10:11

Alan Thompson