2017-04-24 129 views
2

我有一些启动和关闭我的项目中的数据库的灯具。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

如果该功能被添加到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*))) 
+0

哦我一定要检查一下! :)我试图写这个插件,我想也使用'robert/hooke',这看起来很有意义......我会让你知道它是否工作 – andrusieczko

+0

我最终使用了类似于你的代码的东西,十分感谢! – andrusieczko

1

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

...您可以定义全局装备那些只在整个试车运行一次,不管有多少你运行的命名空间。