向Clojure CLI工具和deps.edn添加单元测试



我习惯于将Clojure与Leiningen一起使用。

但我有一个新项目,我启动了LightMod。它使用CLI工具和dep。

这是可行的,但我现在想添加一些单元测试

下面是我的deps.edn文件:

{: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 ...
}}}}

现在,这个文件在:dev和:prod编译中都可以正常工作。

但现在我刚刚添加了:test别名。

并将我的测试放入test/myns/core_tests.clj

然而,当我尝试运行测试时,我会得到这个

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

所以我的解释。而:dev和:prod分支正在成功地引入:app分支中的所有依赖项。:test分支未引入markdown。(可能没有其他在app中声明的依赖项

那为什么要这样呢?我如何明确地告诉我的:test子句,它需要使用与:dev和:prod中使用的所有依赖项相同的依赖项?

您可以使用Kaocha测试运行程序来实现这一点。工作模板项目可以在这里找到。它甚至包括编译Java源文件以及Clojure的能力。

要开始,请像一样设置deps.edn

{: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"}}
}}
}

并添加另一个文件tests.edn来配置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]
}

然后创建一个短shell脚本以在子目录CCD_ 3中启动Koacha。将其命名为bin/kaocha,内容为:

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

现在你已经准备好出发了!测试文件:

(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

我们开始比赛:

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

最新更新