unit-testing - Clojure:如何在测试中使用夹具

标签 unit-testing clojure

我正在编写一些与数据库交互的单元测试。因此,在我的单元测试中使用设置和拆卸方法来创建然后删除表非常有用。然而there are no docs :O 关于使用装置方法。

这是我需要做的:

 (setup-tests)
 (run-tests)
 (teardown-tests)

我目前对每次测试之前和之后运行设置和拆卸不感兴趣,而是在一组测试之前和之后运行一次。你如何做到这一点?

最佳答案

您不能使用 use-fixtures 为自由定义的测试组提供设置和拆卸代码,但可以使用 :once 提供设置和拆卸代码对于每个命名空间:

;; my/test/config.clj
(ns my.test.config)

(defn wrap-setup
  [f]
  (println "wrapping setup")
  ;; note that you generally want to run teardown-tests in a try ...
  ;; finally construct, but this is just an example
  (setup-test)
  (f)
  (teardown-test))    


;; my/package_test.clj
(ns my.package-test
  (:use clojure.test
        my.test.config))

(use-fixtures :once wrap-setup) ; wrap-setup around the whole namespace of tests. 
                                ; use :each to wrap around each individual test 
                                ; in this package.

(testing ... )

这种方法会强制安装和拆卸代码与测试所在的包之间存在一定的耦合,但通常这不是一个大问题。您始终可以在 testing 部分中进行自己的手动包装,例如 the bottom half of this blog post .

关于unit-testing - Clojure:如何在测试中使用夹具,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16350504/

相关文章:

clojure - 与 pred 分离

clojure - Clojure插入符号作为符号?

c# - 无法在单元测试项目中获取类的默认构造函数

javascript - react 测试错误。目标容器不是 DOM 元素

clojure - Clojure中持久数据结构的内存共享

eclipse - 使用 CounterClockwise 和 Eclipse 在 Clojure REPL 中进行评估

tomcat - 在部署到 Tomcat 的 Clojure Ring 应用程序中使用 websockets

java - gradle 测试文件的常规文件系统路径

angularjs - Jest Ionic 测试失败 - 意外的 token 导入

asp.net-mvc - 在 MVC.NET 中测试 User.IsInRole