gpt4 book ai didi

unit-testing - clojure.test/are 与 clojure.test/testing

转载 作者:行者123 更新时间:2023-12-01 04:52:44 25 4
gpt4 key购买 nike

在 clojure.test 中有一个允许同时测试多个设备的宏: are .

在 clojure.test 中,可以结合 are宏与 testing ?

IE。就像是:

(are [scenario expected input]
(testing scenario
(= expected (my-fn input)))
"scenario 1 - replaces -out by -in" "abc-out" "abc-in")

最佳答案

这是不可能的 are在 clojure.test..

我调整了 Stuart Sierra 的 are 支持testing的场景和失败消息如下:

(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."))))

现在测试被包裹在 testing 中添加了场景和失败消息。

这个宏可以这样使用:
(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 failures


Results
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:测试算法”)是可见的。

关于unit-testing - clojure.test/are 与 clojure.test/testing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39943085/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com