A BDD testing framework.
In short: Tests are defined with defdescribe
, suites (groups of nested suites and test cases) are defined with describe
, test cases are defined with it
, and assertions are defined with expect
but can be any function that throws an exception on failure. When lazytest.main/run
is called, all loaded namespaces are checked for tests, and the contained test cases are run (with suites providing context and additional documentation).
Hooks can be used in :context
metadata on suites, and assertion helpers can be used in test cases.
For more information, please check the README.md
.
Test definition vars:
expect
: Create an assertion. Throws if expression returns logical false.it
: Creates a test case, which runs arbitrary code. If the code throws, it's considered a failing test. Otherwise, it's considered a passing test.expect-it
: Combination of expect
and it
to assert a single expression.describe
: Creates a test suite, which is a collection of test cases or other test suites.defdescribe
: Defines a var containing a test suite.Test definition aliases:
Hook vars:
before
, after
: Runs arbitrary code once before (or after) all nested suites and test cases.before-each
, after-each
: Runs arbitrary code before (or after) each nested test case.around
: Run a function taking a single arg, with the arg being a function executing the nested suites or test cases. Useful for binding
(or similar) callsset-ns-context!
: Add hooks to the namespace suite, instead of to a test suite.Assertion helper vars:
throws?
: Calls given no-arg function, returns true if it throws expected class.throws-with-msg?
: Calls given function with no arguments, returns true if it throw expected class and message matches given regex.causes?
: Calls given no-arg function, returns true if it throws expected class anywhere in the cause chain.causes-with-msg?
: Calls given no-arg function, returns true if it throws expected class anywhere in the cause chain and the cause's message matches given regex.ok?
: Calls given no-arg function, returns true if no exception is thrown. (Useful for ignoring logical false return values.)A BDD testing framework. In short: Tests are defined with [[defdescribe]], suites (groups of nested suites and test cases) are defined with [[describe]], test cases are defined with [[it]], and assertions are defined with [[expect]] but can be any function that throws an exception on failure. When `lazytest.main/run` is called, all loaded namespaces are checked for tests, and the contained test cases are run (with suites providing context and additional documentation). Hooks can be used in `:context` metadata on suites, and assertion helpers can be used in test cases. For more information, please check the `README.md`. Test definition vars: * [[expect]]: Create an assertion. Throws if expression returns logical false. * [[it]]: Creates a test case, which runs arbitrary code. If the code throws, it's considered a failing test. Otherwise, it's considered a passing test. * [[expect-it]]: Combination of [[expect]] and [[it]] to assert a single expression. * [[describe]]: Creates a test suite, which is a collection of test cases or other test suites. * [[defdescribe]]: Defines a var containing a test suite. Test definition aliases: * [[should]]: Alias for [[expect]]. * [[specify]]: Alias for [[it]]. * [[context]]: Alias for [[describe]]. Hook vars: * [[before]], [[after]]: Runs arbitrary code once before (or after) all nested suites and test cases. * [[before-each]], [[after-each]]: Runs arbitrary code before (or after) each nested test case. * [[around]]: Run a function taking a single arg, with the arg being a function executing the nested suites or test cases. Useful for `binding` (or similar) calls * [[set-ns-context!]]: Add hooks to the namespace suite, instead of to a test suite. Assertion helper vars: * [[throws?]]: Calls given no-arg function, returns true if it throws expected class. * [[throws-with-msg?]]: Calls given function with no arguments, returns true if it throw expected class and message matches given regex. * [[causes?]]: Calls given no-arg function, returns true if it throws expected class anywhere in the cause chain. * [[causes-with-msg?]]: Calls given no-arg function, returns true if it throws expected class anywhere in the cause chain and the cause's message matches given regex. * [[ok?]]: Calls given no-arg function, returns true if no exception is thrown. (Useful for ignoring logical false return values.)
(->ex-failed expr data)
(->ex-failed &form expr {:keys [message evaluated actual]})
Useful for all expectations. Sets the base properties on the ExpectationFailed.
Useful for all expectations. Sets the base properties on the ExpectationFailed.
(after & body)
Runs body
(presumably for side effects) once after all nested suites and test cases. Wraps body
in a function, calls and discards the result during test runs.
If called outside of a *context*
, returns {:after (fn [] ~@body)}
.
Runs `body` (presumably for side effects) once after all nested suites and test cases. Wraps `body` in a function, calls and discards the result during test runs. If called outside of a `*context*`, returns `{:after (fn [] ~@body)}`.
(after-each & body)
Runs body
(presumably for side effects) after each nested test case. Wraps body
in a function, calls and discards the result during test runs.
If called outside of a *context*
, returns {:after-each (fn [] ~@body)}
.
Runs `body` (presumably for side effects) after each nested test case. Wraps `body` in a function, calls and discards the result during test runs. If called outside of a `*context*`, returns `{:after-each (fn [] ~@body)}`.
(around param & body)
Builds a function for the around
context. Works like an anonymous function:
(around [f]
(binding [*foo* 100]
(f)))
Builds a function for the `around` context. Works like an anonymous function: ```clojure (around [f] (binding [*foo* 100] (f))) ```
(before & body)
Runs body
(presumably for side effects) once before all nested suites and test cases. Wraps body
in a function, calls and discards the result during test runs.
If called outside of a *context*
, returns {:before (fn [] ~@body)}
.
Runs `body` (presumably for side effects) once before all nested suites and test cases. Wraps `body` in a function, calls and discards the result during test runs. If called outside of a `*context*`, returns `{:before (fn [] ~@body)}`.
(before-each & body)
Runs body
(presumably for side effects) before each nested test case. Wraps body
in a function, calls and discards the result during test runs.
If called outside of a *context*
, returns {:before-each (fn [] ~@body)}
.
Runs `body` (presumably for side effects) before each nested test case. Wraps `body` in a function, calls and discards the result during test runs. If called outside of a `*context*`, returns `{:before-each (fn [] ~@body)}`.
(causes-with-msg? c re f)
Calls f with no arguments; catches exceptions with an instance of class c in their cause chain. If the message of the causing exception does not match re (with re-find), throws ExpectationFailed. Any non-matching exception will be re-thrown. Returns false if f throws no exceptions.
Calls f with no arguments; catches exceptions with an instance of class c in their cause chain. If the message of the causing exception does not match re (with re-find), throws ExpectationFailed. Any non-matching exception will be re-thrown. Returns false if f throws no exceptions. Useful in [[expect]] or [[expect-it]].
(causes? c f)
Calls f with no arguments; returns true if it throws an exception whose cause chain includes an instance of class c. Any other exception will be re-thrown. Returns false if f throws no exceptions.
Calls f with no arguments; returns true if it throws an exception whose cause chain includes an instance of class c. Any other exception will be re-thrown. Returns false if f throws no exceptions. Useful in [[expect]] or [[expect-it]].
(defdescribe test-name & children)
(defdescribe test-name doc? attr-map? & children)
describe
helper that assigns a describe
call to a Var of the given name.
test-name is a symbol.
doc (optional) is a documentation string. Unlike the other helpers, this doc must be a string literal.
attr-map (optional) is a metadata map.
children are test cases (see 'it') or nested test suites (see 'describe').
`describe` helper that assigns a `describe` call to a Var of the given name. test-name is a symbol. doc (optional) is a documentation string. Unlike the other helpers, this doc must be a string literal. attr-map (optional) is a metadata map. children are test cases (see 'it') or nested test suites (see 'describe').
(describe doc & children)
(describe doc attr-map? & children)
Defines a suite of tests.
doc is a documentation string expression. If the expression is a local, it will be used as is. If the expression is a resolvable symbol, it will be resolved and used as a var. Otherwise, it's evaluated like normal.
attr-map (optional) is a metadata map.
children are test cases or nested test suites.
Defines a suite of tests. doc is a documentation string expression. If the expression is a local, it will be used as is. If the expression is a resolvable symbol, it will be resolved and used as a var. Otherwise, it's evaluated like normal. attr-map (optional) is a metadata map. children are test cases or nested test suites.
(expect expr)
(expect expr msg)
Evaluates expression. If it returns logical true, returns that result. If the expression returns logical false, throws lazytest.ExpectationFailed with an attached map describing the reason for failure. Metadata on expr and on the 'expect' form itself will be merged into the failure map.
Evaluates expression. If it returns logical true, returns that result. If the expression returns logical false, throws lazytest.ExpectationFailed with an attached map describing the reason for failure. Metadata on expr and on the 'expect' form itself will be merged into the failure map.
(expect-it doc expr)
(expect-it doc|sym? attr-map? expr)
Defines a single test case that wraps the given expr in an expect
call.
doc is a documentation string expression. If the expression is a local, it will be used as is. If the expression is a resolvable symbol, it will be resolved and used as a var. Otherwise, it's evaluated like normal.
attr-map (optional) is a metadata map
expr is a single expression, which must return logical true to indicate the test case passes or logical false to indicate failure.
Defines a single test case that wraps the given expr in an `expect` call. doc is a documentation string expression. If the expression is a local, it will be used as is. If the expression is a resolvable symbol, it will be resolved and used as a var. Otherwise, it's evaluated like normal. attr-map (optional) is a metadata map expr is a single expression, which must return logical true to indicate the test case passes or logical false to indicate failure.
(given bindings & body)
DEPRECATED: No longer needed. Use a normal let
, please.
Alias for 'let'.
DEPRECATED: No longer needed. Use a normal `let`, please. Alias for 'let'.
(it doc & body)
(it doc|sym? attr-map? & body)
Defines a single test case that may execute arbitrary code.
doc is a documentation string expression. If the expression is a local, it will be used as is. If the expression is a resolvable symbol, it will be resolved and used as a var. Otherwise, it's evaluated like normal.
attr-map (optional) is a metadata map
body is any code, which must throw an exception (such as with 'expect') to indicate failure. If the code completes without throwing any exceptions, the test case has passed.
NOTE: Because failure requires an exception, no assertions after the thrown exception will be run.
Defines a single test case that may execute arbitrary code. doc is a documentation string expression. If the expression is a local, it will be used as is. If the expression is a resolvable symbol, it will be resolved and used as a var. Otherwise, it's evaluated like normal. attr-map (optional) is a metadata map body is any code, which must throw an exception (such as with 'expect') to indicate failure. If the code completes without throwing any exceptions, the test case has passed. NOTE: Because failure requires an exception, no assertions after the thrown exception will be run.
(ok? f)
Calls f with no arguments and discards its return value. Returns true if f does not throw any exceptions. Use when checking an expression that returns a logical false value.
Calls f with no arguments and discards its return value. Returns true if f does not throw any exceptions. Use when checking an expression that returns a logical false value. Useful in [[expect]] or [[expect-it]].
(set-ns-context! contexts)
Add hooks to the namespace suite, instead of to a var or test suite.
contexts
is a sequence of context maps.
Add hooks to the namespace suite, instead of to a var or test suite. `contexts` is a sequence of context maps.
(throws-with-msg? c re f)
Calls f with no arguments; catches exceptions of class c. If the message of the caught exception does not match re (with re-find), throws ExpectationFailed. Any other exception not of class c will be re-thrown. Returns false if f throws no exceptions.
Calls f with no arguments; catches exceptions of class c. If the message of the caught exception does not match re (with re-find), throws ExpectationFailed. Any other exception not of class c will be re-thrown. Returns false if f throws no exceptions. Useful in [[expect]] or [[expect-it]].
(throws? c f)
Calls f with no arguments; returns true if it throws an instance of class c. Any other exception will be re-thrown. Returns false if f throws no exceptions.
Useful in expect-it
or expect
.
Calls f with no arguments; returns true if it throws an instance of class c. Any other exception will be re-thrown. Returns false if f throws no exceptions. Useful in `expect-it` or `expect`.
(update-children child)
If in a context and child is a suite or test-case, append to the context's :children and return nil. Otherwise, return the child.
If in a *context* and child is a suite or test-case, append to the context's :children and return nil. Otherwise, return the child.
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close