当前命名空间中没有 运行 测试时如何禁用测试装置?
How to disable test fixtures when no tests are running in the current namespace?
我看到很多 clojure 项目通过将此设置添加到 project.clj
:
默认情况下禁用集成测试
:test-selectors {:default (complement :integration)
:integration :integration}
但是,如果命名空间仅包含集成测试,那么当我 运行 lein test
!
时,其中的固定装置仍然 运行
例如,如果我 运行 lein new app test
并将 core_test.clj
的内容设为:
(defn fixture [f]
(println "Expensive setup fixture is running")
(f))
(use-fixtures :once fixture)
(deftest ^:integration a-test
(println "integration test running"))
然后当我 运行 lein test
我看到夹具 运行ning 即使没有测试 运行.
在 Clojure 中处理这个问题的正确方法是什么?
似乎夹具是 运行ning,无论是否测试 运行。然后,您可以将夹具功能放入测试本身以 "manually" 控制 setup/teardown。伪代码:
(defn run-all-tests []
(do-test-1)
...
(do-test-N))
(deftest ^:slow mytest
(do-setup)
(run-all-tests)
(do-teardown))
完成不 运行 昂贵计算的一种方法是利用这样一个事实,即使 :once
固定装置将 运行 无论是否有测试 运行 是否在 ns 中,:each
fixtures 只会 运行 在每个 actually-运行ning 测试中。
而不是在 :once
夹具中进行实际计算(或获取资源,如数据库连接,或做任何副作用),我们只在第一个(我们只想做)一次!):each
fixture,例如做如下:
(def run-fixture? (atom true))
(defn enable-fixture [f]
(println "enabling expensive fixture...")
(try
(f)
(finally (reset! run-fixture? true))))
(defn expensive-fixture [f]
(if @run-fixture?
(do
(println "doing expensive computation and acquiring resources...")
(reset! run-fixture? false))
(println "yay, expensive thing is done!"))
(f))
(use-fixtures :once enable-fixture)
(use-fixtures :each expensive-fixture)
(deftest ^:integration integration-test
(println "first integration test"))
(deftest ^:integration second-integration-test
(println "second integration test"))
lein test
的输出如下(注意 enable-fixture
如何有 运行 而不是昂贵的 expensive-fixture
):
› lein test
lein test fixture.core-test
enabling expensive fixture...
Ran 0 tests containing 0 assertions.
0 failures, 0 errors.
当运行宁lein test :integration
时,expensive-fixture
将运行恰好一次:
› lein test :integration
lein test fixture.core-test
enabling expensive fixture...
doing expensive computation and acquiring resources...
first integration test
yay, expensive thing is done!
second integration test
Ran 2 tests containing 0 assertions.
0 failures, 0 errors.
我看到很多 clojure 项目通过将此设置添加到 project.clj
:
:test-selectors {:default (complement :integration)
:integration :integration}
但是,如果命名空间仅包含集成测试,那么当我 运行 lein test
!
例如,如果我 运行 lein new app test
并将 core_test.clj
的内容设为:
(defn fixture [f]
(println "Expensive setup fixture is running")
(f))
(use-fixtures :once fixture)
(deftest ^:integration a-test
(println "integration test running"))
然后当我 运行 lein test
我看到夹具 运行ning 即使没有测试 运行.
在 Clojure 中处理这个问题的正确方法是什么?
似乎夹具是 运行ning,无论是否测试 运行。然后,您可以将夹具功能放入测试本身以 "manually" 控制 setup/teardown。伪代码:
(defn run-all-tests []
(do-test-1)
...
(do-test-N))
(deftest ^:slow mytest
(do-setup)
(run-all-tests)
(do-teardown))
完成不 运行 昂贵计算的一种方法是利用这样一个事实,即使 :once
固定装置将 运行 无论是否有测试 运行 是否在 ns 中,:each
fixtures 只会 运行 在每个 actually-运行ning 测试中。
而不是在 :once
夹具中进行实际计算(或获取资源,如数据库连接,或做任何副作用),我们只在第一个(我们只想做)一次!):each
fixture,例如做如下:
(def run-fixture? (atom true))
(defn enable-fixture [f]
(println "enabling expensive fixture...")
(try
(f)
(finally (reset! run-fixture? true))))
(defn expensive-fixture [f]
(if @run-fixture?
(do
(println "doing expensive computation and acquiring resources...")
(reset! run-fixture? false))
(println "yay, expensive thing is done!"))
(f))
(use-fixtures :once enable-fixture)
(use-fixtures :each expensive-fixture)
(deftest ^:integration integration-test
(println "first integration test"))
(deftest ^:integration second-integration-test
(println "second integration test"))
lein test
的输出如下(注意 enable-fixture
如何有 运行 而不是昂贵的 expensive-fixture
):
› lein test
lein test fixture.core-test
enabling expensive fixture...
Ran 0 tests containing 0 assertions.
0 failures, 0 errors.
当运行宁lein test :integration
时,expensive-fixture
将运行恰好一次:
› lein test :integration
lein test fixture.core-test
enabling expensive fixture...
doing expensive computation and acquiring resources...
first integration test
yay, expensive thing is done!
second integration test
Ran 2 tests containing 0 assertions.
0 failures, 0 errors.