Back

with-test-out (clj)

(source)

macro

(with-test-out & body)
Runs body with *out* bound to the value of *test-out*.

Examples

lambdaisland/kaocha
(ns kaocha.report-test
  (:require [clojure.test :as t :refer :all]
            [kaocha.report :as report]
            [kaocha.type :as type]
            [kaocha.test-util :refer [with-test-out-str]]
            [kaocha.output :as output]
            [kaocha.hierarchy :as hierarchy]
            [kaocha.history :as history]
            [kaocha.testable :as testable]
            [slingshot.slingshot :refer [try+]]))

(deftest dispatch-extra-keys-test
  (testing "it dispatches to custom clojure.test/report extensions"
    (.addMethod report/clojure-test-report
                ::yolo
                (fn [m]
                  (clojure.test/with-test-out
                    (println "YOLO expected"
                             (:expected m)
                             "actual"
                             (:actual m)))))

    (is (= "YOLO expected :x actual :y\n"
           (with-test-out-str
             (report/dispatch-extra-keys {:type ::yolo
                                     :expected :x
                                     :actual :y})))))

  (testing "it does nothing if there is no matching multimethod implementation"
    (is (= ""
           (with-test-out-str
             (report/dispatch-extra-keys {:type ::nolo})))))

  (testing "it does nothing if it's a key known to Kaocha"
    (hierarchy/derive! ::knowlo :kaocha/known-key)
    (.addMethod report/clojure-test-report
                ::knowlo
                (fn [m] (clojure.test/with-test-out (println "KNOWLO"))))
    (is (= ""
           (with-test-out-str
             (report/dispatch-extra-keys {:type ::knowlo})))))

  (testing "it does nothing if the key is globally marked as \"known\""
    (derive ::knowlo :kaocha/known-key)
    (.addMethod report/clojure-test-report
                ::knowlo
                (fn [m] (clojure.test/with-test-out (println "KNOWLO"))))
    (is (= ""
           (with-test-out-str
             (report/dispatch-extra-keys {:type ::knowlo}))))))

(deftest dots*-test
  (is (= "."
         (with-test-out-str
           (report/dots* {:type :pass}))))

  (is (= "F"
         (with-test-out-str
           (report/dots* {:type :fail}))))

  (is (= "E"
         (with-test-out-str
           (report/dots* {:type :error}))))

  (is (= "P"
         (with-test-out-str
           (report/dots* {:type :kaocha/pending}))))

  (is (= "("
         (with-test-out-str
           (report/dots* {:type :kaocha/begin-group}))))

  (is (= ")"
         (with-test-out-str
           (report/dots* {:type :kaocha/end-group}))))

  (is (= "["
         (with-test-out-str
           (report/dots* {:type :begin-test-suite}))))

  (is (= "]"
         (with-test-out-str
           (report/dots* {:type :end-test-suite}))))

  (is (= "\n"
         (with-test-out-str
           (report/dots* {:type :summary})))))

(deftest result-test
  (is (= "5 tests, 9 assertions, 2 errors, 1 pending, 3 failures.\n"
         (with-test-out-str
           (binding [history/*history* (atom [])]
             (report/result {:type :summary :test 5 :pass 4 :fail 3 :error 2 :pending 1})))))

  (is (= "5 tests, 5 assertions, 0 failures.\n"
         (with-test-out-str
           (binding [history/*history* (atom [])]
             (report/result {:type :summary :test 5 :pass 5 :fail 0 :error 0 :pending 0}))))))

(deftest result-failures-test
  (is (= "\nFAIL in foo/bar-test (foo.clj:42)\nit does the thing\nNumbers are not equal\nExpected:\n  1\nActual:\n  -1 +2\n5 tests, 6 assertions, 1 failures.\n"
         (with-test-out-str
           (binding [
                     history/*history* (atom [{:type :fail
                                               :file "foo.clj"
                                               :line 42
                                               :kaocha/testable {:kaocha.testable/id :foo/bar-test}
                                               :testing-contexts ["it does the thing"]
                                               :expected '(= 1 (+ 1 1))
                                               :actual '(not (= 1 2))
                                               :message "Numbers are not equal"}])]
             (report/result {:type :summary :test 5 :pass 5 :fail 1 :error 0 :pending 0}))))))

(deftest doc-test
  (is (= "--- xxx ---------------------------"
         (with-test-out-str
           (report/doc {:type :begin-test-suite
                   :kaocha/testable {:kaocha.testable/desc "xxx"}}))))


  (is (= "\nxxx"
         (with-test-out-str
           (report/doc {:type :begin-test-ns
                   :kaocha/testable {:kaocha.testable/desc "xxx"}}))))

  (is (= "\n"
         (with-test-out-str
           (report/doc {:type :end-test-ns
                   :kaocha/testable {:kaocha.testable/desc "xxx"}}))))

  (is (= "\n  xxx"
         (with-test-out-str
           (report/doc {:type :begin-test-var
                   :kaocha/testable {:kaocha.testable/desc "xxx"}}))))

  (is (= "\n    level1\n      level2"
         (with-test-out-str
           (with-redefs [report/doc-printed-contexts (atom nil)]
             (binding [t/*testing-contexts* ["level2" "level1"]]
               (report/doc {:type :pass}))))))

  (is (= "\n    level1\n      level2 ERROR"
         (with-test-out-str
           (with-redefs [report/doc-printed-contexts (atom nil)]
             (binding [t/*testing-contexts* ["level2" "level1"]]
               (report/doc {:type :error}))))))

  (is (= "\n    level1\n      level2 FAIL"
         (with-test-out-str
           (with-redefs [report/doc-printed-contexts (atom nil)]
             (binding [t/*testing-contexts* ["level2" "level1"]]
               (report/doc {:type :fail}))))))

  (is (= "\n"
         (with-test-out-str
           (report/doc {:type :summary})))))

(deftest tap-test
  (is (= "ok  (foo.clj:20)\n"
         (with-test-out-str
           (binding [output/*colored-output* false]
             (report/tap {:type :pass
                     :file "foo.clj"
                     :line 20})))))

  (is (= (str "not ok  (foo.clj:20)\n"
              "#  FAIL in  (foo.clj:20)\n"
              "#  Expected:\n"
              "#    3\n"
              "#  Actual:\n"
              "#    -3 +4\n")
         (with-test-out-str
           (binding [output/*colored-output* false]
             (report/tap {:type :fail
                     :file "foo.clj"
                     :line 20
                     :expected '(= 3 4)
                     :actual '(not (= 3 4))}))))))
lambdaisland/kaocha
(ns kaocha.watch-test
  (:require [clojure.test :refer :all]
            [clojure.spec.alpha :as spec]
            [kaocha.watch :as w]
            [kaocha.platform :as platform]
            [kaocha.test-util :as util]
            [lambdaisland.tools.namespace.dir :as ctn-dir]
            [clojure.java.shell :as shell]
            [lambdaisland.tools.namespace.track :as ctn-track]
            [lambdaisland.tools.namespace.reload :as ctn-reload]
            [kaocha.integration-helpers :as integration]
            [clojure.java.io :as io]
            [kaocha.config :as config]
            [clojure.test :as t]
            [clojure.string :as str]
            [slingshot.slingshot :refer [try+]])
  (:import (java.io File)))

    (future (deliver out-str (util/with-test-out-str
                               (t/with-test-out
                                 (util/with-test-ctx
                                   (w/run* config finish? q))))))

    (future (deliver out-str (util/with-test-out-str
                               (t/with-test-out
                                 (util/with-test-ctx
                                   (let [[ec finish!] (w/run config)]
                                     (loop []
                                       (Thread/sleep 100)
                                       (if @finish?
                                         (finish!)
                                         (recur)))
                                     (deliver exit-code @ec)))))))
lambdaisland/kaocha
(ns kaocha.test-util
  (:require [clojure.test :as t]
            [clojure.spec.alpha :as spec]
            [expound.alpha :as expound]
            [kaocha.report :as report]
            [kaocha.testable :as testable]
            [kaocha.output :as output]
            [kaocha.type :as type]))

(defmacro with-test-out-str
  "Evaluates exprs in a context in which [[clojure.test/*test-out*]] is bound to a
  fresh StringWriter. Returns the string created by any nested printing calls
  that use [[clojure.test/with-test-out]]."
  [& body]
  `(let [s# (new java.io.StringWriter)]
     (binding [clojure.test/*test-out* s#]
       ~@body
       (str s#))))
lambdaisland/kaocha
(ns features.steps.kaocha-integration
  ^{:clojure.tools.namespace.repl/load false
    :clojure.tools.namespace.repl/unload false}
  (:require [clojure.edn :as edn]
            [clojure.java.io :as io]
            [clojure.java.shell :as shell]
            [clojure.string :as str]
            [clojure.test :as t :refer :all]
            [kaocha.integration-helpers :refer :all]
            [kaocha.output :as output]
            [kaocha.shellwords :refer [shellwords]]
            [lambdaisland.cucumber.dsl :refer :all]
            [me.raynes.fs :as fs]))

(Then "print output" [m]
  (t/with-test-out
    (println "----out---------------------------------------")
    (println (:out m))
    (println "----err---------------------------------------")
    (println (:err m)))
  m)
slagyr/speclj
(ns speclj.report.clojure-test-spec
  (:require                                                 ;cljs-macros
    [clojure.string :as str]
    [clojure.test]
    ;cljs-include [goog.string] ;cljs bug?
    [speclj.components :refer [new-characteristic new-description]]
    [speclj.core :refer [-new-exception -new-failure describe it should-contain should= with]]
    [speclj.report.clojure-test :refer [new-clojure-test-reporter]]
    [speclj.reporting :refer [report-error report-fail report-pass report-pending report-runs]]
    [speclj.results :refer [error-result fail-result]]
    [speclj.run.standard :refer [run-specs]]))

(defmacro with-test-out-str [& body]
  `(let [s# (new java.io.StringWriter)]
     (binding [clojure.test/*test-out* s#]
       ~@body
       (str s#))))

  (it "reports pass"
    (with-test-out-str (report-pass @reporter nil))
    (should= 1
             (:pass (deref (.report-counters @reporter)))))

  (it "reports pending"
    (with-test-out-str (report-pending @reporter nil))
    (should= 1
             (:pending (deref (.report-counters @reporter)))))

  (it "reports fail"
    (with-test-out-str (report-fail @reporter @a-failure))
    (should= 1
             (:fail (deref (.report-counters @reporter)))))

  (it "reports failure message"
    (let [lines (str/split-lines (with-test-out-str (report-fail @reporter @a-failure)))]
      (should-contain "FAIL in (Crazy flips)" (nth lines 1))
      (should-contain "Expected flips" (nth lines 2))))

  (it "reports error"
    (with-test-out-str (report-error @reporter @an-error))
    (should= 1
             (:error (deref (.report-counters @reporter)))))

  (it "reports error message"
    (let [lines (str/split-lines (with-test-out-str (report-error @reporter @an-error)))]
      (should-contain "ERROR in (unknown)" (nth lines 1))
      (should-contain "Compilation failed" (nth lines 2))
      (should-contain "actual: java.lang.Exception: Compilation failed" (nth lines 4))))

  (it "reports run results"
    (with-test-out-str
      (report-pending @reporter nil)
      (report-pass @reporter nil)
      (report-fail @reporter @a-failure)
      (report-error @reporter @an-error))
    (let [output (with-test-out-str (report-runs @reporter nil))
          lines  (str/split-lines output)]
      (should= 3 (count lines))
      (should= "Ran 3 tests containing 3 assertions." (nth lines 1))
      (should= "1 failures, 1 errors." (nth lines 2))))
  )