jayunit100
jayunit100

Reputation: 17650

How do I add unit tests to a Leiningen project?

I noticed that leiningen has a great unit test plugin - you just enter "lein test" :) . However, it's not clear how it "finds" the test files. Is there a specific folder I need to put them in? Or, if its just scanning namespaces (which is what it says in the lein docs), how do I know what namespace I need to use for leiningen to see my tests?

I was thinking about simply making one test file, called tests.clj. A sample template would really be nice.

Upvotes: 11

Views: 3372

Answers (2)

Kevin
Kevin

Reputation: 25269

At the top level make a test/ directory, and then create some file, say mytests.clj. Here's a sample (caveat: I didn't actually compile this, but simplified an existing test):

(ns mytests
    (:use clojure.test))

(defn myfixture [block] 
    (do 
        (println "before test")
        (block)
        (println "after test")))

(use-fixtures :each myfixture)

(deftest mytest
    (is (= 2 (+ 1 1))))

Upvotes: 11

matrixmike
matrixmike

Reputation: 31

for lein version 2.7.1 : {this is from Linux Mint with Leiningen 2.7.1 on Java 1.8.0_121 Java HotSpot(TM) 64-Bit Server VM } I wanted to do more with testing and now have this new knowledge...

lein new <projname>
cd <projname>
tree -d
cat test/<projname>/core_test.clj 
lein test
 -> observe failing test template even on blank new project

then you could add: (print "start adding test") and see your efforts ...

Upvotes: -3

Related Questions