Skip to content

Instantly share code, notes, and snippets.

Unit Test

The definition of Unit Test is quite general:

In computer programming, unit testing is a method by which individual units of source code, sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures are tested to determine if they are fit for use. [...] A unit could be an entire module, but it is more commonly an individual function or procedure. --Wikipedia

;; could work
(expect
(more-of tree
SUCCESS (tick {:foo 41} tree {})
SUCCESS (tick {:foo 41} tree {}))
(aidoc/compile [:test? {:key :foo :val 42}]))
;; but I strongly dislike putting fn calls in more. I'd probably use from-each
(expect SUCCESS
(let [tree (aidoc/compile [:test? {:key :foo :val 42}])]

Types of Tests

Before we get back to concrete examples there are a few terms we'll want to define: State Verification, Behavior Verification, Solitary Unit Test, and Sociable Unit Test. These terms have plenty of definitions available on the Internet; unfortunately, many of those definitions differ and some are in direct conflict. This chapter will define those terms for the context of this book.

Strongly Recommended Reference Material

@jaycfields
jaycfields / some.clj
Created December 23, 2013 19:54
let can be the first binding to doseq, and it will be executed exactly 1 time.
(doseq [:let [a (do (println 1) 99)]
x [11 12]]
(println x a))
;; output:
;; 1
;; 11 99
;; 12 99
(expect (interaction (one "hello" {:a :b :c {:dd :ee :ff :gg}}))
(do
(one "hello")
(one "hello" "world" "here")
(one "hello" {:a 1 2 3})))
(given [the-answer]
(expect (= 42 the-answer))
42)
;;; or, if you want to define more context
(given [x y]
(expect x (* 2 y))
42 84
20 (inc (dec 10)))
(given [the-answer]
(expect (= 42 the-answer))
42)
(declare *the-answer*) ; outside of a describe
(around [it]
(binding [*the-answer* 42]
(it)))
(it "knows the answer"
(should= 42 *the-answer*))
(define-clojure-indent
(expect 'defun)
(expect-let 'defun)
(given 'defun)
(context 1)
(freeze-time 1)
(redef-state 1)
(from-each 1))
@jaycfields
jaycfields / e.el
Last active December 18, 2015 00:59
;; clojure.core
;; 141 false greek: ⊥ - http://en.wikipedia.org/wiki/Tautology_(logic)
;; 163 -> key-chord: kl
;; 217 true greek: ⊤ - http://en.wikipedia.org/wiki/Tautology_(logic)
;; 255 nil greek: ∅ - http://en.wikipedia.org/wiki/Null_(mathematics)
;; 316 :keys key-chord: kk, greek: ӄ
;; testing
;; 224 interaction greek: ι
;; 265 no-op greek: ⒩