Re: new in contrib.test-is: fixtures

2009-03-30 Thread Stuart Sierra

On Mar 29, 3:05 pm, John D. Hume duelin.mark...@gmail.com wrote:
 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.)

Hi John,

Using macros and calling fixture functions from tests was how I used
to do this, too.  I think that's still the best way to do per-test
fixtures.  With the per-namespace fixtures, I'm speficically trying to
avoid putting fixture code in every test.  I also wanted to do setup
and cleanup once per namespace without redefining test-ns-hook.  But
this is an experiment; it won't be for everyone.  We'll see how it
works in practice.

-Stuart
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Re: new in contrib.test-is: fixtures

2009-03-29 Thread John D. Hume

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
-~--~~~~--~~--~--~---



Re: new in contrib.test-is: fixtures

2009-03-28 Thread David Nolen

very cool :)

On 3/28/09, Stuart Sierra the.stuart.sie...@gmail.com wrote:

 Hi folks,
 I finally came up with fixtures for clojure.contrib.test-is.  Now you
 can do before/after setup for each test case.  Here's the
 documentation, let me know what you think.
 -Stuart Sierra

   ;; FIXTURES (new)
   ;;
   ;; Fixtures allow you to run code before and after tests, to set up
   ;; the context in which tests should be run.
   ;;
   ;; A fixture is just a function that calls another function passed
 as
   ;; an argument.  It looks like this:
   (defn my-fixture [f]
 ;; Perform setup, establish bindings, whatever.
 (f) ;; Then call the function we were passed.
 ;; Tear-down / clean-up code here.
 )

   ;; Fixtures are attached to namespaces in one of two ways.  each
   ;; fixtures are run repeatedly, once for each test function created
   ;; with deftest or with-test.  each fixtures are useful for
   ;; establishing a consistent before/after state for each test, like
   ;; clearing out database tables.
   ;;
   ;; each fixtures can be attached to the current namespace like
 this:
   (use-fixtures :each fixture1 fixture2 ...)
   ;; The fixture1, fixture2 are just functions like the example above.
   ;; They can also be anonymous functions, like this:
   (use-fixtures :each (fn [f] setup... (f) cleanup...))
   ;;
   ;; The other kind of fixture, a once fixture, is only run once,
   ;; around ALL the tests in the namespace.  once fixtures are
 useful
   ;; for tasks that only need to be performed once, like establishing
   ;; database connections, or for time-consuming tasks.
   ;;
   ;; Attach once fixtures to the current namespace like this:
   (use-fixtures :once fixture1 fixture2 ...)

 


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---