Structing Clojure tests/setup and tear down

314 views
Skip to first unread message

Colin Yates

unread,
May 21, 2013, 10:17:39 AM5/21/13
to clo...@googlegroups.com
Howdy,

I am using clojure.test and have some questions of how to write idiomatic Clojure.  This really isn't about testing at all per-se.

First - I know about fixtures to get (at least) the same as JUnit's before/after behaviour.

My code is a bloomy.  You can configure the bloomy and it does different things based on that behaviour.  Pretty much every test has a different bloomy, *and* that bloomy must be elegantly shut down.

How should I handle this?

At the moment I have the most un-idiomatic way and blunt way of :

[code]
(deftest my-test
  (let [bloomy (create-a-bloomy]
  (try
    (do-something-with-my-bloomy)
    (is (=....))
  (finally (shut-down bloomy))))
[/code]

Yep, try/finally in every test - reminds me of early JDBC libraries before Spring :).  If I understand it correctly, I would end up writing a separate fixture for each and every test, or at least each any every unique set of test context.

I did consider writing a "(defn with-bloomy [bloomy test] (try (test) (finally (shut-down bloomy))))" but I couldn't figure out how to pass my bloomy into the test itself.  I also received lots of "assertion not in expectation" type errors.  To be explicit I would use this as "(with-bloomy (create-a-bloomy) (deftest...)))".

I did consider a variation on the above of passing in a function which only contained the assertions, so "(deftest my-test (let [bloomy...] (with-bloomy bloomy #(is (= 1 (get-something bloomy)))))" but I also ran into similar "assertion not in expectation" type errors, and the indentation in emacs was insane.

I expect a macro might be the answer?

So, how would you solve this?

Ulises

unread,
May 21, 2013, 10:51:28 AM5/21/13
to clo...@googlegroups.com


--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Colin Yates

unread,
May 21, 2013, 11:05:35 AM5/21/13
to clo...@googlegroups.com
Hi Ulises,

I don't think I am as that would require essentially a fixture per distinct combinations of test state, which is almost the same number of tests.

Have I missed something?


You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/Pm-DUDxWeM4/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.

gaz jones

unread,
May 21, 2013, 11:21:48 AM5/21/13
to clo...@googlegroups.com
I think I would use a macro:

(defn with-bloomy-fn [bloomy body]
  (try
   (body)
   (finally
    (shut-down bloomy))))

(defmacro with-bloomy [bloomy & body]
  `(with-bloomy-fn ~bloomy (fn [] ~@body)))

(deftest my-test
  (with-bloomy (create-a-bloomy)
    (...))


FYI code is untested, typing straight in so there may be typos etc.

Colin Yates

unread,
May 21, 2013, 11:38:47 AM5/21/13
to clo...@googlegroups.com
Thanks Gaz,

I was expecting a macro to be the answer, and seeing how you have used a macro to glue together two functions is really helpful, so thanks a bunch!

Ulises

unread,
May 21, 2013, 12:18:30 PM5/21/13
to clo...@googlegroups.com
Hey Colin,

Apologies, I missed your "First - I know about fixtures..." line :)

I'd probably +1 Gaz's macro (I've not tested it either but it looks reasonable.)

Colin Yates

unread,
May 21, 2013, 12:41:51 PM5/21/13
to clo...@googlegroups.com
No worries ;)

Alex Baranosky

unread,
May 21, 2013, 1:14:57 PM5/21/13
to clo...@googlegroups.com
I tend to have macros for different types of setup/teardowns.  Mostly because clojure.test fixtures always want to wrap *every* test in a file.
Reply all
Reply to author
Forward
0 new messages