Migrating from clojure.test

marick edited this page Apr 25, 2012 · 8 revisions
Clone this wiki locally

A short video

I'm going to assume your test file looks like this:

     (ns my.test.ns
       (:use clojure.test))

     (deftest some-test ...)
     (deftest some-test ...)

Leave those old tests alone and just add facts to your test files:

     (ns my.test.ns
       (:use clojure.test)
       (:use midje.sweet))    ;; adding on...

     (deftest some-test ...)
     (deftest some-test ...)

     (fact (+ 1 1) => even?)  ;; adding on...

If you use Leiningen, install the leiningen plugin. You can then run lein midje to see output like this:

+++ The following shows how 'cake midje' checks facts in test files.
+++ The failure is intentional.

FAIL at (t_core.clj:13)
    Expected: "I am a test file fact"
      Actual: 3

+++ The following shows how 'cake midje' checks facts in source files.
+++ The failure is intentional.

FAIL at (core.clj:7)
    Expected: "I am a source-file fact."
      Actual: 3

+++ The following shows how 'cake midje' runs clojure.test deftests.
+++ The failure is intentional.
>>> Output from clojure.test tests:

FAIL in (a-clojure-test-test) (t_core.clj:8)
expected: (= 1 "I am a deftest expected result.")
  actual: (not (= 1 "I am a deftest expected result."))

>>> clojure.test summary:
Ran 1 tests containing 1 assertions.
1 failures, 0 errors.
>>> Midje summary:
FAILURE: 2 facts were not confirmed. 

Notice that both kinds of tests are run, and that summary results are reported correctly for both.

I don't recommend rewriting existing tests unless you are already changing them for some other reason.