<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Clojure Q&amp;A - Recent questions tagged test.check</title>
<link>https://ask.clojure.org/index.php/tag/test.check</link>
<description></description>
<item>
<title>Rose Tree Children Appear to be Eagerly Evaluated (Clojure 1.12+)</title>
<link>https://ask.clojure.org/index.php/15226/rose-tree-children-appear-to-be-eagerly-evaluated-clojure-12</link>
<description>&lt;p&gt;To make a long story shorter, I've been aggressively profiling the generative tests I rely on on a daily basis. In CI, they run on memory-constrained machines which have occasionally ended up in OOM states that cause our test executor to kill the nodes running some generative tests. In many cases, we've been able to adjust sizing to get under the line and resume normally. I've been running into that case more frequently, and have been looking for potential causes/levers to adjust in the source code.&lt;/p&gt;
&lt;p&gt;The below findings are from my relatively powerful Macbook:&lt;/p&gt;
&lt;p&gt;Operating System: MacOS (Tahoe - AArch64)&lt;br&gt;
Clojure version: Verified on 1.12.1, Compared against 1.11.4&lt;br&gt;
JDK vendor and version: OpenJDK 21.0.11&lt;/p&gt;
&lt;hr&gt;
&lt;p&gt;Using &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure-goes-fast/clj-async-profiler&quot;&gt;&lt;code&gt;clj-async-profiler&lt;/code&gt;&lt;/a&gt; and an allocation/timing macro from &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojure-goes-fast.com/kb/benchmarking/time-plus/&quot;&gt;Clojure Goes Fast&lt;/a&gt;, I benchmarked &lt;code&gt;clojure.test.check.generators&lt;/code&gt; against some pretty common generators (Assume I'm using &lt;code&gt;gen&lt;/code&gt; as an alias for that namespace):&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;gen/string&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;gen/large-integer&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;(gen/map gen/keyword gen/string)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;While benchmarking, &lt;code&gt;ReentrantLock&lt;/code&gt; (and &lt;code&gt;ReentrantLock$NonfairSync&lt;/code&gt;), accounted for ~1/3rd of all allocations. Since Clojure 1.12, each &lt;code&gt;LazySeq&lt;/code&gt; also allocates a &lt;code&gt;ReentrantLock&lt;/code&gt;, so I reran the benchmarking after downgrading to Clojure 1.11.4 Obviously, all of those object references were no longer in the profiler results, but the amount of bytes allocated did consistently shrink (~10-15%, depending on the generator).&lt;/p&gt;
&lt;p&gt;Most of the allocation appears to be happening while constructing the children segments of nodes in the rose tree. To my knowledge, we only ever lean on the full tree when shrinking results, and wouldn't read them on a generate-only pass. To test that, I wrapped the &lt;code&gt;children&lt;/code&gt; arguments to a few of the &lt;code&gt;make-rose&lt;/code&gt; calls in the call tree for string generation, and re-instrumented on v.12.1&lt;/p&gt;
&lt;p&gt;This approximated the same reduction in memory use as downgrading to 1.11.4, and wiped out almost all of the &lt;code&gt;ReentrantLock&lt;/code&gt; allocation events. I've been able to verify the fix does not break any tests, and that shrinking is functional as well.&lt;/p&gt;
&lt;p&gt;For posterity / reproduction, the specific call sites I wrapped the &lt;code&gt;children&lt;/code&gt; in were:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;clojure.test.check.rose-tree&lt;/code&gt;&lt;ul&gt;
&lt;li&gt;&lt;code&gt;join&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;fmap&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;shrink&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;shrink-vector*&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;code&gt;clojure.test.check.generators&lt;/code&gt;&lt;ul&gt;
&lt;li&gt;&lt;code&gt;int-rose-tree&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;hr&gt;
&lt;p&gt;Happy to share the profiler output / git patch if anyone is interested!&lt;/p&gt;
</description>
<category>test.check</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/15226/rose-tree-children-appear-to-be-eagerly-evaluated-clojure-12</guid>
<pubDate>Mon, 31 Aug 2026 18:18:35 +0000</pubDate>
</item>
<item>
<title>`(s/gen ...)` caches its failure to load generators and thus is incompatible with `add-lib`</title>
<link>https://ask.clojure.org/index.php/14718/gen-caches-failure-load-generators-thus-incompatible-with</link>
<description>&lt;pre&gt;&lt;code&gt;Clojure 1.12.0
user=&amp;gt; (require '[clojure.spec.alpha :as s])
nil
user=&amp;gt; (s/def ::x #{1 2 3})
:user/x
user=&amp;gt; (s/gen ::x)
Execution error (FileNotFoundException) at user/eval145 (REPL:1).
Could not locate clojure/test/check/generators__init.class, clojure/test/check/generators.clj or clojure/test/check/generators.cljc on classpath.
user=&amp;gt; (add-lib 'org.clojure/test.check)
[org.clojure/test.check]
user=&amp;gt; (s/gen ::x)
Execution error (FileNotFoundException) at user/eval145 (REPL:1).
Could not locate clojure/test/check/generators__init.class, clojure/test/check/generators.clj or clojure/test/check/generators.cljc on classpath.
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>REPL</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/14718/gen-caches-failure-load-generators-thus-incompatible-with</guid>
<pubDate>Tue, 23 Sep 2025 16:23:58 +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>Add option to enable automatically running tests when form is evaluated</title>
<link>https://ask.clojure.org/index.php/12585/option-enable-automatically-running-tests-when-evaluated</link>
<description>&lt;p&gt;Libraries like &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/hyperfiddle/rcf&quot;&gt;RCF&lt;/a&gt; support a programming style where tests are written inline with the source code and always run when the test form is evaluated (for example if the file is reloaded) for fast feedback and better locality of tests and examples - small example tests sit next to the code, and unlike &lt;code&gt;comment&lt;/code&gt; blocks, are at lower risk of going out of sync with the code base.&lt;/p&gt;
&lt;p&gt;This way, forms wrapped with &lt;code&gt;with-tests&lt;/code&gt;, &lt;code&gt;deftest&lt;/code&gt; and &lt;code&gt;set-test&lt;/code&gt; will run their tests when they're evaluated.&lt;/p&gt;
&lt;p&gt;This isn't suitable for long running or heavy tests, which is why having a dynamic var which could be set per namespace seems like a decent fit.&lt;/p&gt;
&lt;p&gt;RCF achieves it by always adding a call to test-var at the end of the test form:&lt;/p&gt;
&lt;p&gt;e.g.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(when *run-tests* `(test-var (var ~name)))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Then tests written inline with code will be able to run automatically when code is loaded and provide immediate feedback&lt;/p&gt;
&lt;p&gt;Alternative: use tools.namespace&lt;/p&gt;
&lt;p&gt;Thouhghts?&lt;/p&gt;
</description>
<category>Test</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12585/option-enable-automatically-running-tests-when-evaluated</guid>
<pubDate>Thu, 26 Jan 2023 11:29:13 +0000</pubDate>
</item>
<item>
<title>CLJS compilation warnings from `test.check` for undeclared Var in `goog.math.Long` from clojure.test.check.random.longs</title>
<link>https://ask.clojure.org/index.php/10541/compilation-warnings-undeclared-clojure-check-random-longs</link>
<description>&lt;p&gt;When compiling &lt;code&gt;test.check&lt;/code&gt; version 1.1.0 with Clojurescript version 1.10.844, I'm seeing warnings about the following missing vars from &lt;code&gt;goog.math.Long&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;WARNING: Use of undeclared Var goog.math.Long/fromBits at line 64 file:~/.m2/repository/org/clojure/test.check/1.1.0/test.check-1.1.0.jar!/clojure/test/check/random/longs.cljs
WARNING: Use of undeclared Var goog.math.Long/fromString at line 77 file:~/.m2/repository/org/clojure/test.check/1.1.0/test.check-1.1.0.jar!/clojure/test/check/random/longs.cljs
WARNING: Use of undeclared Var goog.math.Long/fromNumber at line 81 file:~/.m2/repository/org/clojure/test.check/1.1.0/test.check-1.1.0.jar!/clojure/test/check/random/longs.cljs
WARNING: Use of undeclared Var goog.math.Long/fromNumber at line 87 file:~/.m2/repository/org/clojure/test.check/1.1.0/test.check-1.1.0.jar!/clojure/test/check/random/longs.cljs
WARNING: Use of undeclared Var goog.math.Long/getOne at line 92 file:~/.m2/repository/org/clojure/test.check/1.1.0/test.check-1.1.0.jar!/clojure/test/check/random/longs.cljs
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I believe this showed up after the recent CLJS release, so maybe related to deprecations in the namespaces provided by the latest version of the Google Closure Compiler or associated libraries?&lt;/p&gt;
</description>
<category>ClojureScript</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10541/compilation-warnings-undeclared-clojure-check-random-longs</guid>
<pubDate>Thu, 29 Apr 2021 22:22:21 +0000</pubDate>
</item>
<item>
<title>Generating a function invokes the function</title>
<link>https://ask.clojure.org/index.php/10293/generating-a-function-invokes-the-function</link>
<description>&lt;p&gt;I was experimenting with custom function generators in order to support the scenario describe in spec guide &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojure.org/guides/spec#_combining_check_and_instrument&quot;&gt;https://clojure.org/guides/spec#_combining_check_and_instrument&lt;/a&gt; but with the goal of making the generated values depend on the concrete arguments and not only on the :ret spec.&lt;/p&gt;
&lt;p&gt;While working on that, I noticed a weird behavior that I fail to understand where a generated function is being called 21 times before actually being returned.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(require '[clojure.spec.alpha :as s]
         '[clojure.spec.gen.alpha :as gen])

(s/fdef foo
  :args (s/cat :x int?)
  :gen #(gen/return
         (fn [&amp;amp; argv]
           (prn argv)
           (gen/generate (s/gen string?)))))

(gen/generate (s/gen `foo))
;; Prints:
;; (-1)
;; (-1)
;; (-2)
;; (-1)
;; (3)
;; (-2)
;; (-2)
;; (-1)
;; (-35)
;; (14)
;; (-16)
;; (26)
;; (-17)
;; (150)
;; (0)
;; (-1)
;; (25)
;; (5638)
;; (543)
;; (57)
;; (257)
;; Returns: #function[fspec-gen-bug.core/fn--6577/fn--6578]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This behavior seems specific to function generation for example &lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(s/def ::bar
  (s/spec (s/coll-of int?)
          :gen #(gen/return
                 (doto [(gen/generate (s/gen int?))]
                   prn))))

(gen/generate (s/gen ::bar))
;; Prints:
;; [438803]
;; Returns: [438803]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Is there a good reason why generated functions are invoked eagerly ? or is it a bug ?&lt;/p&gt;
</description>
<category>Spec</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10293/generating-a-function-invokes-the-function</guid>
<pubDate>Sat, 06 Mar 2021 21:55:46 +0000</pubDate>
</item>
<item>
<title>Clarify the position of :/ as a keyword</title>
<link>https://ask.clojure.org/index.php/9427/clarify-the-position-of-as-a-keyword</link>
<description>&lt;p&gt;The Clojure and Clojure reader both accept &lt;code&gt;:/&lt;/code&gt; as a keyword, and test.check generates it. From what I could gather from conversations on Slack however it does not seem to be officially considered valid. For example in &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojurians-log.clojureverse.org/cljs-dev/2020-06-16/1592292452.283200&quot;&gt;this thread&lt;/a&gt;, which mentions there was an earlier discussion in clojure-dev, but unfortunately I was not able to find it.&lt;/p&gt;
&lt;p&gt;It was suggested to remove it from test.check (&lt;a rel=&quot;nofollow&quot; href=&quot;https://clojure.atlassian.net/browse/TCHECK-155&quot;&gt;TCHECK-155&lt;/a&gt;) which was closed with a call to authority and quote  from Alex Miller, however Alex has also stated that he &lt;a rel=&quot;nofollow&quot; href=&quot;https://clojurians-log.clojureverse.org/clojurescript/2019-05-16/1558023591.112600&quot;&gt;considers this a test.check bug&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The quote in TCHECK-155 also mentions that &quot;clojurescript should fix its weirdness&quot;, but I could not find the original conversation so I'm not sure what this refers to or if there's a ticket. I did find that clojurescript behaves differently than clojure in at least one way&lt;/p&gt;
&lt;p&gt;&lt;code&gt;(name (keyword &quot;/&quot;)) 
;;clj =&amp;gt; &quot;&quot;
;;cljs =&amp;gt; &quot;/&quot;&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;I guess my question is, is &lt;code&gt;:/&lt;/code&gt; considered a) valid, b) invalid, or c) undefined / left for future expansion, in EDN and/or in Clojure?&lt;/p&gt;
&lt;p&gt;If it is a) valid, is it then safe to say that &lt;code&gt;(name (keyword &quot;/&quot;))&lt;/code&gt; is a clojurescript bug?&lt;/p&gt;
&lt;p&gt;If it is b) or c), does that mean that TCHECK-155 should be reopened?&lt;/p&gt;
&lt;p&gt;Thanks!&lt;/p&gt;
</description>
<category>test.check</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9427/clarify-the-position-of-as-a-keyword</guid>
<pubDate>Thu, 09 Jul 2020 06:36:27 +0000</pubDate>
</item>
</channel>
</rss>