Catch and handle fixture errors during tests

This commit is contained in:
Alex Hall 2016-04-29 21:56:23 +00:00
parent 66c7f9a8ea
commit 61fd4de9db
7 changed files with 64 additions and 0 deletions

View file

@ -74,6 +74,25 @@
(let [failures# (atom {})
selected-namespaces# ~(form-for-nses-selectors-match selectors ns-sym)
_# (when ~*monkeypatch?*
(leiningen.core.injected/add-hook
#'clojure.test/test-ns
(fn [test-ns# ns#]
(try
(test-ns# ns#)
(catch Throwable t#
(binding [clojure.test/*report-counters*
(ref clojure.test/*initial-report-counters*)
clojure.test/*testing-vars*
(list (with-meta 'test
{:name ns#
:ns ns#}))]
(clojure.test/do-report {:type :error
:message "Uncaught exception in test fixture"
:expected nil
:actual t#})
(clojure.test/do-report {:type :end-test-ns
:ns (the-ns ns#)})
@clojure.test/*report-counters*)))))
(leiningen.core.injected/add-hook
#'clojure.test/report
(fn [report# m# & args#]

View file

@ -41,6 +41,8 @@
(def sample-reader-cond-project (read-test-project "sample-reader-cond"))
(def sample-fixture-error-project (read-test-project "sample-fixture-error"))
(def tricky-name-project (read-test-project "tricky-name"))
(def native-project (read-test-project "native"))

View file

@ -5,6 +5,7 @@
[leiningen.test.helper :refer [tmp-dir sample-no-aot-project
sample-reader-cond-project
sample-failing-project
sample-fixture-error-project
with-system-err-str]]
[clojure.java.io :as io]
[leiningen.core.main :as main]
@ -97,3 +98,7 @@
(is (= "EOF while reading" (try (test project) false
(catch Exception e
(.getMessage e))))))))
(deftest test-catch-fixture-errors
(test sample-fixture-error-project)
(is (= (ran?) #{:test-a :test-c})))

View file

@ -0,0 +1,7 @@
;; This project is used for leiningen's test suite, so don't change
;; any of these values without updating the relevant tests. If you
;; just want a basic project to work from, generate a new one with
;; "lein new".
(defproject sample-fixture-error "0.1.0-SNAPSHOT"
:dependencies [[org.clojure/clojure "1.8.0"]])

View file

@ -0,0 +1,13 @@
(ns test-a
(:require [clojure.test :refer :all]
[clojure.java.io :refer [writer]]))
(defn record-ran [t]
(let [file-name (format "%s/lein-test-ran"
(System/getProperty "java.io.tmpdir"))]
(with-open [w (writer file-name :append true)]
(.write w (str t "\n")))))
(deftest test-a
(record-ran :test-a)
(is (= 1 1)))

View file

@ -0,0 +1,11 @@
(ns test-b
(:require [clojure.test :refer :all]
[test-a :refer [record-ran]]))
(use-fixtures :once
(fn [& _]
(throw (Exception. "Don't panic. This is an expected exception."))))
(deftest test-b
(record-ran :test-b)
(is (= 1 1)))

View file

@ -0,0 +1,7 @@
(ns test-c
(:require [clojure.test :refer :all]
[test-a :refer (record-ran)]))
(deftest test-c
(record-ran :test-c)
(is (= 1 1)))