简体   繁体   English

如何在Clojure中运行测试文件

[英]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. 我正在使用jEdit,我想要做的是写一个文件并运行测试。 I'm interested in the process of running the tests, not the files themselves. 我对运行测试的过程感兴趣,而不是文件本身。

So I wrote this simple function in File.clj 所以我在File.clj编写了这个简单的函数

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

And this test in TheTest.clj 而这个测试在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: 然后我运行文件TheTest.clj但它说:

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. 有人可以告诉这是做这些事情的方法,如果不是如何运行测试:(没有必要在jEdit,它可能在命令提示符,如果它应该。我也有leiningen安装和File.cljTheTest.clj在同一个目录中。我也尝试在加载文件函数中提供文件的完整路径但没有成功。

Thank you very much, good people! 非常感谢,好人! :) :)

It's certainly possible to get your example working without Leingingen. 如果没有Leingingen,你的榜样肯定是可行的。 It works on my computer with one minor alteration: remove the brackets around 5 , so the test line reads 它可以在我的计算机上进行一次小改动:删除5左右的括号,以便测试线读取

(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: 要成功运行脚本,请找到Clojure JAR的位置,打开终端并切换到源文件所在的目录,然后执行以下命令:

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. 显然将/path/to/clojure/clojure-1.4.0.jar替换为系统上Clojure的实际路径。 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 ) 如果您已经使用了Leiningen,那么Maven存储库中将有一个副本(我已经在~/.m2/repository/org/clojure/clojure/1.4.0/clojure-1.4.0.jar

I've run TheTest.clj and the output is: 我运行了TheTest.clj ,输出是:

Testing user

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM