我正在编写一些与数据库交互的单元测试。因此,在我的单元测试中使用设置和拆卸方法来创建然后删除表非常有用。然而there are no docs :O 关于使用装置方法。
这是我需要做的:
(setup-tests)
(run-tests)
(teardown-tests)
我目前对每次测试之前和之后运行设置和拆卸不感兴趣,而是在一组测试之前和之后运行一次。你如何做到这一点?
最佳答案
您不能使用 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 .
关于unit-testing - Clojure:如何在测试中使用夹具,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16350504/