如果我通过use-fixtures
多个灯具,它会一个接一个地关闭它们:
(def ^:dynamic *path* nil)
(defn sun [f]
(println "sun setup" *path*)
(binding [*path* "sun"]
(f))
(println "sun cleanup"))
(defn rain [f]
(println "rain setup" *path*)
(binding [*path* "rain"]
(f))
(println "rain cleanup"))
(use-fixtures :once sun rain)
(deftest sometest1
(println "sometest1" *path*))
(deftest sometest2
(println "sometest2" *path*))
(run-tests)
Testing scratchpad.core
sun setup nil
rain setup sun
sometest1 rain
sometest2 rain
rain cleanup
sun cleanup
这很有用但是如何改为:
Testing scratchpad.core
sun setup nil
sometest1 sun
sometest2 sun
sun cleanup
rain setup nil
sometest1 rain
sometest2 rain
rain cleanup
答案 0 :(得分:4)
我写了一个简短的函数,可以做你想要的,juxt-fixture。希望这适合你。
(def ^:dynamic *path* nil)
(defn sun [f]
(println "sun setup" *path*)
(binding [*path* "sun"]
(f))
(println "sun cleanup"))
(defn rain [f]
(println "rain setup" *path*)
(binding [*path* "rain"]
(f))
(println "rain cleanup"))
(defn juxt-fixtures [& fixtures]
(fn [f]
(->> ((apply juxt fixtures) f)
dorun)))
(use-fixtures :once (juxt-fixtures sun rain))
(deftest sometest1
(println "sometest1" *path*))
(deftest sometest2
(println "sometest2" *path*))
输出:
sun setup nil
sometest2 sun
sometest1 sun
sun cleanup
rain setup nil
sometest2 rain
sometest1 rain
rain cleanup
答案 1 :(得分:1)
你不应该尝试使用clojure.test灯具。从旧的(但在许多方面仍然是最新的)post by Stuart Sierra:
只能为每个命名空间分配灯具。固定装置很晚了 除了clojure.test并且与其他人没有很好地融合 该设计。它们全局适用于整个事实 除了最简单的情况之外,命名空间使它们无用。
不希望它们全局应用于整个命名空间吗?您需要一个不同的解决方案。我会寻找一些东西:
使用功能中包含的testing
宏来定义常用测试,并为每个上下文使用deftest
来进行测试设置,运行common&具体测试,最后是清理:
(defn path-test [path]
(testing "test1"
(println "sometest1" path))
(testing "test2"
(println "sometest2" path)))
(deftest sun-test
(println "sun setup")
(let [path "sun"]
(path-test "sun")
(is (= :warm (get-temperature path))))
(println "sun cleanup"))
(deftest rain-test
(println "rain setup")
(let [path "rain"]
(path-test "rain")
(is (= :wet (get-floor path))))
(println "rain cleanup"))
可以在"callable-contexts" in thi-ng.geom tests中看到这种技术的一个例子。
另一个策略是使用test-ns-hook
。您可以找到示例here。
如果您想要测试多个实现,或者在多个上下文中测试一个实现,则这两种策略都有效。