Migrating from clojure.test

Phillip Mates edited this page Jun 8, 2017 · 9 revisions

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.

Adapting setup and teardown logic

If existing clojure.test tests make use of setup/teardown logic (for instance, via use-fixtures), then this logic can also be migrated to midje using with-state-changes as described in Setup and teardown.

For example:

(def person (atom nil))
(defn create-state [] (reset! person "bill"))
(defn destroy-state [] (reset! person nil))

(defn my-test-fixture [f]
  (create-state)
  (f)
  (destroy-state))

(use-fixtures :each my-test-fixture)

(deftest person-is-named-bill
  (is (= "bill" @person)))

;; can be adapted to Midje using `with-state-changes`:
(with-state-changes [(before :facts (create-state))
                     (after :facts (destroy-state))]
  (fact @person => "bill"))
Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.