[英]How to use clojure.test methods from repl?
我试着在规定的方法这个职位,但它表明, 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=>
这是我最喜欢的方式。 文件如下所示:
~/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.
您可以执行以下操作:
(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}
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.