Anatomy of a test
There are three macros in the clojure.test
namespace, which are the trifecta of testing in Clojure: deftest
, is
, and testing
. These three macros are explained as follows:
deftest
: This is similar todef
ordefn
and defines our test function. Tests may call other tests, however, this is a practice that I consider dangerous, as it can result in fragile tests that are, by their nature, rather difficult to debug.Usage: (
deftest name & body
)is
: This is used to make an assertion in our test. The predicate we pass tois
should return a Boolean. We can also optionally provide a message, which will be attached to the assertion and displayed as part of the failure written tostdout
.Usage: (
is form
)(
is form message
)is
also allows the following two special forms that can be used to check for exceptions:thrown?
: This is used to ensure that an exception of a specific type is thrown, and if not, fails the test.Usage: (
thrown? e form
)Here's an example of this form:
...