Faery
Faery

Reputation: 4650

How to run test files in Clojure

I'm using jEdit and what I want to do is to write a file and run tests to it. I'm interested in the process of running the tests, not the files themselves.

So I wrote this simple function in File.clj

(defn abs [n]
  (if (neg? n) (- n) n))

And this test in TheTest.clj

(use 'clojure.test)

(load-file "File.clj")

(deftest sample-test
  (testing "abs"
    (is (= 5 (abs (5))))))

(run-tests)

and then I run the file TheTest.clj but it says:

java.io.FileNotFoundException: File.clj (The system cannot find the file specified) (TheTest.clj:49)

Can someone please tell is this is the way to do the things, and if not how to run the tests :( It's not necessary to be in jEdit, it may be in the command prompt if it should. Also I have leiningen installed and File.clj and TheTest.clj are in same directory. I also tried providing the whole path to the file in the load-file function but with no success.

Thank you very much, good people! :)

Upvotes: 3

Views: 1758

Answers (1)

Andrew
Andrew

Reputation: 7516

It's certainly possible to get your example working without Leingingen. It works on my computer with one minor alteration: remove the brackets around 5, so the test line reads

(is (= 5 (abs 5)))

To run the script successfully, find the location of your Clojure JAR, open a terminal and switch to the directory where your source files live, then execute the following:

java -cp /path/to/clojure/clojure-1.4.0.jar clojure.main TheTest.clj

Obviously replace /path/to/clojure/clojure-1.4.0.jar with the actual path to Clojure on your system. If you've used Leiningen at all there'll be a copy in your Maven repository (I've got it at ~/.m2/repository/org/clojure/clojure/1.4.0/clojure-1.4.0.jar)

I've run TheTest.clj and the output is:

Testing user

Ran 1 tests containing 1 assertions.
0 failures, 0 errors.

Upvotes: 6

Related Questions