Skip to content

Instantly share code, notes, and snippets.

@noisesmith
Created August 2, 2019 23:36
Show Gist options
  • Save noisesmith/0ce03113a3c15aa43177636610b28952 to your computer and use it in GitHub Desktop.
Save noisesmith/0ce03113a3c15aa43177636610b28952 to your computer and use it in GitHub Desktop.
function to run clojure tests under a directory by ns regex
(require '[clojure.java.io :as io]
'[clojure.main :as m]
'[clojure.test :as test])
(defn run-matching-tests
"(re)loads and runs tests under dir/ which match your regex,
useful for running all tests under a specific prefix
or containing a common name element like \"integration\""
([match]
(run-matching-tests match "test/" false))
([match dir]
(run-matching-tests match dir false))
([match dir dry-run?]
(let [test-files (->> (io/file dir)
(file-seq)
(map str)
(filter clj-filename?))
namespaces (into {} ;; each key will be the str of the namespace to match, the val will be a file path
(map (juxt (partial path-to-ns dir) identity))
test-files)
matches (filter #(re-find match %) (keys namespaces))]
;; (re)load them all)
(run! (fn [ns-string]
(println "loading" ns-string "from" (namespaces ns-string))
(when-not dry-run?
(load-file (namespaces ns-string))))
matches)
;; run them all
(apply println "running tests on" matches)
(when-not dry-run?
(apply test/run-tests (map symbol matches))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment