简体   繁体   English

clojure.test /正在使用clojure.test / testing

[英]clojure.test/are with clojure.test/testing

In clojure.test there is a macro that allows to test several fixtures at the same time: are . 在clojure.test中有一个宏允许同时测试几个灯具: are

In clojure.test, is is possible to combine the are macro with testing ? 在clojure.test,是可以结合are宏观与testing

Ie. IE浏览器。 something like: 就像是:

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

This is not possible with are in clojure.test.. 这是不可能的, are在clojure.test ..

I adjusted Stuart Sierra's are to support testing 's scenario and failing message as follows: 我调整司徒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."))))

Now the tests are wrapped in a testing scenario and fail-messaged are added. 现在,测试包含在testing场景中,并添加了失败消息。

This macro can be used like this: 这个宏可以像这样使用:

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

This leads to: 这导致:

Test Summary 测试摘要

Tested 1 namespaces 测试了1个命名空间
Ran 3 assertions, in 1 test functions 在3个测试函数中执行3个断言
1 failures 1次失败


Results 结果
1 non-passing tests: 1次非通过测试:

Fail in my-test 我的测试失败了
Scenario 1: testing arithmetic 场景1:测试算术
Testing my stuff failed 测试我的东西失败了
expected: (= 2 (- 4 1)) 预期:(= 2( - 4 1))
actual: (not (= 2 3)) 实际:(不是(= 2 3))

You can see that the three assertions are executed, that the fail message ("Testing my stuff failed) is shown for the test that fails, and that the scenario message ("Scenario 1: testing arithmetic") is visible. 您可以看到执行了三个断言,即失败的测试显示失败消息(“测试我的东西失败”),并且可以看到场景消息(“场景1:测试算术”)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM