0

Question

I would like to black hole print like behaviors within my test bodies in order to keep my log output looking clean and tidy.

(deftest some-test
   (testing "something"
      (logless 
         (is (= 22 (test-thing 14))))))

I expect test-thing to call println and other similar calls to *out* and would like those to stop polluting my test output.

Is there a recognized way to do this in general?

I found this guy (with-out-str) but it's capturing the string, not quite what I'm looking for.

Background

I'm fairly new to Clojure, coming largely from a javascript world. Having a blast so far! But there's lots left for me to learn.

In Clojure, not Clojure.script (if it matters)

Suni
  • 633
  • 8
  • 16

2 Answers2

2

Just use with-out-str and then ignore the string.

Note that this will not capture error messages or messages from Java libraries. If you want to capture and/or suppress this output, I have written 3 additional functions in the Tupelo library that you may find useful:

The code looks like this:

   (defmacro with-system-err-str
     "Evaluates exprs in a context in which JVM System/err is bound to a fresh
     PrintStream.  Returns the string created by any nested printing calls."
     [& body]
     `(let [baos# (ByteArrayOutputStream.)
            ps#   (PrintStream. baos#)]
        (System/setErr ps#)
        ~@body
        (System/setErr System/err)
        (.close ps#)
        (.toString baos#)))

If you wanted, you could make a new macro like so:

(defmacro with-printing-suppressed
  "Evaluates exprs in a context in which JVM System/err and System/out captured & discarded."
  [& body]
  `(let [baos# (ByteArrayOutputStream.)
         ps#   (PrintStream. baos#)
         s#    (new java.io.StringWriter)]
     (System/setErr ps#)
     (System/setOut ps#)
     (binding [*err* s#
               *out* s#]
       (let [result# (do ~@body)]
         (.close ps#)
         (System/setErr System/err)
         (System/setOut System/out)
         result#))))

(defn stuff []
  (println "***** doing stuff *****")
  42)

and then test it:

  (println "calling - before")
  (is= 42 (with-printing-suppressed 
            (stuff)))
  (println "calling - after")

with result:

calling - before
calling - after
Alan Thompson
  • 29,276
  • 6
  • 41
  • 48
  • 1
    Unless I'm screwwing something up (very possible!) this also appears to swallow the return value which is causing me some troubles. So I suppose I wanted something more like `with-no-err-out` while retaining the return. Also, thank you for the very educational and fast response! – Suni Jun 04 '19 at 17:03
  • I updated the answer so that all print output is suppressed, but the return value is preserved. – Alan Thompson Jun 05 '19 at 14:48
0

Use default logging and logback.xml for output configuration. default clojure logging

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/30008289) – sorosh_sabz Oct 06 '21 at 21:10