如何测量每个名称空间运行clojure测试所花费的时间?

时间:2016-10-19 07:17:58

标签: unit-testing clojure tdd clojure.test

有没有办法检测代码并找出每个命名空间需要多长时间,或者唯一的方法是使用灯具?

解决此类问题的好方法是什么?

2 个答案:

答案 0 :(得分:1)

Your suggestion of using run-tests over a list of namespaces will work.

Here is a function I wrote to loop ever namespaces in the repl, reload them all, then run tests for each ns. You could modify the code for your purpose. You would have to add code to capture the starting/ending time and print the elapsed time for each ns.

As the docstring says, this code assumes the namespaces are set up to look like:

    myproj.myfeat      ; main namespace
tst.myproj.myfeat      ; testing namespace

so just strip out or modify the part with (symbol (str "tst." curr-ns)) as required for your setup. You'll probably want to use all-ns to get a listing of all namespaces, then either remove or ignore the non-testing namespaces. Good luck!

(defn ^:deprecated ^:no-doc test-all
  "Convenience fn to reload a namespace & the corresponding test namespace from disk and
  execute tests in the REPL.  Assumes canonical project test file organization with
  parallel src/... & test/tst/... directories, where a 'tst.' prefix is added to all src
  namespaces to generate the cooresponding test namespace.  Example:

    (test-all 'tupelo.core 'tupelo.csv)

  This will reload tupelo.core, tst.tupelo.core, tupelo.csv, tst.tupelo.csv and
  then execute clojure.test/run-tests on both of the test namespaces."
  [& ns-list]
  (let [test-ns-list (for [curr-ns ns-list]
                       (let [curr-ns-test (symbol (str "tst." curr-ns))]
                         (println (str "testing " curr-ns " & " curr-ns-test))
                         (require curr-ns curr-ns-test :reload)
                         curr-ns-test))
        ]
    (println "-----------------------------------------------------------------------------")
    (apply clojure.test/run-tests test-ns-list)
    (println "-----------------------------------------------------------------------------")
    (newline)
    ))

答案 1 :(得分:0)

您可以使用eftest:test-warn-time来衡量每个NS的测试时间。

添加后,您将看到彩色(未显示)输出,表示测试缓慢:

LONG TEST in foo-api.handlers.foo-test during :clojure.test/once-fixtures
Test took 12.300 seconds seconds to run