Clojure测试:全球设备

时间:2017-04-24 10:10:11

标签: testing clojure

我有一些在我的项目中启动并关闭数据库的装置。

现在它看起来像这样:

(use-fixtures :once with-embedded-db)

在灯具本身,我有一个动态变量,我在不同的地方使用:

(def ^:dynamic *db*)

(defn with-embedded-db [f]
  (binding [*db* (db/connect args)]
    (f)
    (finally
      (db/clean-up *db)))

现在,假设db/connectdb/clean-up需要一些时间。

问题

当我使用lein test运行测试时,需要花费很长时间,不必花时间连接并断开每个命名空间的数据库。

问题

有没有办法设置全局灯具,这样当我运行lein test时,它只为所有测试命名空间调用一次

谢谢!

2 个答案:

答案 0 :(得分:2)

如果将此功能添加到leiningen本身会更好。如果不是PR,至少应该打开一张票。

以下解决方案很脏,但您可以了解并将其转换为更智能的内容。

;; profect.clj
:profiles 
 {:dev {:dependencies [[robert/hooke "1.1.2"]]

 :injections   [(require '[robert.hooke :as hooke])
  (defn run-all-test-hook [f & nss]
  (doall (map (fn [a]
   (when (intern a '*db*)
    (intern a '*db* "1234"))) nss))
  (apply f nss))
  (hooke/add-hook #'clojure.test/run-tests #'run-all-test-hook)
 ]}}

注意:leiningen本身在其核心使用robert / hooke。 然后在测试的某个地方:

(ns reagenttest.cli
    (:require [clojure.test :refer :all]))

(def ^:dynamic *db*) ;; should be defined in every NS where it is needed

(deftest Again
    (testing "new"
        (prn *db*)))

答案 1 :(得分:1)

使用circleci.test,它支持:global-fixtures

  

...无论您运行多少名称空间,都可以定义仅对整个测试运行运行一次的全局装置。