Clojure test with multiple assertions

孤街浪徒 提交于 2019-12-11 06:59:46

问题


I am starting to learn how to write tests for my Clojure program. I have a test with multiple assertions (calls to is). I want the test to stop running as soon as the first assertion fails. Here is my test:

(deftest register-one-command
  (testing "Register-one-command"
    (do
      (is (= 0 (count @test/list-of-commands)))
      (test/add-to-list-of-commands ["A"])
      (is (= 1 (count @test/list-of-commands))))))

If the first is fails, I want the whole thing to fail.


回答1:


The simplest way is to just wrap the different tests in an and

(deftest register-one-command
  (testing "Register-one-command"
    (is (and
          (= 0 (count @test/list-of-commands))
          (test/add-to-list-of-commands ["A"])
          (= 1 (count @test/list-of-commands))))))

If possible, however, I would try to restructure the tests so that this extra bit of complexity is unnecessary.



来源:https://stackoverflow.com/questions/47675996/clojure-test-with-multiple-assertions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!