<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Clojure Q&amp;A - Recent questions tagged clojure.test</title>
<link>https://ask.clojure.org/index.php/tag/clojure.test</link>
<description></description>
<item>
<title>Separate bindable reporting function from clojure.test/report</title>
<link>https://ask.clojure.org/index.php/14089/separate-bindable-reporting-function-from-clojure-report</link>
<description>&lt;p&gt;&lt;strong&gt;Set Up&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;code&gt;clojure.test&lt;/code&gt; is built on the idea that a test will call an assertion function that directly calls a reporting function: &lt;code&gt;is&lt;/code&gt; calls &lt;code&gt;report&lt;/code&gt; (through &lt;code&gt;do-report&lt;/code&gt;). The function &lt;code&gt;report&lt;/code&gt; is a multimethod, allowing for adding or overriding keys to allow for implementing alternative reporting styles. It's also dynamic, meant to be rebound if an entirely new reporting system is to be used (TAP, JUnit, etc).&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Problem&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;As discussed in the excellent &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/lambdaisland/kaocha/blob/15e553ddba5497a91099f0edda8433d66ef1094f/src/kaocha/report.clj#L40-L71&quot;&gt;kaocha source code&lt;/a&gt;, these two approaches don't work together, which leads to issues such as kaocha's workarounds (and monkey-patching), &lt;a rel=&quot;nofollow&quot; href=&quot;https://codeberg.org/leiningen/leiningen/src/commit/795c3a5d68de7a2465582da4184c42d7bd065c76/sample.project.clj#L371-L373&quot;&gt;leiningen&lt;/a&gt;'s monkey-patching, Cursive's test runner not always integrating with other tools due to &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojurians.slack.com/archives/C0744GXCJ/p1720462210536379?thread_ts=1713354339.092179&amp;amp;cid=C0744GXCJ&quot;&gt;binding &lt;code&gt;report&lt;/code&gt;&lt;/a&gt;, and others.&lt;/p&gt;
&lt;p&gt;&lt;a rel=&quot;nofollow&quot; href=&quot;https://www.lambdasierra.com/2022/hello&quot;&gt;Alessandra Sierra&lt;/a&gt; wrote about &lt;a rel=&quot;nofollow&quot; href=&quot;https://stuartsierra.com/2010/07/05/lazytest-status-report&quot;&gt;this issue&lt;/a&gt; as well when she attempted a follow-up to &lt;code&gt;clojure.test&lt;/code&gt; called Lazytest (which I have resurrected over the last year). She ended up not finishing the library, and many other alternatives to Clojure test have not been as successful as one might hope either.&lt;/p&gt;
&lt;p&gt;Given that &lt;code&gt;clojure.test&lt;/code&gt; is here to stay, it seems prudent to make it easier to use and adapt without changing the fundamental api.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Potential Solution&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Separating the &quot;dynamic variable to be rebound&quot; from &quot;the default reporting function&quot; would make it easier for libraries and tools to choose to extend &lt;code&gt;report&lt;/code&gt; or replace it entirely, without worrying that they'll clash or run into each other. This can be accomplished by adding a new dynamic variable &lt;code&gt;*reporter*&lt;/code&gt; to &lt;code&gt;clojure.test&lt;/code&gt; that &lt;code&gt;do-report&lt;/code&gt; calls, and defaulting &lt;code&gt;*reporter*&lt;/code&gt; to &lt;code&gt;clojure.test/report&lt;/code&gt;. (&lt;code&gt;report&lt;/code&gt; would stay dynamic so as to not change working code.)&lt;/p&gt;
&lt;p&gt;With such a change in place, workarounds such as Leiningen's &lt;a rel=&quot;nofollow&quot; href=&quot;https://codeberg.org/leiningen/leiningen/src/commit/b72ac7840c34e89eb358bada5ab5c2466008d476/src/leiningen/test.clj#L115-L129&quot;&gt;monkey-patching&lt;/a&gt; could simply bind &lt;code&gt;*report*&lt;/code&gt; with a new function and not worry about stepping on any other library's toes.&lt;/p&gt;
</description>
<category>Test</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14089/separate-bindable-reporting-function-from-clojure-report</guid>
<pubDate>Tue, 27 Aug 2024 18:38:20 +0000</pubDate>
</item>
<item>
<title>How to suppress/capture the output of defspec for use in clojure.test?</title>
<link>https://ask.clojure.org/index.php/14040/how-suppress-capture-the-output-defspec-for-use-clojure-test</link>
<description>&lt;p&gt;I've been a fan of &lt;code&gt;test.check&lt;/code&gt; since the beginning. However, I've never found a convenient way to capture the output when using &lt;code&gt;defspec&lt;/code&gt;.  When used with &lt;code&gt;clojure.test&lt;/code&gt;, one gets output that looks like:&lt;/p&gt;
&lt;p&gt;&lt;code&gt;Testing tst.tupelo.y64
{:result true, :num-tests 999, :seed 1722976402694, :time-elapsed-ms 53, :test-var &quot;dospec-line-53&quot;}
{:result true, :num-tests 999, :seed 1722976402747, :time-elapsed-ms 36, :test-var &quot;dospec-line-44&quot;}&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;somewhat polluting the unit test output.  How can one capture and/or suppress this output?&lt;/p&gt;
</description>
<category>test.check</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14040/how-suppress-capture-the-output-defspec-for-use-clojure-test</guid>
<pubDate>Tue, 06 Aug 2024 20:41:33 +0000</pubDate>
</item>
<item>
<title>cljs.core/test does not work as the docstring describes</title>
<link>https://ask.clojure.org/index.php/13756/cljs-core-test-does-not-work-as-the-docstring-describes</link>
<description>&lt;p&gt;&lt;code&gt;cljs.core/test&lt;/code&gt; does not work according to its docstring description.&lt;/p&gt;
&lt;p&gt;The definition of cljs.core/test is&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defn test
  &quot;test [v] finds fn at key :test in var metadata and calls it,
  presuming failure will throw exception&quot;
  [v]
  (let [f (.-cljs$lang$test v)]
    (if f
      (do (f) :ok)
      :no-test)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It's intended use, as far as I understand it, is as such (example copied from the documented Clojure version of the same function at ClojureDocs, &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojuredocs.org/clojure.core/test#example-542692cac026201cdc326b8a&quot;&gt;https://clojuredocs.org/clojure.core/test#example-542692cac026201cdc326b8a&lt;/a&gt; ):&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defn my-function
  &quot;this function adds two numbers&quot;
  {:test #(do
            (assert (= (my-function 2 3) 5))
            (assert (= (my-function 4 4) 8)))}
  ([x y] (+ x y)))

(test #'my-function)  ;equal to (test (var my-function))
=&amp;gt; :ok
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So, &lt;code&gt;cljs.core/test&lt;/code&gt; enables functions to self-document their capabilities via tests made available in metadata under the &lt;code&gt;:test&lt;/code&gt; key.&lt;/p&gt;
&lt;p&gt;However, repeating the above in a CLJS environment yields a different result:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defn my-function
  &quot;this function adds two numbers&quot;
  {:test #(do
            (assert (= (my-function 2 3) 5))
            (assert (= (my-function 4 4) 8)))}
  ([x y] (+ x y)))

(test #'my-function)
=&amp;gt; :no-test
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The interesting thing is that, given that I evalated the above in a CLJS REPL, the metadata &lt;em&gt;is&lt;/em&gt; available, as far as I can tell:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(meta #'my-function)
=&amp;gt;
{:ns cljs.user,
 :name my-function,
 :file &quot;&amp;lt;cljs repl&amp;gt;&quot;,
 :end-column 18,
 :source &quot;my-function&quot;,
 :column 1,
 :line 1,
 :end-line 1,
 :arglists ([x y]),
 :doc &quot;this function adds two numbers&quot;, 
 :test #object[Function]}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This can be made even clearer by substituting the rather opaque function for a keyword:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defn my-function
  &quot;this function adds two numbers&quot;
  {:test :just-a-keyword}
  ([x y] (+ x y)))

(meta #'my-function)
=&amp;gt;
{:ns cljs.user,
 :name my-function,
 :file &quot;&amp;lt;cljs repl&amp;gt;&quot;,
 :end-column 18,
 :source &quot;my-function&quot;,
 :column 1,
 :line 1,
 :end-line 1,
 :arglists ([x y]),
 :doc &quot;this function adds two numbers&quot;, 
 :test :just-a-keyword}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I spoke to @theller and @hiredman about this on the Clojurians Slack, and they speculated that &lt;code&gt;cljs.core/test&lt;/code&gt; has been repurposed to execute the body of &lt;code&gt;deftest&lt;/code&gt;s, since the two pieces seem to fit together. Observe:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(require '[cljs.test :as test])
(test/deftest foo (test/is (= 1 2)))
=&amp;gt; #'cljs.user/foo

(test foo)
FAIL in () (&amp;lt;NO_SOURCE_FILE&amp;gt;:1:28)
expected: (= 1 2)
  actual: (not (= 1 2))

:ok
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;deftest&lt;/code&gt; body is executed and the keyword &lt;code&gt;:ok&lt;/code&gt; is returned (regardless of the &lt;code&gt;is&lt;/code&gt; result), as can also be gathered from reading definition of &lt;code&gt;cljs.core/test&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;If it is true that &lt;code&gt;cljs.core/test&lt;/code&gt; has been repurposed in this manner, then two things are also true:&lt;/p&gt;
&lt;p&gt;A) If it ever were the intent that &lt;code&gt;cljs.core/test&lt;/code&gt; should enable testing &lt;code&gt;:test&lt;/code&gt; assertions for &lt;code&gt;defn&lt;/code&gt;s, that no longer holds.&lt;br&gt;
B) The docstring of &lt;code&gt;cljs.core/test&lt;/code&gt; is somewhat misleading.&lt;/p&gt;
&lt;p&gt;The first bit of the docstring, &lt;code&gt;&quot;test [v] finds fn at key :test in var metadata and calls it&quot;&lt;/code&gt;, may be technically correct, but mentions no connection with &lt;code&gt;deftest&lt;/code&gt;, and therefore somewhat unhelpful. &lt;/p&gt;
&lt;p&gt;The second bit of the docstring, &lt;code&gt;&quot;, presuming failure will throw exception&lt;/code&gt;&quot;, is certainly not correct when used for &lt;code&gt;deftest&lt;/code&gt; - the &lt;code&gt;cljs.test/is&lt;/code&gt; macro, which I think is fair to say that most &lt;code&gt;deftest&lt;/code&gt;s use, catches exceptions and reports instead. I think the original intent is that the execption is thrown, so &lt;code&gt;:ok&lt;/code&gt; is not returned, but in this context, that construct fails.&lt;/p&gt;
&lt;p&gt;The docstring seems to be a carbon copy of the Clojure version, so possibly, it warrants an update. Or maybe &lt;code&gt;cljs.core/test&lt;/code&gt; is simply unused and redundant at this point?&lt;/p&gt;
</description>
<category>ClojureScript</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13756/cljs-core-test-does-not-work-as-the-docstring-describes</guid>
<pubDate>Tue, 27 Feb 2024 12:33:17 +0000</pubDate>
</item>
<item>
<title>Way to set clojure.test/*load-tests* when starting a REPL</title>
<link>https://ask.clojure.org/index.php/12626/way-to-set-clojure-test-load-tests-when-starting-a-repl</link>
<description>&lt;p&gt;Is there a way to set &lt;code&gt;*load-tests*&lt;/code&gt; when starting the REPL?&lt;br&gt;
Some options can be set using system properties, like compiler options, any chance for something similar for clojure.test?&lt;/p&gt;
</description>
<category>Test</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12626/way-to-set-clojure-test-load-tests-when-starting-a-repl</guid>
<pubDate>Thu, 02 Feb 2023 14:13:28 +0000</pubDate>
</item>
</channel>
</rss>