撰写测试时无法删除重复

时间:2017-12-23 18:21:20

标签: clojure clojure.test

我无法删除clojure.test测试中的重复内容。

假设我有相同抽象的多个实现:

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

我还有一个测试,所有实现都应该通过:

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

我现在可以创建一个clojure.test测试,只需一步检查所有实现:

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

一切都很好;我在REPL中运行测试:

(run-tests)

Testing user

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

我现在想修改test-all-impls以删除必须为每个实现显式调用test-impl的重复。我发现修改test-all-impls如下:

(deftest test-all-impls
  (for [foo [foo1 foo2 foo3]] (test-impl foo))
嗯,现在并非一切都很好;在REPL我得到:

(run-tests)

Testing user

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

我错过了什么?

2 个答案:

答案 0 :(得分:3)

要绕过懒惰,请改用doseq:

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

答案 1 :(得分:1)

另一个答案是将结果转换为向量,这将强制for循环运行:

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