Welcome! Please see the About page for a little more info on how this works.

0 votes
in Test by

Start a socket repl

clojure -A:socket2
Clojure 1.10.3
user=> (require '[clojure.test :as t])
nil
user=> (t/deftest foo (t/is (= 1 2)))
#'user/foo
user=> (foo)

FAIL in (foo) (NO_SOURCE_FILE:1)
expected: (= 1 2)
  actual: (not (= 1 2))
nil
user=>

This prints the test results correctly. In another terminal, connect to the socket repl

% nc localhost 60606
user=> (foo)
nil
user=>

This causes the report to be printed in the original terminal repl, not the socket repl.

but if you bind you can keep your test results:

user=> (binding [t/*test-out* *out*] (foo))

FAIL in (foo) (NO_SOURCE_FILE:1)
expected: (= 1 2)
  actual: (not (= 1 2))
nil
user=>

1 Answer

0 votes
by

I believe this is "as designed" and is relied on by some of the utilities that extend clojure.test's behavior. The clojure.test ns docstring has this to say:

SAVING TEST OUTPUT TO A FILE

   All the test reporting functions write to the var *test-out*.  By
   default, this is the same as *out*, but you can rebind it to any
   PrintWriter.  For example, it could be a file opened with
   clojure.java.io/writer.

There's a with-test-out macro that is specifically intended for use by test reporters so they can rely on this behavior:

(defmacro with-test-out
  "Runs body with *out* bound to the value of *test-out*."
  {:added "1.1"}
  [& body]
  `(binding [*out* *test-out*]
     ~@body))
by
I kinda agree with you. But this is an example where it did not go to `*out*`. It went to someone else's `*out*` because whoever first requires clojure.test sets the `*out*` rather than it using the current value of `*out*`.
by
Given how stateful clojure.test is already, I'm not really sure how it could work differently since there are many "run test" entry points and several of them call through each other so where could the default binding to *out* safely occur in a way that users could still easily override it for their use case (such as a test runner)/
by
Yeah i agree. I just wanted to follow the stu bug report format and submit what feels like buggy behavior and see if anyone had an idea. If the answer is keep using the binding that's what i'll do. But it would be nice to have better support for this.
...