不能使用use-fixtures
爲測試自由定義組提供的安裝和拆卸的代碼,但是你可以使用:once
提供的安裝和拆卸代碼每個命名空間:
;; my/test/config.clj
(ns my.test.config)
(defn wrap-setup
[f]
(println "wrapping setup")
;; note that you generally want to run teardown-tests in a try ...
;; finally construct, but this is just an example
(setup-test)
(f)
(teardown-test))
;; my/package_test.clj
(ns my.package-test
(:use clojure.test
my.test.config))
(use-fixtures :once wrap-setup) ; wrap-setup around the whole namespace of tests.
; use :each to wrap around each individual test
; in this package.
(testing ...)
這種方式迫使某些耦合在設置和拆卸代碼和測試包所在的包之間,但通常這不是一個大問題。您可以在testing
部分中始終執行自己的手動換行操作,例如參見the bottom half of this blog post。
謝謝,我結束了使用這樣的:'(DEFN測試-NS-鉤[] (創建表) (把-4) (把-5) (GET-2) (得到-3) (GET-4) (掃描2) (掃描3) (SCAN-4) (落表))' – 2013-05-03 18:06:01
@DavidWilliams你不是真的應該把你的測試中包裹/鉤。整個裝置的重點在於你將設置代碼從測試中分離出來。這就是這個鉤子的論點(在我的例子中是f)。這是回調在你的燈具代碼中正確的位置運行測試(以及其他任何鉤子)。然後,像往常一樣定義測試(例如,使用deftest)。 – 2013-05-04 08:34:10
我同意ThornyDev博客文章是clojure.test的use-fixtures部分的一個不錯的總結。 http://thornydev.blogspot.com/2012/09/before-and-after-logic-in-clojuretest.html – 2014-07-10 18:05:25