Skip to content

Instantly share code, notes, and snippets.

@brentonashworth
Created December 3, 2009 06:38
Show Gist options
  • Select an option

  • Save brentonashworth/247939 to your computer and use it in GitHub Desktop.

Select an option

Save brentonashworth/247939 to your computer and use it in GitHub Desktop.
Clojure Test Runner
(ns com.formpluslogic.util.test
(:use [clojure.test]
[clojure.test :only (run-tests)]
[com.formpluslogic.util.file_utils]))
(def test-dir "test")
(def test-file-prefix "test_")
(def test-file-ext ".clj")
(def *logging* false)
(defn set-logging! [b]
(def *logging* b))
(defmacro def-test [name & body]
(if *logging*
`(deftest ~name
(println (str ~name))
(time (do ~@body)))
`(deftest ~name ~@body)))
(defn- is-test-file? [file-info]
(let [file-name (first file-info)
re-s (str test-file-prefix ".*" test-file-ext)
re-p (re-pattern re-s)]
(re-matches re-p file-name)))
(defn- get-file-info [f]
[(.getName f) (file-to-ns-string f test-dir)])
(def test-namespaces
(map #(symbol (last %))
(file-seq-map-filter test-dir get-file-info is-test-file?)))
(defn namespace-filter? [arg]
(if (or (nil? arg) (.startsWith arg "-")) false true))
(defn get-test-namespaces []
(let [arg (first *command-line-args*)]
(if (namespace-filter? arg)
(filter #(.endsWith (name %) arg) test-namespaces)
test-namespaces)))
(defn run-sequential? []
(if (= (first *command-line-args*) "-seq") true false))
(defn run
"Runs all defined tests"
[]
(println "\nLoading tests...")
(let [namespaces (get-test-namespaces)]
(apply require :reload-all namespaces)
(if (run-sequential?)
(time (apply run-tests namespaces))
(let [agents (map #(agent %) namespaces)]
(println "Running tests with" (count agents) "agents")
(time
(do
(dorun (map #(send-off % (fn [x] (with-out-str (time (run-tests x))))) agents))
(apply await agents)
(dorun (map #(println "\nAgent report:\n" (deref %)) agents))))))
(shutdown-agents)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment