bshanks
bshanks

Reputation: 1248

Strengths of Clojure testing frameworks?

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

Answers (3)

Jake McCrary
Jake McCrary

Reputation: 1180

I prefer expectations or clojure.test with humane-test-output. Both options give the most readable errors and provide fairly minimal syntax.

clojure.test example

Given the following test you get the following output below.

(deftest map-comparisons
  (is (= {:sheep 1} {:cheese 1 :sheep 1})))

Default clojure.test output

FAIL in (map-comparisons) (map_test.clj:5)
expected: (= {:sheep 1} {:cheese 1, :sheep 1})
  actual: (not (= {:sheep 1} {:cheese 1, :sheep 1}))

Example of clojure.test output with humane-test-output

FAIL in (map-comparisons) (map_test.clj:5)
expected: {:sheep 1}
  actual: {:cheese 1, :sheep 1}
    diff: + {:cheese 1}

expectations example

The test looks like:

(expect {:sheep 1} {:sheep 1, :cheese 1})

expectations output

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

Alex Baranosky
Alex Baranosky

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.

Midje wiki

Here's a simple example:

(fact "Midje can do simple stubbing"
  (+ (a) 2) => 5
  (provided 
    (a) => 3))

Upvotes: 11

mikera
mikera

Reputation: 106351

I haven't tried them all, but I like plain old clojure.test for the following reasons:

  • It's already there in the Clojure API: No extra dependencies.
  • Integrates well with Maven: I use Eclipse with the clojure-maven-plugin to ensure that both Clojure and Java tests get run automatically whenever I build.
  • It's simple: 99% of what I need in testing is just to be able to write a well structured set of assertions, clojure.test makes that pretty easy

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

Related Questions