4

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Stumbling on my own answer now, it would be nicer if a number of test cases within the are could be wrapped in multiple testing blocks, to differentiate them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.