Reputation: 1248
Which one do you prefer and why? What are the pros and cons of each? In which scenario does each outshine the others?
I'm particularly interested in midje vs. clojure.test, but feel free to bring up other Clojure testing frameworks too.
See also Best unit testing framework for Clojure? (the answers to that question didn't provide much detail on the "why").
Upvotes: 15
Views: 4711
Reputation: 1180
I prefer expectations or clojure.test
with humane-test-output. Both options give the most readable errors and provide fairly minimal syntax.
Given the following test you get the following output below.
(deftest map-comparisons
(is (= {:sheep 1} {:cheese 1 :sheep 1})))
FAIL in (map-comparisons) (map_test.clj:5)
expected: (= {:sheep 1} {:cheese 1, :sheep 1})
actual: (not (= {:sheep 1} {:cheese 1, :sheep 1}))
FAIL in (map-comparisons) (map_test.clj:5)
expected: {:sheep 1}
actual: {:cheese 1, :sheep 1}
diff: + {:cheese 1}
The test looks like:
(expect {:sheep 1} {:sheep 1, :cheese 1})
failure in (map_expectations.clj:6) : example.map-expectations
(expect {:sheep 1} {:sheep 1, :cheese 1})
expected: {:sheep 1}
was: {:cheese 1, :sheep 1}
in expected, not actual: null
in actual, not expected: {:cheese 1}
I did a more detailed comparison of output of the four main Clojure testing libraries and that can be found here.
Upvotes: 5
Reputation: 50064
I prefer Midje. Midje provides a migration path from clojure.test to a more flexible, readable, abstract, and gracious style of testing.
Midje supports top-down as well as bottom-up TDD styles, and has mocking and stubbing facilities baked into it, as well as some powerful features like checkers, metaconstants, tabular facts.
Here's a simple example:
(fact "Midje can do simple stubbing"
(+ (a) 2) => 5
(provided
(a) => 3))
Upvotes: 11
Reputation: 106351
I haven't tried them all, but I like plain old clojure.test for the following reasons:
Sample code:
(testing "Arithmetic"
(testing "with positive integers"
(is (= 4 (+ 2 2)))
(is (= 7 (+ 3 4))))
(testing "with negative integers"
(is (= -4 (+ -2 -2)))
(is (= -1 (+ 3 -4)))))
Upvotes: 11