interstar
interstar

Reputation: 27246

Adding unit-tests to Clojure CLI tools and deps.edn

I'm used to using Clojure with Leiningen.

But I have a new project that I booted into existence with LightMod. And it uses CLI tools and deps.

This works but I now want to add some unit testing to it.

So here's (a slightly simplified version of) my deps.edn file :

{:paths ["src" "resources"],
 :aliases
 {
  :dev
  {:extra-deps
   {orchestra #:mvn{:version "2018.12.06-2"},
    expound #:mvn{:version "0.7.2"},
    nightlight #:mvn{:version "RELEASE"},
    com.bhauman/figwheel-main #:mvn{:version "0.2.0"}},
   :main-opts ["dev.clj"]},

  :test
  {:extra-paths ["test"]
   :extra-deps {com.cognitect/test-runner {:git/url "https://github.com/cognitect-labs/test-runner.git"                                           :sha "209b64504cb3bd3b99ecfec7937b358a879f55c1"}}
   :main-opts ["-m" "cognitect.test-runner"]}

  :prod
  {:extra-deps
   {leiningen #:mvn{:version "2.9.0"},
    org.clojure/clojurescript #:mvn{:version "1.10.439"}},
   :main-opts ["prod.clj"]},

  :app
  {:extra-deps
   {
    markdown-clj {:mvn/version "1.10.1"}
    instaparse {
                :mvn/version"1.4.10"
                }
    ... MORE ...
}}}}

Now this file has worked OK with both :dev and :prod compilation.

But now I've just added the :test alias.

And put my tests into test/myns/core_tests.clj

However, when I try to run tests, I get this

Could not locate markdown/core__init.class, markdown/core.clj or markdown/core.cljc on classpath

So my interpretation. While the :dev and the :prod branches are successfully pulling in all the dependencies in the :app branch. The :test branch is not pulling in markdown. (And probably none of the other dependencies declared in :app

So why should this be? And how can I explicitly tell my :test clause that it needs to use all the same dependencies that are used in :dev and :prod?

Upvotes: 1

Views: 1519

Answers (1)

Alan Thompson
Alan Thompson

Reputation: 29984

You can use the Kaocha test runner for this. A working template project can be found here. It even includes the ability to compile Java source files as well as Clojure.

To get started, set up deps.edn like so

{:paths   ["src/clj"]
 :deps    {
           cambium/cambium.codec-simple {:mvn/version "0.9.3"}
           cambium/cambium.core         {:mvn/version "0.9.3"}
           cambium/cambium.logback.core {:mvn/version "0.4.3"}
           org.clojure/clojure          {:mvn/version "1.10.1"}
           prismatic/schema             {:mvn/version "1.1.12"}
           tupelo                       {:mvn/version "0.9.200"}
           }

 :aliases {:test {
                  :extra-deps  {lambdaisland/kaocha {:mvn/version "1.0-612"}}
                  }}
 }

and add another file tests.edn to configure Kaocha:

; ***** Be sure to use tagged reader literal '#kaocha/v1' *****
#kaocha/v1 {:tests
            [{:id          :unit
              :test-paths  ["test/clj"]
              :ns-patterns ["^tst..*"]
              }]
            ; :reporter kaocha.report.progress/report
            ; :plugins [:kaocha.plugin/profiling :kaocha.plugin/notifier]
            }

then create a short shell script to launch Koacha in the subdir ./bin. Name it bin/kaocha with contents:

#!/bin/bash
clojure -A:test -m kaocha.runner "$@"

Now you are ready to go! A test file:

(ns tst.demo.core
  (:use demo.core tupelo.core tupelo.test) )

(dotest
  (is= 5 (+ 2 3))
  (isnt= 9 (+ 2 3))
  (throws? (/ 1 0)) ; verify that an illegal operation throws an exception

  (is= 3 (add2 1 2))
  (throws? (add2 1 "two"))) ; Prismatic Schema will throw since "two" is not a number

; NOTE:  Clojure Deps/CLI can't handle Java source code at present

and we are off to the races:

~/io.tupelo/clj-template > bin/kaocha 
[(.....)]
1 tests, 5 assertions, 0 failures.

Upvotes: 2

Related Questions