使用固定裝置包裹每個測試或所有測試

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)