我尝试了this post中指定的方法,但它表明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=>发布于 2016-10-07 22:55:04
你可以这样做:
(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}
)发布于 2016-10-07 22:39:57
这是我最喜欢的方式。文件如下所示:
~/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.https://stackoverflow.com/questions/39926238
复制相似问题