clojure.test parameterized tests

858 views
Skip to first unread message

Brian Craft

unread,
May 1, 2014, 8:58:51 PM5/1/14
to clo...@googlegroups.com
I have a number of tests that I would like to run against different implementations of a protocol. In clojure.test there doesn't appear to be a way to parameterize a test over the implementations. Is there a good way to do this?

Chris Price

unread,
May 2, 2014, 11:29:23 AM5/2/14
to clo...@googlegroups.com
I have been curious about this too.  I was playing around with it a few weeks ago and came up with this:


Which is pretty gross; it uses `binding` + a dynamic var in the shared test namespace, and then it calls `clojure.test/run-tests` from inside of a `deftest`.  I'm sure there's got to be a better way, but that was as far as I got in the small amount of time that I was able to spend on it.

Brian Craft

unread,
May 2, 2014, 1:08:09 PM5/2/14
to clo...@googlegroups.com
Thanks. I did something similar. I have different implementations per db, so use a global *db* var:

(ct/deftest run-tests
  (matrix1)) ; matrix1 tests against *db*

(ct/deftest test-h2
  (binding [*db* (h2/create-db2 "test" {:subprotocol "h2:mem"})]
    (run-tests)))

(defn test-ns-hook []
  (test-h2))

Clumsy, but seems to be the only option.

Karsten Schmidt

unread,
May 2, 2014, 2:05:56 PM5/2/14
to clo...@googlegroups.com

You can use the `testing` macro and wrap it in a function, which accepts your type/protocol implementation or even individual protocol methods as args. Example here:

https://github.com/thi-ng/geom/blob/master/test/core.org#callable-contexts

--
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/d/optout.

Brian Craft

unread,
May 2, 2014, 2:38:27 PM5/2/14
to clo...@googlegroups.com, in...@toxi.co.uk
Wow, I never would have figured that out from the docs. Thanks.

Just found a different problem with my solution: nested tests, as described in the docs, prevent the use of fixtures. You have to add test-ns-hook when using nested tests, and then fixtures aren't run.

Reid Draper

unread,
May 4, 2014, 2:31:46 PM5/4/14
to clo...@googlegroups.com, in...@toxi.co.uk
Keep in mind too that since test.check/quick-check takes a property as an argument, you can construct a property by simply closing over some implementation. For example:

(defn make-prop
  [impl]
  (prop/for-all [...]
    (= (impl ...) (other ...)))

And then test with different properties created with (make-prop ...).

Reid

Brian Craft

unread,
May 6, 2014, 3:14:42 PM5/6/14
to clo...@googlegroups.com, in...@toxi.co.uk
Using the "testing" macro introduces the same problem with fixtures. Fixtures don't run except around things defined with deftest.

Anyone have an example of a test 1) parameterized, and 2) with working fixture?

Someone suggested the Expectations lib, but I don't see any fixture support there. Anyone using Expectations that can point me to the right place?

Sean Corfield

unread,
May 6, 2014, 3:17:32 PM5/6/14
to clo...@googlegroups.com
signature.asc
Reply all
Reply to author
Forward
0 new messages