简体   繁体   中英

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 .

In clojure.test, is is possible to combine the are macro with testing ?

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..

I adjusted Stuart Sierra's are to support testing 's scenario and failing message as follows:

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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