Generative testing with Spec and Spec Testλ︎
Clojure spec has been used so far to create specifications for both data and functions.
Now spec and spec test libraries are used not just validity checking, but also to generate random samples of the data that can be used for extensive testing.
Generative testing provides a far more effective alternative to unit testing.
clojure.spec.test/check/instrument verifies that calls to a function satisfy the function's specification, the
clojure.spec.test/check function generates 1000 data values to be used as the inputs to a function, checks that the invocation of the function satisfies its specification, the
fdef. The argument specification,
fdef is used to generate a wide range of results, which are more capable of finding edge cases that fail.
Example: card gameλ︎
practicalli/spec-generative-testing is a simple card game with specifications that are used for basic generative testing.
- Clojure.org guides: Spec - Generators
- API reference: clojure.spec.gen.alpha
- API reference: clojure.spec.test.alpha
- Video: How to do Stateful Property Testing in Clojure?