On 10/28/2011 09:42 AM, AndyK wrote:
> I am setting up tests with clojure.test that are driven by a CSV where
> each line represents one test case. Right now, there is a single
> deftest function that runs all the assertions. That's ok but creates
> reporting like 1 test was run with 1000s of assertions. Clojure being
> so dynamic, is it possible to create tests on-the-fly and run them
> where each dynamic test represents each row so that the reporting says
> X tests (where X == number of CSV rows).
> 
> I'm fairly new to clojure and quite unfamiliar with the ins-and-outs
> of clojure.test.
> Any pointers here would be appreciated.
> 
> Thank you
> 
It absolutely would be possible, and furthermore this is an area where
macros really shine.

I would choose macros because from what you describe, it sounds like
you'd like to write a program that generates a bunch of deftest forms,
and then runs those tests. But you need language facilities like reading
from a csv file in order to do so. Clojure (and indeed all lisps) give
you this ability.

You could write a macro that reads in the CSV file and for each line,
generates a deftest form. Below is a quick sketch of what it might look
like if your CSV file consisted of two columns of values that were
supposed to be equal to each other.

(use 'clojure.test)
(require '[clojure.java.io :as io]
         '[clojure.data.csv :as csv])

(defn testdef-form [n [expected actual]]
  `(deftest ~(str "testfromline" n)
     (is (= ~expected ~actual))))

(defmacro defcsvtests [filename]
  (with-open [in-file (io/reader "in-file.csv")]
    (let [testdefs (csv/read-csv in-file)]
      `(do ~@(map testdef-form (iterate inc 1) testdefs)))))

(defcsvtests "test-cases.csv")

-- 
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
Note that posts from new members are moderated - please be patient with your 
first post.
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

Reply via email to