在clojure.test中有一个宏允许同时测试几个灯具:
are
.
在clojure.test中,有可能将宏与测试结合起来吗?
IE浏览器.就像是:
(are [scenario expected input]
(testing scenario
(= expected (my-fn input)))
"scenario 1 - replaces -out by -in" "abc-out" "abc-in")
最佳答案 这在clojure.test中是不可能的.
我调整了Stuart Sierra的are
来支持测试场景和失败消息,如下所示:
(defmacro my-are
[scenario fail-msg argv expr & args]
(if (or
(and (empty? argv) (empty? args))
(and (pos? (count argv))
(pos? (count args))
(zero? (mod (count args) (count argv)))))
`(testing ~scenario
(clojure.template/do-template ~argv (is ~expr ~fail-msg) ~@args))
(throw (IllegalArgumentException. "The number of args doesn't match are's argv."))))
现在,测试包含在测试场景中,并添加了失败消息.
这个宏可以像这样使用:
(deftest my-test
(my-are "Scenario 1: testing arithmetic" "Testing my stuff failed"
[x y] (= x y)
2 (- 4 1)
4 (* 2 2)
5 (/ 10 2)))
这导致:
Test Summary
Tested 1 namespaces
Ran 3 assertions, in 1 test functions
1 failuresResults
1 non-passing tests:Fail in my-test
Scenario 1: testing arithmetic
Testing my stuff failed
expected: (= 2 (- 4 1))
actual: (not (= 2 3))
您可以看到执行了三个断言,即失败的测试显示失败消息(“测试我的东西失败”),并且可以看到场景消息(“场景1:测试算术”).