Liking cljdoc? Tell your friends :D

test.assert

an assertion framework.

ASSERTIONS

The core of the library is the "is" macro, which lets you make assertions of any arbitrary expression:

(is (= 4 (+ 2 2))) (is (instance? Integer 256)) (is (.startsWith "abcde" "ab"))

You can type an "is" expression directly at the REPL, which will print a message if it fails.

user> (is (= 5 (+ 2 2)))

FAIL in  (:1)
expected: (= 5 (+ 2 2))
  actual: (not (= 5 4))
false

The "expected:" line shows you the original expression, and the "actual:" shows you what actually happened. In this case, it shows that (+ 2 2) returned 4, which is not = to 5. Finally, the "false" on the last line is the value returned from the expression. The "is" macro always returns the result of the inner expression.

There are two special assertions for testing exceptions. The "(is (thrown? c ...))" form tests if an exception of class c is thrown:

(is (thrown? ArithmeticException (/ 1 0)))

"(is (thrown-with-msg? c re ...))" does the same thing and also tests that the message on the exception matches the regular expression re:

(is (thrown-with-msg? ArithmeticException #"Divide by zero" (/ 1 0)))

DOCUMENTING TESTS

"is" takes an optional second argument, a string describing the assertion. This message will be included in the error report.

(is (= 5 (+ 2 2)) "Crazy arithmetic")

In addition, you can document groups of assertions with the "testing" macro, which takes a string followed by any number of assertions. The string will be included in failure reports. Calls to "testing" may be nested, and all of the strings will be joined together with spaces in the final report, in a style similar to RSpec http://rspec.info/

(testing "Arithmetic" (testing "with positive integers" (is (= 4 (+ 2 2))) (is (= 7 (+ 3 4)))) (testing "with negative integers" (is (= -4 (+ -2 -2))) (is (= -1 (+ 3 -4)))))

Note that, unlike RSpec, the "testing" macro may only be used INSIDE a "deftest" or "with-test" form (see below).

SAVING TEST OUTPUT TO A FILE

All the test reporting functions write to the var test-out. By default, this is the same as out, but you can rebind it to any PrintWriter. For example, it could be a file opened with clojure.java.io/writer.

EXTENDING TEST-IS (ADVANCED)

You can extend the behavior of the "is" macro by defining new methods for the "assert-expr" multimethod. These methods are called during expansion of the "is" macro, so they should return quoted forms to be evaluated.

You can plug in your own test-reporting framework by rebinding the "report" function: (report event)

The 'event' argument is a map. It will always have a :type key, whose value will be a keyword signaling the type of event being reported. Standard events with :type value of :pass, :fail, and :error are called when an assertion passes, fails, and throws an exception, respectively. In that case, the event will also have the following keys:

:expected The form that was expected to be true :actual A form representing what actually occurred :message The string message given as an argument to 'is'

The "testing" strings will be a list in "testing-contexts", and the vars being tested will be a list in "testing-vars".

Your "report" function should wrap any printing calls in the "with-test-out" macro, which rebinds out to the current value of test-out.

For additional event types, see the examples in the code.

an assertion framework.

ASSERTIONS

The core of the library is the "is" macro, which lets you make
assertions of any arbitrary expression:

(is (= 4 (+ 2 2)))
(is (instance? Integer 256))
(is (.startsWith "abcde" "ab"))

You can type an "is" expression directly at the REPL, which will
print a message if it fails.

    user> (is (= 5 (+ 2 2)))

    FAIL in  (:1)
    expected: (= 5 (+ 2 2))
      actual: (not (= 5 4))
    false

The "expected:" line shows you the original expression, and the
"actual:" shows you what actually happened.  In this case, it
shows that (+ 2 2) returned 4, which is not = to 5.  Finally, the
"false" on the last line is the value returned from the
expression.  The "is" macro always returns the result of the
inner expression.

There are two special assertions for testing exceptions.  The
"(is (thrown? c ...))" form tests if an exception of class c is
thrown:

(is (thrown? ArithmeticException (/ 1 0))) 

"(is (thrown-with-msg? c re ...))" does the same thing and also
tests that the message on the exception matches the regular
expression re:

(is (thrown-with-msg? ArithmeticException #"Divide by zero"
                      (/ 1 0)))

DOCUMENTING TESTS

"is" takes an optional second argument, a string describing the
assertion.  This message will be included in the error report.

(is (= 5 (+ 2 2)) "Crazy arithmetic")

In addition, you can document groups of assertions with the
"testing" macro, which takes a string followed by any number of
assertions.  The string will be included in failure reports.
Calls to "testing" may be nested, and all of the strings will be
joined together with spaces in the final report, in a style
similar to RSpec <http://rspec.info/>

(testing "Arithmetic"
  (testing "with positive integers"
    (is (= 4 (+ 2 2)))
    (is (= 7 (+ 3 4))))
  (testing "with negative integers"
    (is (= -4 (+ -2 -2)))
    (is (= -1 (+ 3 -4)))))

Note that, unlike RSpec, the "testing" macro may only be used
INSIDE a "deftest" or "with-test" form (see below).


SAVING TEST OUTPUT TO A FILE

All the test reporting functions write to the var *test-out*.  By
default, this is the same as *out*, but you can rebind it to any
PrintWriter.  For example, it could be a file opened with
clojure.java.io/writer.


EXTENDING TEST-IS (ADVANCED)

You can extend the behavior of the "is" macro by defining new
methods for the "assert-expr" multimethod.  These methods are
called during expansion of the "is" macro, so they should return
quoted forms to be evaluated.

You can plug in your own test-reporting framework by rebinding
the "report" function: (report event)

The 'event' argument is a map.  It will always have a :type key,
whose value will be a keyword signaling the type of event being
reported.  Standard events with :type value of :pass, :fail, and
:error are called when an assertion passes, fails, and throws an
exception, respectively.  In that case, the event will also have
the following keys:

  :expected   The form that was expected to be true
  :actual     A form representing what actually occurred
  :message    The string message given as an argument to 'is'

The "testing" strings will be a list in "*testing-contexts*", and
the vars being tested will be a list in "*testing-vars*".

Your "report" function should wrap any printing calls in the
"with-test-out" macro, which rebinds *out* to the current value
of *test-out*.

For additional event types, see the examples in the code.
raw docstring

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
Move to previous article
Move to next article
Ctrl+/Jump to the search field
× close