我尝试了this帖子中指定的方法,但它显示is
无法解析为符号。如何使用clojure.test方法进行单元测试?
user=> (ns clojure.test)
nil
clojure.test=> (is (= 4 4))
CompilerException java.lang.RuntimeException: Unable to resolve symbol: is in this context, compiling:(NO_SOURCE_PATH:55:1)
clojure.test=> (is (= 4 4))
CompilerException java.lang.RuntimeException: Unable to resolve symbol: is in this context, compiling:(NO_SOURCE_PATH:56:1)
clojure.test=> (:require [clojure.test :as test])
CompilerException java.lang.ClassNotFoundException: clojure.test, compiling:(NO_SOURCE_PATH:57:1)
clojure.test=> (ns user)
nil
user=> (:require [clojure.test :as test])
CompilerException java.lang.ClassNotFoundException: clojure.test, compiling:(NO_SOURCE_PATH:59:1)
user=> (ns a (:require [clojure.test :as test]))
nil
a=> (test/is (= 1 1))
CompilerException java.lang.RuntimeException: No such var: test/is, compiling:(NO_SOURCE_PATH:61:1)
a=> (ns a (:require [clojure.test :refer :all]))
nil
a=> (is (= 2 2))
CompilerException java.lang.RuntimeException: Unable to resolve symbol: is in this context, compiling:(NO_SOURCE_PATH:63:1)
a=>
答案 0 :(得分:1)
这是我最喜欢的方式。文件看起来像这样:
~/clj > ls -ldF **/core.*
-rw-rw-r-- 1 alan alan 40 Oct 7 15:32 src/clj/core.clj
-rw-rw-r-- 1 alan alan 618 Oct 7 15:34 test/tst/clj/core.clj
主要代码:
(ns clj.core)
(defn add [x y] (+ x y))
测试代码:
(ns tst.clj.core
(:use clj.core
clojure.test ))
(deftest t-op
(is (= 3 (add 1 2)))
)
首先,检查测试实际上是否正在运行。将正确的值3
更改为虚拟值,例如99
:
(deftest t-op
(is (= 99 (add 1 2)))
)
从命令行运行测试并看到它失败:
> lein test
lein test tst.clj.core
lein test :only tst.clj.core/t-op
FAIL in (t-op) (core.clj:25)
expected: (= 99 (add 1 2))
actual: (not (= 99 3))
Ran 1 tests containing 1 assertions.
1 failures, 0 errors.
Tests failed.
然后,放回实际测试值,重新运行,并看到它通过:
> lein test
lein test tst.clj.core
Ran 1 tests containing 1 assertions.
0 failures, 0 errors.
答案 1 :(得分:1)
您可以执行以下操作:
(ns my-project.my-ns-test
(:require [my-ns :as sut]
[clojure.test :refer :all]))
(deftest my-ns-fn-test (testing "can add"
(is (= 3 (sut/add 1 2)))))
(comment ;; ie. in your REPL
;;https://clojuredocs.org/clojure.test/run-tests
(run-tests 'my-project.my-ns) ;; {:test 1, :pass 1, :fail 0, :error 0, :type :summary}
)