I would like to build a jar file containing a suite of clojure.test
tests, and then use lein test
to run those tests. As far as I can tell, :test-paths
only supports paths to test source, not test jars.
Is there a way to accomplish this?
I would like to build a jar file containing a suite of clojure.test
tests, and then use lein test
to run those tests. As far as I can tell, :test-paths
only supports paths to test source, not test jars.
Is there a way to accomplish this?
Copyright © 2021 Jogjafile Inc.
You can do this by manually creating a test runner. Suppose you have a project
demo
and a testing nstst.demo.core
like so:Under the
src/demo
directory, create a test driverdemo.tst
like so:We can run our driver which will call all of our tests in
tst.demo.core
:How It Works:
We digress a bit to discuss the Clojure
var
. Thevar
is like a pointer from a symbol likepi
to the function that prints3.14
. Normally we don't even realize thevar
is there, as with the first printout:We invoked the
pi
function, which returned the value 3.14, as usual.However, we can get a reference to the
var
for thepi
function using the wither#'pi
or(var pi)
. We then invoke thepi
function using the var, not the symbolpi
. We see:We use the
ns-interns
to get a map from symbols to vars in thetst.demo.core
namespace (note: it is critical that we have:require [tst.demo.core ...]
at the top of the file in thens
form). We printout this map:We only need the vars themselves, so we grab them and save into
tst-vars
:Note that each
(dotest ...)
form in the testing nstst.demo.core
creates a function that is invoked duringlein test
. We can invoke these functions ourselves, using the vars we just retrieved. This is what thedoseq
loop does, and we see the output of our mock test functionst1
andt2
:The final step is to designate
demo.tst
as the main ns inproject.clj
viawhich should allow you to create a uberjar and run it: