3

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Robert3452
  • 1,354
  • 2
  • 17
  • 39
  • Thanks for the edits. I notice the reformatting of the code so the close brackets are on the same line rather than below the matching opening bracket. I am new to Clojure so am probably doing it wrong but I like the close brackets below the opening ones because it makes it a lot easier for me to copy and paste individual lines. Also when I edit the code it is important to match opening and closing brackets and not every editor is going to highlight for me. – Robert3452 Dec 07 '17 at 14:46
  • Check out this post https://adambard.com/blog/acceptable-error-handling-in-clojure/ While it's not about testing it talks about how to do a general if success then ... if success ... -> which is basically an error monad. – Harindu Dilshan Dec 08 '17 at 23:36

1 Answers1

5

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.

Alan Thompson
  • 29,276
  • 6
  • 41
  • 48