2017-04-24 66 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

...您可以定義全局裝備那些只在整個試車運行一次,不管有多少你運行的命名空間。