首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用repl中的clojure.test方法?

如何使用repl中的clojure.test方法?
EN

Stack Overflow用户
提问于 2016-10-07 21:46:15
回答 2查看 347关注 0票数 0

我尝试了this post中指定的方法,但它表明is不能解析为符号。如何使用clojure.test方法进行单元测试?

代码语言:javascript
复制
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=>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-10-07 22:55:04

你可以这样做:

代码语言:javascript
复制
(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}
  )

另见:https://clojuredocs.org/clojure.test/run-all-tests

票数 1
EN

Stack Overflow用户

发布于 2016-10-07 22:39:57

这是我最喜欢的方式。文件如下所示:

代码语言:javascript
复制
~/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

主要代码:

代码语言:javascript
复制
(ns clj.core)

(defn add [x y] (+ x y))

测试代码:

代码语言:javascript
复制
(ns tst.clj.core
  (:use clj.core
        clojure.test ))

(deftest t-op
  (is (= 3 (add 1 2)))
)

首先,检查实际正在运行的测试。将正确的值3更改为虚拟值,如99

代码语言:javascript
复制
(deftest t-op
  (is (= 99 (add 1 2)))
)

从命令行运行测试,并看到它失败:

代码语言:javascript
复制
 > 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.

然后,返回实际的测试值,重新运行,并看到它通过:

代码语言:javascript
复制
> lein test
lein test tst.clj.core

Ran 1 tests containing 1 assertions.
0 failures, 0 errors.
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39926238

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档