On Sat, Mar 28, 2009 at 10:24 PM, Stuart Sierra <the.stuart.sie...@gmail.com> wrote: > (defn my-fixture [f] > ;; Perform setup, establish bindings, whatever. > (f) ;; Then call the function we were passed. > ;; Tear-down / clean-up code here. > )
This is a cool functional way of defining these, but I think I'd prefer to just call the fixture function from the tests needing common setup because of the standard problems with shared setup in unit tests. (In brief, the test doesn't stand on its own to express what it's about, so you have to scroll around to figure out what context the test runs in.) For clj-record's db-connected tests I defined a macro called "defdbtest" [1] that created and rolled back a transaction around each test. For something less common but still shared among multiple tests I might prefer something like the following (using a fixture function called "with-valid-foo") over a fixture declaration that worked on the whole namespace: (deftest foo-bars-like-its-supposed-to (with-valid-foo (fn [foo] (is ( ... something about foo barring))))) with-valid-foo is similar to one of your fixtures, but it passes any needed stuff to the given function as args rather than using thread-local bindings. On the other hand, I might just prefer this: (deftest foo-bars-like-its-supposed-to (let [foo (valid-foo)] (is ( ... something about foo barring))))) Either of these approaches make each test a little more wordy, but the two big advantages are * each test lets you know what context it's interested in and how it got it * not all tests in the namespace have to use the same fixtures. If you agree it would be nice for individual tests to pick and choose fixtures but you're a fan of using thread-local bindings, maybe you could enhance deftest to let it say what fixtures it wants instead of a namespace-wide declaration. -hume. [1] http://github.com/duelinmarkers/clj-record/blob/master/src/clj_record/test/test_helper.clj -- http://elhumidor.blogspot.com/ --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---