How do I add unit tests to a Leiningen project?
Asked Answered
K

2

11

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.

Keenakeenan answered 14/10, 2011 at 18:16 Comment(0)
I
11

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))))
Insincere answered 14/10, 2011 at 18:26 Comment(2)
when leiningen creates a project it will create the test directory it should be already there with a single failing test.Nasturtium
This code fails "Caused by: java.io.FileNotFoundException: Could not locate mytests__init.class or mytests.clj on classpath: " ... Is this related to a namespace ?Keenakeenan
C
-3

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 ...

Clarance answered 17/4, 2017 at 5:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.