Reputation:
I tried the methods specified in this post, but it shows that is
cannot be resolved to a symbol. How to use the clojure.test methods for unit testing?
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=>
Upvotes: 0
Views: 477
Reputation: 18005
You can do something like:
(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}
)
Also see: https://clojuredocs.org/clojure.test/run-all-tests
Upvotes: 1
Reputation: 29958
This is my favorite way. Files look like this:
~/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
Main code:
(ns clj.core)
(defn add [x y] (+ x y))
Test code:
(ns tst.clj.core
(:use clj.core
clojure.test ))
(deftest t-op
(is (= 3 (add 1 2)))
)
First, check the test is actually being run. Change the correct value 3
to a dummy value like 99
:
(deftest t-op
(is (= 99 (add 1 2)))
)
Run the tests from the command line and see it fail:
> 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.
Then, put back the actual test value, re-run, and see it pass:
> lein test
lein test tst.clj.core
Ran 1 tests containing 1 assertions.
0 failures, 0 errors.
Upvotes: 2