ClojureDocs

命名空间

clojure.test

A unit testing 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 

   (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).


   DEFINING TESTS

   There are two ways to define tests.  The "with-test" macro takes
   a defn or def form as its first argument, followed by any number
   of assertions.  The tests will be stored as metadata on the
   definition.

   (with-test
       (defn my-function [x y]
         (+ x y))
     (is (= 4 (my-function 2 2)))
     (is (= 7 (my-function 3 4))))

   As of Clojure SVN rev. 1221, this does not work with defmacro.
   See http://code.google.com/p/clojure/issues/detail?id=51

   The other way lets you define tests separately from the rest of
   your code, even in a different namespace:

   (deftest addition
     (is (= 4 (+ 2 2)))
     (is (= 7 (+ 3 4))))

   (deftest subtraction
     (is (= 1 (- 4 3)))
     (is (= 3 (- 7 4))))

   This creates functions named "addition" and "subtraction", which
   can be called like any other function.  Therefore, tests can be
   grouped and composed, in a style similar to the test framework in
   Peter Seibel's "Practical Common Lisp"
   

   (deftest arithmetic
     (addition)
     (subtraction))

   The names of the nested tests will be joined in a list, like
   "(arithmetic addition)", in failure reports.  You can use nested
   tests to set up a context shared by several tests.


   RUNNING TESTS

   Run tests with the function "(run-tests namespaces...)":

   (run-tests 'your.namespace 'some.other.namespace)

   If you don't specify any namespaces, the current namespace is
   used.  To run all tests in all namespaces, use "(run-all-tests)".

   By default, these functions will search for all tests defined in
   a namespace and run them in an undefined order.  However, if you
   are composing tests, as in the "arithmetic" example above, you
   probably do not want the "addition" and "subtraction" tests run
   separately.  In that case, you must define a special function
   named "test-ns-hook" that runs your tests in the correct order:

   (defn test-ns-hook []
     (arithmetic))

   Note: test-ns-hook prevents execution of fixtures (see below).


   OMITTING TESTS FROM PRODUCTION CODE

   You can bind the variable "*load-tests*" to false when loading or
   compiling code in production.  This will prevent any tests from
   being created by "with-test" or "deftest".


   FIXTURES

   Fixtures allow you to run code before and after tests, to set up
   the context in which tests should be run.

   A fixture is just a function that calls another function passed as
   an argument.  It looks like this:

   (defn my-fixture [f]
      Perform setup, establish bindings, whatever.
     (f)  Then call the function we were passed.
      Tear-down / clean-up code here.
    )

   Fixtures are attached to namespaces in one of two ways.  "each"
   fixtures are run repeatedly, once for each test function created
   with "deftest" or "with-test".  "each" fixtures are useful for
   establishing a consistent before/after state for each test, like
   clearing out database tables.

   "each" fixtures can be attached to the current namespace like this:
   (use-fixtures :each fixture1 fixture2 ...)
   The fixture1, fixture2 are just functions like the example above.
   They can also be anonymous functions, like this:
   (use-fixtures :each (fn [f] setup... (f) cleanup...))

   The other kind of fixture, a "once" fixture, is only run once,
   around ALL the tests in the namespace.  "once" fixtures are useful
   for tasks that only need to be performed once, like establishing
   database connections, or for time-consuming tasks.

   Attach "once" fixtures to the current namespace like this:
   (use-fixtures :once fixture1 fixture2 ...)

   Note: Fixtures and test-ns-hook are mutually incompatible.  If you
   are using test-ns-hook, fixture functions will *never* be run.


   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.

为单元测试 Clojure 代码提供基本工具。

clojure.test 中的变量

*^%

*load-tests*
默认值为 true。如果设置为 false,则 deftest、set-test 或 with-test 不会创建任何测试函数。在编译或加载生产代码时,使用此项来省略测试。
*stack-trace-depth*
在测试期间引发异常时要打印的堆栈跟踪的最大深度。默认为 nil,表示打印完整的堆栈跟踪。
*test-out*
无文档

a

are
使用模板表达式检查多个断言。参阅 clojure.template/do-template 了解有关模板的说明。示例:(are [x y] (= x y) 2 (+ 1 1) 4 (* 2 2)) 扩展为:(do (is (= 2 (+ 1 1))) (is (= 4 (* 2 2)))) 注意:这会破坏某些报告功能,如行号。
assert-any
返回任何测试(包括宏、Java 方法调用或独立符号)的通用断言代码。
assert-expr
无文档
assert-predicate
返回任何函数谓词的通用断言代码。'report' 的 'expected' 参数将包含原始形式,'actual' 参数将包含已计算其所有子形式的形式。如果谓词返回 false,'actual' 形式将包装在 (not...) 中。

c

compose-fixtures
合成两个初始化函数,创建一个将它们的行为结合起来的新初始化函数。

d

deftest
定义一个无参数的测试函数。测试函数可以调用其他测试,因此可以组合测试。如果您组合测试,还应定义名为 test-ns-hook 的函数;run-tests 将调用 test-ns-hook,而不是测试所有变量。注意:实际上,测试主体位于变量的 :test 元数据中,而实际函数(变量的值)调用其自身的 test-var。当 *load-tests* 为 false 时,将忽略 deftest。
deftest-
与 deftest 类似,但创建一个私有变量。
do-report
将文件和行信息添加到测试结果,并调用报告。如果您正在编写自定义 assert-expr 方法,请调用此函数将测试结果传递给报告。

f

file-position
返回一个向量 [filename line-number] 用于调用堆栈中的第 n 个元素。在 1.2 中已弃用:现在测试报告所需的信息位于结果映射中的 :file 和 :line 键中。
函数?
如果参数是函数或会解析为函数(不是宏)的符号,则返回 true。

g

get-possibly-unbound-var
类似于 var-get,但当变量未绑定时则返回 nil。

i

inc-report-counter
增加 *report-counters* 中的命名计数器(对映射的 ref)。如果 *report-counters* 为 nil,则不执行任何操作。
is
通用断言宏。'form' 是任意谓词测试。'msg' 是一个可选消息,可附加到断言中。示例:(is (= 4 (+ 2 2)) "两个加上两个应该等于 4") 特殊形式:(is (thrown? c body)) 检查 c 的实例是从 body 中抛出的,如果没有抛出,则失败;然后返回被抛出对象。(is (thrown-with-msg? c re body)) 检查 c 的实例被抛出并且异常上的信息与正则表达式 re 匹配(使用 re-find)。

j

join-fixtures
按顺序组合一个集合的装置。即使集合为空,也始终返回一个有效的装置函数。

r

report
通用报告函数,可以被重写以插入不同的报告格式(例如,TAP、JUnit)。例如,'is' 等断言调用 'report' 以指示结果。给 'report' 的参数将是一个包含 :type 键的映射。有关 'report' 的参数类型,请参见 test_is.clj 顶部的文档。
run-all-tests
运行所有命名空间中的所有测试;打印结果。可选参数是一个正则表达式;只有名称与正则表达式匹配(使用 re-matches)的命名空间才会被测试。
run-test
运行单个测试。由于目的是运行单个测试,因此不会检查命名空间 test-ns-hook。
run-test-var
运行单个 Var 的测试,在测试周围执行装置,并在之后输出摘要。
run-tests
运行给定命名空间中的所有测试;打印结果。如果未给出,则默认为当前命名空间。返回一个汇总测试结果的映射。

s

set-test
实验性的。将命名 var 的 :test 元数据设置为具有给定正文的 fn。该 var 必须已经存在。不会修改 var 的值。当 *load-tests* 为 false 时,set-test 将被忽略。
successful?
如果给定的测试摘要表示所有测试都成功,则返回 true,否则返回 false。

t

test-all-vars
使用装置调用命名空间中记录的每个 var 上的 test-vars。
test-ns
如果命名空间定义了一个名为 test-ns-hook 的函数,则调用该函数。否则,在命名空间上调用 test-all-vars。'ns' 是一个命名空间对象或一个符号。在内部将 *report-counters* 绑定到初始化为 *initial-report-counters* 的 ref。返回 *report-counters* 的最终解除引用状态。
test-var
如果 v 在其 :test 元数据中有一个函数,则调用该函数,并将 *testing-vars* 绑定到 (conj *testing-vars* v)。
test-vars
按命名空间对 var 进行分组,并使用应用的适当装置对它们运行 test-var。
testing
将一个新的字符串添加到测试环境的列表中。可以嵌套,但必须在测试函数(deftest)中执行。
testing-contexts-str
返回当前测试环境的字符串表示形式。使用空格连接 *testing-contexts* 中的字符串。
testing-vars-str
返回当前测试的字符串表示形式。将 *testing-vars* 中的名称渲染成一个列表,然后渲染当前断言的源文件和代码行。
try-expr
由 'is' 宏用于捕获意外的异常。你不会调用它。

u

use-fixtures
使用装置函数包装测试运行,以执行设置和清理。使用 :each 类型的装置将单独包装每个测试,而 :once 类型的装置将使用单个函数包装整个运行。

w

with-test
取任意定义窗体(返回 Var)作为第一个参数。剩余主体进入 Var 的 :test 元数据函数。当 *load-tests* 为 false,仅评估定义,忽略测试。
with-test-out
使用 *out* 绑定到 *test-out* 的值运行主体。