Clojure.test Cheat Sheet

ADVERTISEMENT

clojure.test cheatsheet
https://clojure.github.io/clojure/clojure.test-api.html
name your test namespace with the
ns form
same name but add -test Don't forget
to convert hyphens to underscores in
(ns your.namespace-test
the filename.
(:require
[your.namespace
:refer :all])
(:require [clojure.test
:refer :all]))
pull in everything from the namespace
you're testing and clojure.test
defining tests
make a new test called
<name>.
(deftest
<name>
put assertions in the
<body>.
<body>)
assert that
<test-expr>
returns
truthy
(not
false
or nil)
assertions
(is <test-expr>)
assert that
<test-expr>
throws an exception of
type
<class>
(is (thrown?
<class>
<test-expr>))
(is (thrown-with-msg?
<class> <regex>
<test-expr>))
assert that
<test-expr>
throws an exception of
adding context
type
<class>
and the message matches
<regex>
(testing
<text>
<body>)
add
<text>
(a string) to the report for all failing assertions in
<body>
running tests at the REPL
user>
(require 'your.namespace-test :reload-all)
load (or reload) your tests
user>
(in-ns 'your.namespace-test)
switch to the test namespace if you need to
your.namespace-test>
(run-tests)
run tests in the current namespace
fixtures
run code around all tests in namespace
(use-fixtures
:once
takes one or more functions. each function takes the tests as argument
(fn [tests]
<before>
the function should define what runs
before
and
after
tests.
are
tests
(tests)
called like a function.
<after>))
run code around each test in namespace
(use-fixtures
:each
(fn [test]
takes one or more functions. each function takes the test as argument
<before>
(test)
the function should define what runs
before
and
after
the test.
test
is
<after>))
called like a function.

ADVERTISEMENT

00 votes

Related Articles

Related forms

Related Categories

Parent category: Education
Go