不能使用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