Catch and handle fixture errors during tests
This commit is contained in:
parent
66c7f9a8ea
commit
61fd4de9db
7 changed files with 64 additions and 0 deletions
|
@ -74,6 +74,25 @@
|
||||||
(let [failures# (atom {})
|
(let [failures# (atom {})
|
||||||
selected-namespaces# ~(form-for-nses-selectors-match selectors ns-sym)
|
selected-namespaces# ~(form-for-nses-selectors-match selectors ns-sym)
|
||||||
_# (when ~*monkeypatch?*
|
_# (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
|
(leiningen.core.injected/add-hook
|
||||||
#'clojure.test/report
|
#'clojure.test/report
|
||||||
(fn [report# m# & args#]
|
(fn [report# m# & args#]
|
||||||
|
|
|
@ -41,6 +41,8 @@
|
||||||
|
|
||||||
(def sample-reader-cond-project (read-test-project "sample-reader-cond"))
|
(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 tricky-name-project (read-test-project "tricky-name"))
|
||||||
|
|
||||||
(def native-project (read-test-project "native"))
|
(def native-project (read-test-project "native"))
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
[leiningen.test.helper :refer [tmp-dir sample-no-aot-project
|
[leiningen.test.helper :refer [tmp-dir sample-no-aot-project
|
||||||
sample-reader-cond-project
|
sample-reader-cond-project
|
||||||
sample-failing-project
|
sample-failing-project
|
||||||
|
sample-fixture-error-project
|
||||||
with-system-err-str]]
|
with-system-err-str]]
|
||||||
[clojure.java.io :as io]
|
[clojure.java.io :as io]
|
||||||
[leiningen.core.main :as main]
|
[leiningen.core.main :as main]
|
||||||
|
@ -97,3 +98,7 @@
|
||||||
(is (= "EOF while reading" (try (test project) false
|
(is (= "EOF while reading" (try (test project) false
|
||||||
(catch Exception e
|
(catch Exception e
|
||||||
(.getMessage e))))))))
|
(.getMessage e))))))))
|
||||||
|
|
||||||
|
(deftest test-catch-fixture-errors
|
||||||
|
(test sample-fixture-error-project)
|
||||||
|
(is (= (ran?) #{:test-a :test-c})))
|
||||||
|
|
7
test_projects/sample-fixture-error/project.clj
Normal file
7
test_projects/sample-fixture-error/project.clj
Normal 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"]])
|
13
test_projects/sample-fixture-error/test/test_a.clj
Normal file
13
test_projects/sample-fixture-error/test/test_a.clj
Normal 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)))
|
11
test_projects/sample-fixture-error/test/test_b.clj
Normal file
11
test_projects/sample-fixture-error/test/test_b.clj
Normal 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)))
|
7
test_projects/sample-fixture-error/test/test_c.clj
Normal file
7
test_projects/sample-fixture-error/test/test_c.clj
Normal 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)))
|
Loading…
Reference in a new issue