Clojure:如何在测试中使用夹具 [英] Clojure: How To use-fixtures in Testing

查看:119
本文介绍了Clojure:如何在测试中使用夹具的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一些与数据库交互的单元测试。因此,在我的单元测试中使用一个设置和拆卸方法来创建并删除表是非常有用的。但是,没有文档:使用use-fixtures方法。

I am writing some unit tests that interact with a database. For this reason it is useful to have a setup and a teardown method in my unit test to create and then drop the table. However there are no docs :O on the use-fixtures method.

这里是我需要做的:

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



我不感兴趣目前在运行一个设置和拆卸前后每个测试,但一次之前一组测试和一次后。

I am not interested currently in running a setup and teardown before and after each test, but once before a group of tests and once after. How do you do this?

推荐答案

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

You can't use use-fixtures to provide setup and teardown code for freely defined groups of tests, but you can use :once to provide setup and teardown code for each namespace:

;; 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 ... )

和teardown代码和测试所在的包,但通常这不是一个巨大的问题。您可以随时在测试部分中执行自己的手动换行,参见例如本博文的下半部分

This approach forces some coupling between setup and teardown code and the packages the tests are in, but generally that's not a huge problem. You can always do your own manual wrapping in testing sections, see for example the bottom half of this blog post.

这篇关于Clojure:如何在测试中使用夹具的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆