gpt4 book ai didi

testing - Clojure 测试 : global fixtures

转载 作者:行者123 更新时间:2023-11-28 19:53:09 25 4
gpt4 key购买 nike

我有一些固定装置可以在我的项目中启动和关闭数据库。

现在看起来像这样:

(use-fixtures :once with-embedded-db)

在灯具本身中,我有一个在不同地方使用的动态变量:

(def ^:dynamic *db*)

(defn with-embedded-db [f]
(binding [*db* (db/connect args)]
(f)
(finally
(db/clean-up *db)))

现在,假设 db/connectdb/clean-up 需要一些时间。

问题:

当我使用 lein test 运行测试时,它需要很长时间,不必要地花费时间连接和断开连接到每个命名空间的数据库。

问题:

有没有办法设置global fixtures,这样当我运行lein test时,它只对所有测试命名空间调用一次?

谢谢!

最佳答案

如果将该功能添加到 leiningen 本身会更好。如果不是 PR,至少应该开一张票。

下面的解决方案是肮脏的,但你可以得到这个想法并将其转化为更智能的东西。

;; profect.clj
:profiles
{:dev {:dependencies [[robert/hooke "1.1.2"]]

:injections [(require '[robert.hooke :as hooke])
(defn run-all-test-hook [f & nss]
(doall (map (fn [a]
(when (intern a '*db*)
(intern a '*db* "1234"))) nss))
(apply f nss))
(hooke/add-hook #'clojure.test/run-tests #'run-all-test-hook)
]}}

注意:leiningen 本身在其核心中使用 robert/hooke。然后在测试的某个地方:

(ns reagenttest.cli
(:require [clojure.test :refer :all]))

(def ^:dynamic *db*) ;; should be defined in every NS where it is needed

(deftest Again
(testing "new"
(prn *db*)))

关于testing - Clojure 测试 : global fixtures,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43585325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com