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)

2 Answers 2

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
2
  • 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
    Commented Jun 4, 2019 at 17:03
  • I updated the answer so that all print output is suppressed, but the return value is preserved. Commented Jun 5, 2019 at 14:48
0

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

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.