January 4, 2020

Building test check Generators

Speaker

Gary Fredericks, developer at DRW

Thoughts

Very interesting talk, many code examples about how to model your domain problem. This was more approachable to a real case usage of the test.check library.

I will start to use it for some small functions in my code base to grow the feel and knowledge about how to use it more broadly.

Don’t write tests! Generate them!

Quotes

test.check likes to start the tests with small inputs and grows as it goes.

Recomendations

  1. There are concrete data generators, and abstract combinators

  2. The abstractness is in service of the value proposition

  3. Like FP - familiarity takes practice

  4. With practice, you can generate anything, and customize its distribution, growth, and shrinking

Code snippets

Simple data generators

(def generate-some-great-data
  (gen/hash-map
   :a-boolean gen/boolean
   :some-small-integer (gen/vector gen/nat)
   :a-large-integer gen/large-integer
   :a-double gen/double
   :a-color (gen/elements [:red :green :blue])
   :a-uuid gen/uuid
   :a-string-and-a-keyword (gen/tuple gen/string
                                      gen/keyword)))

(gen/generate generate-some-great-data 10)

combinators

(gen/tuple g1 g2 ...)
(gen/fmap (fn [x] x') g)
(gen/bind g (fn [x] g'))
(gen/such-that pred g)
(gen/frequency [[w1 g1] [w2 g2] ...])
(gen/one-of [g1 g2 ...])
Tags: nice-code-snippets generative testing clojure