use-fixtures
allows to wrap each deftest
in namespace with code that runs before and after test. It can be used for fixtures or stubbing.
Fixtures are just functions that take test function and run it with other necessary steps (before/after, wrap).
(ns myapp.test
(require [clojure.test :refer :all])
(defn stub-current-thing [body]
;; with-redefs stubs things/current-thing function to return fixed
;; value for duration of each test
(with-redefs [things/current-thing (fn [] {:foo :bar})]
;; run test body
(body)))
(use-fixtures :each stub-current-thing)
When used with :once
, it wraps whole run of tests in current namespace with function
(defn database-for-tests [all-tests]
(setup-database)
(all-tests)
(drop-database))
(use-fixtures :once database-for-tests)