我想修改cake test
,以便它与a different value for *stack-trace-depth*
一起使用。
(deftask test #{compile}
"Run project tests."
"Specify which tests to run as arguments like: namespace, namespace/function, or :tag"
(run-project-tests))
理想情况下,我想使用命令行参数--depth=n
指定值,这是有效的:
(binding [*stack-trace-depth* (if (*opts* :depth)
(read-string (*opts* :depth)))]
(run-project-tests))
我需要使用哪些代码才能完成这项工作?
根据回复:将以下内容放入tasks.clj
(undeftask test)
(deftask test #{compile}
(.bindRoot #'*stack-trace-depth* 5)
(println "Defining task: *stack-trace-depth* is" *stack-trace-depth* "in" (Thread/currentThread))
(run-project-tests))
产生以下输出:
正在加载
test/cake_test/core.clj
:Loading tests: *stack-trace-depth* is nil in #<Thread[thread-13,5,main]>
$ cake test
Defining task: *stack-trace-depth* is 5 in #<Thread[Thread-18,5,main]> In test: *stack-trace-depth* is nil in #<Thread[Thread-16,5,main]> Testing cake-testing.core FAIL in (test-stack-trace-depth) (core.clj:8) expected: (= *stack-trace-depth* 5) actual: (not (= nil 5)) Ran 1 tests containing 1 assertions. 1 failures, 0 errors. ---- Finished in 0.011865 seconds.
(经测试的代码为on Gist。)
答案 0 :(得分:3)
(更新:抛出原来的答案,这里似乎是一个有效的解决方案。)
我从您的Gist中获取了示例项目并进行了以下更改:
rm tasks.clj
在project.clj
表单下方的defproject
添加了以下代码:
(use '[bake.find-namespaces :only [find-namespaces-in-dir]]
'[cake.tasks.test :only [test-opts]])
(undeftask test)
(deftask test #{compile}
(bake (:use bake.test
[bake.core :only [with-context]]
[clojure.test :only [*stack-trace-depth*]])
[namespaces (find-namespaces-in-dir (java.io.File. "test"))
opts (test-opts)]
(with-context :test
(binding [*stack-trace-depth* 5]
(run-project-tests namespaces opts)))))
使用以下内容创建了一个新文件test/cake_testing/core_test.clj
:
(ns cake-testing.core-test
(:use clojure.test))
(deftest correct-stack-trace-depth?
(is (= *stack-trace-depth* 5)))
此时,一切似乎都有效 - cake test
输出
Testing cake-testing.core-test
Ran 1 tests containing 1 assertions.
0 failures, 0 errors.
----
Finished in 0.033200374 seconds.
此外,添加一个故意抛出异常的“测试”会导致打印好的,短的堆栈跟踪。
答案 1 :(得分:2)
能够将它放在与project.clj文件相同级别的task.clj文件中,当我运行cake test --depth=5
时,我可以看到“运行带有 stack-trace-depth <的测试/ em> = 5“正在打印。希望这会有所帮助。
(ns tasks
(:use cake cake.core cake.tasks.test
[clojure.test :only [*stack-trace-depth*]]))
(undeftask test)
(deftask test #{compile}
"Run tests"
(binding [*stack-trace-depth* (if (*opts* :depth)
(read-string (first (*opts* :depth))))]
(println "Running tests with *stack-trace-depth* = " *stack-trace-depth*)
(run-project-tests)))