deftest

added
1.1

ns
clojure.test

type
macro

(deftest name & body)

Defines a test function with no arguments.  Test functions may call
other tests, so tests may be composed.  If you compose tests, you
should also define a function named test-ns-hook; run-tests will
call test-ns-hook instead of testing all vars.

Note: Actually, the test body goes in the :test metadata on the var,
and the real function (the value of the var) calls test-var on
itself.

When *load-tests* is false, deftest is ignored.

                ;successful test example
(ns testing)
(use 'clojure.test)


(deftest addition
  (is (= 4 (+ 2 2)))
  (is (= 7 (+ 3 4))))
=> #'testing/addition

(deftest subtraction
  (is (= 1 (- 4 3)))
  (is (= 3 (- 7 4))))
=> #'testing/subtraction

;composing tests
(deftest arithmetic
  (addition)
  (subtraction))
=> #'testing/arithmetic

(run-tests 'testing)

=> Testing testing

Ran 6 tests containing 10 assertions.
0 failures, 0 errors.
{:type :summary, :test 6, :pass 10, :fail 0, :error 0}
            
                ;failure test example

;there is nesting, so when a leaf test fails so does its parents, in this example 2 tests fail, though there was only one real error.

(ns testing)
(use 'clojure.test)


(deftest addition
  (is (= 4 (+ 2 2)))
  (is (= 7 (+ 3 4))))
=> #'testing/addition

(deftest subtraction
  (is (= 1 (- 4 3)))
  (is (= 6 (- 7 4))))           ;error
=> #'testing/subtraction

;composing tests
(deftest arithmetic
  (addition)
  (subtraction))
=> #'testing/arithmetic

(run-tests 'testing)

=> Testing testing

FAIL in (arithmetic subtraction) (NO_SOURCE_FILE:669)
expected: (= 6 (- 7 4))
  actual: (not (= 6 3))

FAIL in (subtraction) (NO_SOURCE_FILE:669)
expected: (= 6 (- 7 4))
  actual: (not (= 6 3))

Ran 6 tests containing 10 assertions.
2 failures, 0 errors.
{:type :summary, :test 6, :pass 8, :fail 2, :error 0}