使用固定装置包裹每个测试或所有测试

use-fixtures 允许使用在测试之前和之后运行的代码将每个 deftest 包装在命名空间中。它可用于固定装置或短截线。

夹具只是具有测试功能的功能,并通过其他必要步骤(前/后,换行)运行。

(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)

当与:once 一起使用时,它会在当前命名空间中使用函数包装整个测试

(defn database-for-tests [all-tests]
  (setup-database)
  (all-tests)
  (drop-database))

(use-fixtures :once database-for-tests)