简体   繁体   中英

How to output successful tests from `lein test`?

Using clojure.test, when running lein test , the default settings only print out the number of assertions, like "Ran 12 tests containing 19 assertions," the details for failed tests, and the namespaces tested. I would like an output of the successful tests as well, so I can see what tests were actually run. Frameworks like Mocha in JS have this behavior by default.

For instance, if the following test passed:

(deftest aTest
  (testing "Simple test"
      (is (= 1 1))))

I would like an output like (formatting is arbitrary):

Testing <namespace>
    Passed: aTest -> Simple test

Ideally, the passed test would also be color coded.

I looked at a few libraries, including lein-test-refresh and humane-test-output, but didn't see what I needed. The other option looks like to rewrite the report function in clojure.test, but I'd like to avoid that if possible.

I think you'll have to write it yourself. If you would like an example of how to leverage the existing deftest into a custom macro, here is an example from the Tupelo library that automatically picks a "name" for the deftest group based on the line number:

(defmacro dotest [& body]
  (let [test-name-sym (symbol (str "dotest-line-" (:line (meta &form))))]
  `(clojure.test/deftest ~test-name-sym ~@body)))

(dotest
  (is (= 5 (+ 2 3))))  ; works!

You could just add in a println and voila!

I remember, the Eftest library provides nice colored output when running tests, you may take a look.

Also, the standard clojure.test framework supports adding your own reports. Check the Test report library for the reference.

Probably, when running the tests from your editor or IDE, it could provide the colored output. Here is my Cider-powered Emacs screenshot with the failure report:

在此输入图像描述

I have searched multiple times for an answer to this question, so here is a complete solution building on @ivan-grishaev's hint using Test Report :

Test Report hooks into clojure.test to provide custom reporters that are then being run with every event occuring through a test run. Those events are passed as maps called "messages". The minimal example just applies clojure.pprint/pprint to the messages, but this only gets you so far. Here is a bit more code to give you a feel how to write custom reporters. The indenting-reporter indents test namespaces, deftest s and testing vars, and lists them all independent of test outcome.

Create a file like this in your project:

(ns my.own.test-reporter
  (:require [clojure.pprint :refer [pprint]]
            [clojure.test :as ct]
            [clojure.core.match :refer [match]]))

(defn simple-reporter [msg]
  (pprint msg))

(defn indenting-reporter [msg]
  (match (:type msg)
         :begin-test-ns (println (str "Testing " (:ns msg) "\n"))
         :begin-test-var (println (str "  " (-> msg :var meta :name)))
         :pass (do (println (str "    " (-> msg :context first) " :pass"))
                   (ct/inc-report-counter :pass))
         :fail (do (println (str "    " (-> msg :context first) " :fail"))
                   (ct/inc-report-counter :fail))
         :error (do (println (str "    " (-> msg :context first) " :error"))
                    (ct/inc-report-counter :error))
         :end-test-ns (println)
         :end-test-var ()
         :summary (pprint msg)
         :else (pprint msg)))

and then use it in the :test profile of your project.clj :

:injections [(require '[my.own.test-reporter :as tr])]
:test-report {:reporters [tr/indenting-reporter]}

You could now go on an add color to the outputs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM