簡體   English   中英

如何測量每個名稱空間運行clojure測試所花費的時間?

[英]How do I measure time taken per namepace to run clojure tests?

有沒有辦法檢測代碼並找出每個命名空間需要多少時間,或者唯一的方法是使用fixture?

解決這個問題的好方法是什么?

您在命名空間列表上使用run-tests建議將起作用。

這是我寫的一個函數,用於在repl中循環命名空間,重新加載它們,然后為每個ns運行測試。 您可以為您的目的修改代碼。 您必須添加代碼以捕獲開始/結束時間並打印每個ns的已用時間。

正如docstring所說,這段代碼假定命名空間的設置如下:

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

所以只需根據設置要求(symbol (str "tst." curr-ns))或修改部件。 您可能希望使用all-ns來獲取所有名稱空間的列表,然后刪除或忽略非測試名稱空間。 祝好運!

(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)
    ))

您可以使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM