[英]Run tests from Clojure Repl and Leiningen
As a newbie to clojure, I have used leiningen to create a sample project with 作为clojure的新手,我使用leiningen创建了一个示例项目
lein new app first-project
which gave me this directory 这给了我这个目录
.
├── doc
│ └── intro.md
├── LICENSE
├── project.clj
├── README.md
├── resources
├── src
│ └── first_project
│ └── core.clj
├── target
│ └── repl
│ ├── classes
│ └── stale
│ └── extract-native.dependencies
└── test
└── first_project
└── core_test.clj
Without modifying any files, I can lauch successfully the only failing test with 无需修改任何文件,我就可以成功完成唯一失败的测试
lein test
...
Ran 1 tests containing 1 assertions.
1 failures, 0 errors.
Tests failed.
But I am unable to do the same from the REPL using run-tests 但是我无法使用运行测试在REPL中执行相同操作
lein repl
first-project.core=> (use 'clojure.test)
nil
first-project.core=> (run-tests)
Testing first-project.core
Ran 0 tests containing 0 assertions.
0 failures, 0 errors.
{:type :summary, :pass 0, :test 0, :error 0, :fail 0}
I tried (but does not work) 我尝试过(但不起作用)
(require 'first-project.core-test)
Start a REPL with lein repl
, then use require 用
lein repl
启动一个REPL,然后使用require
(require '[clojure.test :refer [run-tests]])
(require 'your-ns.example-test :reload-all)
(run-tests 'your-ns.example-test)
I prefer to stay in the user
namespace, as opposed to changing it with in-ns
as mentioned by another answer . 我更喜欢保留在
user
名称空间中,而不是像另一个答案提到的那样使用in-ns
进行更改。 Instead, pass the namespace as an argument to run-tests
(as shown above). 而是将名称空间作为参数传递给
run-tests
(如上所示)。
I'd also recommend staying away from (use 'clojure.test)
; 我也建议远离
(use 'clojure.test)
; that is why I suggested (require '[clojure.test :refer [run-tests]])
above. 这就是为什么我在上面建议
(require '[clojure.test :refer [run-tests]])
。 For more background, read http://dev.clojure.org/jira/browse/CLJ-879 有关更多背景信息,请阅读http://dev.clojure.org/jira/browse/CLJ-879
In your example above the repl is in the wrong namespace. 在上面的示例中,REPL位于错误的名称空间中。 It may work better if you switch the repl to the
core_test
namespace. 如果将repl切换到
core_test
命名空间,则可能会更好。 and then run (run-tests)
. 然后运行
(run-tests)
。
(in-ns 'first-project.core-test)
(run-tests)
Another fun way of developing tests is to just run them from the REPL until they work, because tests are normal functions with some extra metadata. 开发测试的另一种有趣方式是仅在REPL上运行它们,直到它们起作用为止,因为测试是具有一些额外元数据的正常功能。
(in-ns 'first-project.core-test)
(my-test)
Remember you have to load the file in addition to calling in-ns
Let's say your test file is tests/first_project/core_test.clj
, then you will need to call 记住,除了调用
in-ns
外,您还必须加载文件。假设您的测试文件是tests/first_project/core_test.clj
,那么您将需要调用
(load "tests/first_project/core_test")
(in-ns 'first-project.core-test)
(my-test)
Keep in mind that _
in the file system becomes -
in the namespace and /
becomes .
请记住,文件系统中的
_
在命名空间中变为-
,并且/
变为.
. 。
To recap: 回顾一下:
Full qualification of functions is only needed if you issued an in-ns
earlier. 仅当您之前发出
in-ns
才需要完全限定功能。 Then do: 然后做:
(clojure.core/require '[clojure.core :refer [require]]
'[clojure.test :refer [run-tests]]
'[clojure.repl :refer [dir]])
; Load whatever is in namespace "foo.bar-test" and reload everything
; if `:reload-all` has been additionally given
(require 'foo.bar-test :reload-all)
;=> nil
; List your tests for good measure (Note: don't quote the namespace symbol!)
(dir foo.bar-test)
;=> t-math
;=> t-arith
;=> t-exponential
;=> nil
; Check meta-information on a test to verify selector for example
(meta #'foo.bar-test/t-math)
;=> {:basic-math true, :test #object[foo.bar_tes...
; `run-tests` will probably run nothing because the current namespace
; doesn't contain any tests, unless you have changed it with "in-ns"
(run-tests)
;=> Ran 0 tests containing 0 assertions.
; run tests by giving namespace explicitly instead
(run-tests 'foo.bar-test)
;=> Ran 3 tests containing 29 assertions.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.