2016-12-06 31 views
2

我已經加入此設置project.clj看到默認情況下禁用集成測試很多Clojure的項目:如何在當前命名空間中沒有運行測試時禁用測試裝置?

:test-selectors {:default (complement :integration) 
       :integration :integration} 

但是,如果一個命名空間中只包含集成測試,在它的燈具,當我運行lein test還跑!

例如,如果我跑lein new app test,使內容的core_test.clj這樣的:

(defn fixture [f] 
    (println "Expensive setup fixture is running") 
    (f)) 
(use-fixtures :once fixture) 

(deftest ^:integration a-test 
    (println "integration test running")) 

後來,當我跑lein test我看到燈具運行,即使沒有測試運行。

在clojure中處理這個問題的正確方法是什麼?

回答

3

一種方法是利用這樣的事實優勢,即使:once將燈具無論是否有考試在納秒運行或不運行時,:each燈具只會在每次實際運行的測試中運行。

而不是做實際的計算(或獲取資源,比如一個數據庫連接,或做任何副作用)在:once固定的,我們只是做第一個(我們想這樣做只有一次!):each夾具,例如做如下:

(def run-fixture? (atom true)) 

(defn enable-fixture [f] 
    (println "enabling expensive fixture...") 
    (try 
    (f) 
    (finally (reset! run-fixture? true)))) 

(defn expensive-fixture [f] 
    (if @run-fixture? 
    (do 
     (println "doing expensive computation and acquiring resources...") 
     (reset! run-fixture? false)) 
    (println "yay, expensive thing is done!")) 
    (f)) 

(use-fixtures :once enable-fixture) 
(use-fixtures :each expensive-fixture) 

(deftest ^:integration integration-test 
    (println "first integration test")) 

(deftest ^:integration second-integration-test 
    (println "second integration test")) 

lein test輸出將是如下(請注意如何enable-fixture已經運行,但不昂貴的expensive-fixture):

› lein test 

lein test fixture.core-test 
enabling expensive fixture... 

Ran 0 tests containing 0 assertions. 
0 failures, 0 errors. 

運行時lein test :integrationexpensive-fixture只能運行一次:

› lein test :integration 

lein test fixture.core-test 
enabling expensive fixture... 
doing expensive computation and acquiring resources... 
first integration test 
yay, expensive thing is done! 
second integration test 

Ran 2 tests containing 0 assertions. 
0 failures, 0 errors. 
1

無論測試是否運行,似乎燈具都在運行。然後,您可以將夾具功能置入測試中,以「手動」控制該設置/拆卸。僞代碼:完成沒有運行昂貴的計算

(defn run-all-tests [] 
    (do-test-1) 
    ... 
    (do-test-N)) 

(deftest ^:slow mytest 
    (do-setup) 
    (run-all-tests) 
    (do-teardown)) 
相關問題