Back

report (clj)

(source)

multimethod

Generic reporting function, may be overridden to plug in different report formats (e.g., TAP, JUnit). Assertions such as 'is' call 'report' to indicate results. The argument given to 'report' will be a map with a :type key. See the documentation at the top of test_is.clj for more information on the types of arguments for 'report'.

Examples

clojure
(ns clojure.test-clojure.run-single-test
  (:require [clojure.test :refer [is deftest run-test run-tests]]
            [clojure.test-helper :refer [with-err-string-writer]]
            [clojure.test-clojure.test-fixtures :as tf]))

(deftest reports-missing-var
  (should-print-to-err #"^Unable to resolve .*/function-missing to a test function.*"
    (let [result (eval `(run-test function-missing))]
      (is (nil? result)))))

(deftest reports-non-test-var
  (should-print-to-err #"^.*/not-a-test is not a test.*"
    (let [result (eval `(run-test not-a-test))]
      (is (nil? result)))))
clojure
(ns clojure.test-clojure.protocols
  (:use clojure.test clojure.test-clojure.protocols.examples)
  (:require [clojure.test-clojure.protocols.more-examples :as other]
            [clojure.set :as set]
            clojure.test-helper)
  (:import [clojure.test_clojure.protocols.examples ExampleInterface]))

(deftest marker-tests
  (testing "That a marker protocol has no methods"
    (is (= '() (method-names clojure.test_clojure.protocols.examples.MarkerProtocol))))
  (testing "That types with markers are reportedly satifying them."
    (let [hm (HasMarkers.)
          wgm (WillGetMarker.)]
      (is (satisfies? MarkerProtocol hm))
      (is (satisfies? MarkerProtocol2 hm))
      (is (satisfies? MarkerProtocol wgm)))))
clojure

(ns clojure.test-clojure.compilation
  (:import (clojure.lang Compiler Compiler$CompilerException))
  (:require [clojure.test.generative :refer (defspec)]
            [clojure.data.generators :as gen]
            [clojure.test-clojure.compilation.line-number-examples :as line])
  (:use clojure.test
        [clojure.test-helper :only (should-not-reflect should-print-err-message)]))

(deftest test-fnexpr-type-hint
  (testing "CLJ-1378: FnExpr should be allowed to override its reported class with a type hint."
    (is (thrown? Compiler$CompilerException
                 (load-string "(.submit (java.util.concurrent.Executors/newCachedThreadPool) #())")))
    (is (try (load-string "(.submit (java.util.concurrent.Executors/newCachedThreadPool) ^Runnable #())")
             (catch Compiler$CompilerException e nil)))))
tonsky/datascript
(ns datascript.test.query
  (:require
    #?(:cljs [cljs.test    :as t :refer-macros [is are deftest testing]]
       :clj  [clojure.test :as t :refer        [is are deftest testing]])
    [datascript.core :as d]
    [datascript.db :as db]
    [datascript.test.core :as tdc])
    #?(:clj
      (:import [clojure.lang ExceptionInfo])))

    (testing "Empty coll handling"
      (is (= (d/q '[:find ?id
                    :in $ [?id ...]
                    :where [?id :age _]]
               [[1 :name "Ivan"]
                [2 :name "Petr"]]
               [])
             #{}))
      (is (= (d/q '[:find ?id
                    :in $ [[?id]]
                    :where [?id :age _]]
               [[1 :name "Ivan"]
                [2 :name "Petr"]]
               [])
             #{})))
    
    (testing "Placeholders"
      (is (= (d/q '[:find ?x ?z
                    :in [?x _ ?z]]
                  [:x :y :z])
             #{[:x :z]}))
      (is (= (d/q '[:find ?x ?z
                    :in [[?x _ ?z]]]
                  [[:x :y :z] [:a :b :c]])
             #{[:x :z] [:a :c]})))
    
    (testing "Error reporting"
      (is (thrown-with-msg? ExceptionInfo #"Cannot bind value :a to tuple \[\?a \?b\]"
            (d/q '[:find ?a ?b :in [?a ?b]] :a)))
      (is (thrown-with-msg? ExceptionInfo #"Cannot bind value :a to collection \[\?a \.\.\.\]"
            (d/q '[:find ?a :in [?a ...]] :a)))
      (is (thrown-with-msg? ExceptionInfo #"Not enough elements in a collection \[:a\] to bind tuple \[\?a \?b\]"
            (d/q '[:find ?a ?b :in [?a ?b]] [:a]))))
jakemcc/test-refresh
(ns lein2.sample-report
  (:require [clojure.test :as t]))


(defmulti ^:dynamic my-report :type)

(defmethod my-report :default [m])

(defmethod my-report :pass [m]
  (t/with-test-out
    (t/inc-report-counter :pass)))

(defmethod my-report :error [m]
  (t/with-test-out
    (t/inc-report-counter :error)
    (println "\nERROR in" (t/testing-vars-str m))
    (when-let [message (:message m)] (println message))
    (println "expected:" (pr-str (:expected m)))
    (println "  actual:" (pr-str (:actual m)))
    (println)))

(defmethod my-report :fail [m]
  (t/with-test-out
    (t/inc-report-counter :fail)
    (println "\nFAIL in" (t/testing-vars-str m))
    (when-let [message (:message m)] (println message))
    (println "expected:" (pr-str (:expected m)))
    (println "  actual:" (pr-str (:actual m)))
    (println)))

(defmethod my-report :begin-test-ns [m]
  (t/with-test-out
    (println "\nTesting" (ns-name (:ns m)))))

(defmethod my-report :end-test-ns [m])

(defmethod my-report :summary [m]
  (t/with-test-out
    (println "CUSTOM REPORT")
    (println "\nRan" (:test m) "tests containing"
             (+ (:pass m) (:fail m) (:error m)) "assertions.")
    (println (:fail m) "failures," (:error m) "errors.")))
gojek/ziggurat
(ns ziggurat.middleware.json-test
  (:require [cheshire.core :refer [generate-string]]
            [clojure.test :refer [deftest is join-fixtures testing use-fixtures]]
            [ziggurat.fixtures :as fix]
            [ziggurat.metrics :as metrics]
            [ziggurat.middleware.json :refer [parse-json]]))

(deftest parse-json-test
  (testing "Given a handler function (without passing key-fn), parse-json should call that function on after deserializing the string to JSON object."
    (let [handler-fn-called? (atom false)
          json-message       {:a "A"
                              :b "B"}
          topic-entity-name  "test"
          handler-fn         (fn [{:keys [message metadata]}]
                               (when (and (= json-message message)
                                          (= (:topic metadata) "topic")
                                          (= (:timestamp metadata) 1234567890)
                                          (= (:partition metadata) 1))
                                 (reset! handler-fn-called? true)))]
      ((parse-json handler-fn topic-entity-name) {:message (generate-string json-message) :metadata {:topic "topic" :timestamp 1234567890 :partition 1}})
      (is (true? @handler-fn-called?))))
  (testing "Given a handler function and key-fn as false, parse-json should call that function on after
            deserializing the string without coercing the keys to keywords."
    (let [handler-fn-called? (atom false)
          json-message       {"a" "A" "b" "B"}
          topic-entity-name  "test"
          handler-fn         (fn [{:keys [message metadata]}]
                               (when (and (= json-message message)
                                          (= (:topic metadata) "topic")
                                          (= (:timestamp metadata) 1234567890)
                                          (= (:partition metadata) 1))
                                 (reset! handler-fn-called? true)))]
      ((parse-json handler-fn topic-entity-name false) {:message (generate-string json-message) :metadata {:topic "topic" :timestamp 1234567890 :partition 1}})
      (is (true? @handler-fn-called?))))
  (testing "Given a handler function and a key-fn, parse-json should call that function after
            deserializing the string by applying key-fn to keys."
    (let [handler-fn-called? (atom false)
          key-fn             (fn [k] (str k "-modified"))
          json-message       {"a" "A"
                              "b" "B"}
          topic-entity-name  "test"
          handler-fn         (fn [{:keys [message metadata]}]
                               (is (= {"a-modified" "A", "b-modified" "B"} message))
                               (when (and (= {"a-modified" "A", "b-modified" "B"} message)
                                          (= (:topic metadata) "topic")
                                          (= (:timestamp metadata) 1234567890)
                                          (= (:partition metadata) 1))
                                 (reset! handler-fn-called? true)))]
      ((parse-json handler-fn topic-entity-name key-fn) {:message (generate-string json-message) :metadata {:topic "topic" :timestamp 1234567890 :partition 1}})
      (is (true? @handler-fn-called?))))
  (testing "Should report metrics when JSON deserialization fails"
    (let [handler-fn-called?      (atom false)
          metric-reporter-called? (atom false)
          topic-entity-name       "test"
          json-message            "{\"foo\":\"bar"
          handler-fn              (fn [{:keys [message _]}]
                                    (when (nil? message)
                                      (reset! handler-fn-called? true)))]
      (with-redefs [metrics/multi-ns-increment-count (fn [_ _ _]
                                                       (reset! metric-reporter-called? true))]
        ((parse-json handler-fn topic-entity-name true) {:message json-message :metadata {:topic "topic" :timestamp 1234567890 :partition 1}}))
      (is (true? @handler-fn-called?))
      (is (true? @metric-reporter-called?)))))
hyperfiddle/rcf
(ns hyperfiddle.rcf.reporters
  (:require [clojure.test :as t]
            [clojure.string :as str]))

(defmethod t/report :hyperfiddle.rcf/pass [_m]
  (t/with-test-out
    (t/inc-report-counter :pass)
    (print "✅")
    (flush)))

(defmethod t/report :hyperfiddle.rcf/fail [m]
  (print "❌ ")
  (print (str/triml (with-out-str
                      (binding [t/*test-out* *out*]
                        (t/report (assoc m :type :fail)))))))
brabster/crucible
(ns crucible.assertion
  (:require [clojure.test :as test]
            [crucible.encoding :as enc]))

;; clojure.test doesn't print ex-data which is a pain with clojure.spec.alpha
;; remove when ex-data is printed on test failures by default...
(defmethod test/assert-expr 'encoded-as [msg form]
  (let [expected (nth form 1)
        resource (nth form 2)]
    `(let [result# (try (enc/rewrite-element-data ~resource)
                        (catch ExceptionInfo e# (ex-data e#)))]
       (if (= ~expected result#)
         (test/do-report {:type :pass :expected ~expected :actual ~resource :message ~msg})
         (test/do-report {:type :fail :expected ~expected :actual result# :message ~msg})))))