Search code examples
clojureclojure.test

unable to remove duplication when composing tests


I am unable to remove duplication inside a clojure.test test.

Suppose I have multiple implementations for the same abstraction:

(defn foo1 [] ,,,)
(defn foo2 [] ,,,)
(defn foo3 [] ,,,)

and I also have a test that all implementations should pass:

(defn test-impl [foo]
  (is (= ,,, (foo))))

I can now create a clojure.test test that checks all the implementations in a single step:

(deftest test-all-impls
  (test-impl foo1)
  (test-impl foo2)
  (test-impl foo3))

All is good; running the tests in the REPL I get:

(run-tests)

Testing user

Ran 1 tests containing 3 assertions.
0 failures, 0 errors.
=> {:test 1, :pass 3, :fail 0, :error 0, :type :summary}

I now want to modify test-all-impls to remove the duplication of having to invoke test-impl explicitly for each of the implementations. It occurs to me to modify test-all-impls as follows:

(deftest test-all-impls
  (for [foo [foo1 foo2 foo3]] (test-impl foo))

Hmm, now not all is good; in the REPL I get:

(run-tests)

Testing user

Ran 1 tests containing 0 assertions.
0 failures, 0 errors.
=> {:test 1, :pass 0, :fail 0, :error 0, :type :summary}

What am I missing?


Solution

  • To get around the lazyness of for, use doseq instead:

    (deftest test-all-impls
      (doseq [foo [foo1 foo2 foo3]] (test-impl foo))