Skip to content

Quick Start for Existing Project

alvarogarcia7 edited this page Jan 10, 2015 · 8 revisions

Quick Start Guide for an Existing Clojure Project

(especially for those who think they know what they are doing already)

So, you have Leiningen installed and already have a project started, and now you want to start using Midje. At this point you may or may not have any tests.

This tutorial will get you set up to run Midje with its --lazytest option which is analogous to tools like autotest or guard.

For the sake of this tutorial we'll use coolapp as the name of the project you already have.

Adding Midje to your project

Install the Leiningen Midje plugin. The Readme has simple instructions for this.

Edit your project.clj file to include Midje.

For Leiningen 1:

:dev-dependencies [[midje "1.4.0"]]

For Leiningen 2:

:profiles {:dev {:dependencies [[midje "1.6.3"]]
                 :plugins [[lein-midje "3.1.3"]]}}

Run lein midje now and you should see it running your clojure.test tests.

If you didn't have any tests you should at least see: All claimed facts (0) have been confirmed.

Writing your first Midje test

If you have existing tests then you can switch to the Getting Started guide which will walk you through transforming clojure.test tests into Midje tests.

If you don't have any tests at all the next step will be to get a first failing test to ensure everything is connected correctly.

coolapp has some code in the src/coolapp/models/coolness.clj file which seems like a good place to start testing. Specifically it has a function to compute the square of a number (skware).

Create the file test/coolapp/test/models/coolness.clj and put the following content into it:

(ns coolapp.test.models.coolness
  (:use midje.sweet))

(fact (+ 2 2) => 5)

Now run lein midje and you should see the following output:

FAIL at (coolness.clj:4)
    Expected: 5
      Actual: 4
FAILURE: 1 fact was not confirmed. 

Congratulations! We have our first failing test. We now know that all the moving parts are in place. Let's switch the test to test our square function.

Change the contents of the file to be:

(ns coolapp.test.models.coolness
  (:use coolapp.models.coolness)
  (:use midje.sweet))

(fact (skware 2) => 5)

(5 is definitely the wrong answer - so why have it? To once again double check that the test is doing the right thing. If you have never seen a test fail - how do you know it will ever fail?)

Running lein midje again we see the same error. Fix the 5 to be a 4 and when we run lein midje one last time we see:

All claimed facts (1) have been confirmed.

Now the system is all ready to go. Commit the current code and start thinking of what your next test will be.

Setting up the --lazytest feature

Running the lein midje command over and over can be time consuming - one way to cut down the time to test feedback is to use the --lazytest feature. It takes only a small change to the project.clj file to set it up.

You will need to add a dev dependency on lazytest and add the maven repository where it can be found. Now your project.clj file will have the following:

:dev-dependencies [[midje "1.3.2-SNAPSHOT"]
                   [com.stuartsierra/lazytest "1.2.3"]]
:repositories {"stuart" "http://stuartsierra.com/maven2"}

Now this lets you run lein midje --lazytest which will run the tests over and over as it sees changes to the source or test code.