Share your thoughts in the 2021 Clojure Community Survey!

Welcome! Please see the About page for a little more info on how this works.

+2 votes
in Test by

How can I use this feature to check that a function is valid given

2 Answers

+6 votes

It helps to define a helper function, something like:

(defn speccheck [fn-to-check options]
  (let [results (t/check [fn-to-check] options)]
    (if (some :failure results)
        (println "\nFailed specs:")
        (doseq [result results
               :when (:failure result)]
          (println (:sym result))
          (pprint (or (ex-data (:failure result))
                      (:failure result)))))

(deftest my-fun-test
  (is (speccheck `this/is-my-fn {}))) ; pass options to t/check as needed

This helps in the failure case because you want the failures printed.

thanks I'm gonna give it a try
0 votes

You can also use expound for reporting:

(defn is*
  (assert (seq results))
   (if (some :failure results)
     (exp/explain-results results)


(is* (stest/check `ns/fn))

Note: assert will fail if there is no spec for that function.