clojure 用夹具将每个测试或所有测试包装起来

示例

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)