`
xiuzhuchenqing
  • 浏览: 15810 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Clojure的测试框架 基于测试的开发方式: 好的测试框架有利于驱动项目的开发,当编写代码前写了测试代码,那么就非常有利于开发人员集中精力,着力于当前待解决的

 
阅读更多

Clojure的测试框架

基于测试的开发方式:

好的测试框架有利于驱动项目的开发,当编写代码前写了测试代码,那么就非常有利于开发人员集中精力,着力于当前待解决的问题,加上ClojureREPL环境,提高了开发速度。

<!--[if !supportLists]-->第一章<!--[endif]-->断言

Clojure测试框架中的is宏能够对任意表达式进行断言,也是其最核心的内容。

<!--[if mso & !supportInlineShapes & supportFields]><span style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK36'><span style='mso-bookmark:OLE_LINK1'><span style='mso-bookmark:OLE_LINK2'></span></span></span></span><span style='mso-element:field-begin;mso-field-lock:yes'></span><span style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK36'><span style='mso-bookmark:OLE_LINK1'><span style='mso-bookmark:OLE_LINK2'><span lang=EN-US style='font-size:9.0pt'><span style='mso-spacerun:yes'>&nbsp;</span>SHAPE <span style='mso-spacerun:yes'>&nbsp;</span>\* MERGEFORMAT <span style='mso-element: field-separator'></span></span></span></span></span></span><![endif]--><!--[if gte vml 1]><v:rect id="_x0000_s1040" style='width:177.95pt;height:60.3pt;flip:x;mso-wrap-style:none; mso-wrap-distance-top:7.2pt;mso-wrap-distance-bottom:36pt; mso-position-horizontal-relative:char;mso-position-vertical-relative:line' o:allowincell="f" fillcolor="white [3201]" strokecolor="#c2d69b [1942]" strokeweight="1pt"> <v:fill color2="#d6e3bc [1302]" focusposition="1" focussize="" focus="100%" type="gradient" /> <v:stroke dashstyle="1 1" endcap="round" /> <v:shadow on="t" type="perspective" color="#4e6128 [1606]" opacity=".5" offset="1pt" offset2="-3pt" /> <v:textbox style='mso-next-textbox:#_x0000_s1040;mso-fit-shape-to-text:t' inset="2.5mm,2.5mm,2.5mm,7.2pt"> <![if !mso]> <table cellpadding=0 cellspacing=0 width="100%"> <tr> <td><![endif]> <div> <p class=MsoNormal align=left style='text-align:left;text-indent:0cm; mso-char-indent-count:0;mso-layout-grid-align:none;text-autospace:none'><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CC7A7A; mso-font-kerning:0pt'>(</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#7F0055;mso-font-kerning:0pt'>is</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black; mso-font-kerning:0pt'> </span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#CCB07A;mso-font-kerning:0pt'>(</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#7F0055; mso-font-kerning:0pt'>=</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:black;mso-font-kerning:0pt'> 4 </span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#7ACC7A; mso-font-kerning:0pt'>(</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#7F0055;mso-font-kerning:0pt'>+</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black; mso-font-kerning:0pt'> 2 2</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#7ACC7A;mso-font-kerning:0pt'>)</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CCB07A; mso-font-kerning:0pt'>)</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#CC7A7A;mso-font-kerning:0pt'>)</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;mso-font-kerning: 0pt'><o:p></o:p></span></p> <p class=MsoNormal align=left style='text-align:left;text-indent:0cm; mso-char-indent-count:0;mso-layout-grid-align:none;text-autospace:none'><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CC7A7A; mso-font-kerning:0pt'>(</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#7F0055;mso-font-kerning:0pt'>is</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black; mso-font-kerning:0pt'> </span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#CCB07A;mso-font-kerning:0pt'>(</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#7F0055; mso-font-kerning:0pt'>instance?</span><span lang=EN-US style='font-size: 10.0pt;font-family:Consolas;color:black;mso-font-kerning:0pt'> <i>Integer</i> 256</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas; color:#CCB07A;mso-font-kerning:0pt'>)</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CC7A7A;mso-font-kerning: 0pt'>)</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas; mso-font-kerning:0pt'><o:p></o:p></span></p> <p class=MsoNormal align=left style='text-align:left;text-indent:0cm; mso-char-indent-count:0'><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#CC7A7A;mso-font-kerning:0pt'>(</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#7F0055; mso-font-kerning:0pt'>is</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:black;mso-font-kerning:0pt'> </span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CCB07A; mso-font-kerning:0pt'>(</span><i><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#7F0055;mso-font-kerning:0pt'>.startsWith</span></i><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black; mso-font-kerning:0pt'> </span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#2A00FF;mso-font-kerning:0pt'>&quot;abcde&quot;</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black; mso-font-kerning:0pt'> </span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#2A00FF;mso-font-kerning:0pt'>&quot;ab&quot;</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CCB07A; mso-font-kerning:0pt'>)</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#CC7A7A;mso-font-kerning:0pt'>)</span><i><span lang=EN-US style='font-size:14.0pt;color:#D3DFEE;mso-themecolor:accent1; mso-themetint:63'><o:p></o:p></span></i></p> </div> <![if !mso]></td> </tr> </table> <![endif]></v:textbox> <w:wrap type="none" anchorx="page" anchory="page" /> <w:anchorlock /> </v:rect><![endif]--><!--[if !vml]-->文本框: (is (= 4 (+ 2 2)))
(is (instance? Integer 256))
(is (.startsWith "abcde" "ab"))
<!--[endif]--><!--[if gte vml 1]><v:shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"> <v:stroke joinstyle="miter" /> <v:formulas> <v:f eqn="if lineDrawn pixelLineWidth 0" /> <v:f eqn="sum @0 1 0" /> <v:f eqn="sum 0 0 @1" /> <v:f eqn="prod @2 1 2" /> <v:f eqn="prod @3 21600 pixelWidth" /> <v:f eqn="prod @3 21600 pixelHeight" /> <v:f eqn="sum @0 0 1" /> <v:f eqn="prod @6 1 2" /> <v:f eqn="prod @7 21600 pixelWidth" /> <v:f eqn="sum @8 21600 0" /> <v:f eqn="prod @7 21600 pixelHeight" /> <v:f eqn="sum @10 21600 0" /> </v:formulas> <v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect" /> <o:lock v:ext="edit" aspectratio="t" /> </v:shapetype><![endif]--><!--[if mso & !supportInlineShapes & supportFields]><span style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK36'><span style='mso-bookmark:OLE_LINK1'><span style='mso-bookmark:OLE_LINK2'></span></span></span></span><v:shape id="_x0000_i1025" type="#_x0000_t75" style='width:177.95pt;height:60.3pt'> <v:imagedata croptop="-65520f" cropbottom="65520f" /> </v:shape><span style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK36'><span style='mso-bookmark:OLE_LINK1'><span style='mso-bookmark:OLE_LINK2'><span lang=EN-US style='font-size:9.0pt'><span style='mso-element:field-end'></span></span></span></span></span></span><![endif]-->

is宏能够直接使用在REPL中,如果断言失败会打印出详细的信息。

<!--[if mso & !supportInlineShapes & supportFields]><span style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK36'></span></span><span style='mso-element:field-begin;mso-field-lock:yes'></span><span style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK36'><span lang=EN-US style='font-size:9.0pt'><span style='mso-spacerun:yes'>&nbsp;</span>SHAPE <span style='mso-spacerun:yes'>&nbsp;</span>\* MERGEFORMAT <span style='mso-element: field-separator'></span></span></span></span><![endif]--><!--[if gte vml 1]><v:rect id="_x0000_s1039" style='width:361.7pt;height:105.3pt;flip:x;mso-wrap-style:none; mso-wrap-distance-top:7.2pt;mso-wrap-distance-bottom:36pt; mso-position-horizontal-relative:char;mso-position-vertical-relative:line' o:allowincell="f" fillcolor="white [3201]" strokecolor="#c2d69b [1942]" strokeweight="1pt"> <v:fill color2="#d6e3bc [1302]" focusposition="1" focussize="" focus="100%" type="gradient" /> <v:stroke dashstyle="1 1" endcap="round" /> <v:shadow on="t" type="perspective" color="#4e6128 [1606]" opacity=".5" offset="1pt" offset2="-3pt" /> <v:textbox style='mso-next-textbox:#_x0000_s1039;mso-fit-shape-to-text:t' inset="2.5mm,2.5mm,2.5mm,7.2pt"> <![if !mso]> <table cellpadding=0 cellspacing=0 width="100%"> <tr> <td><![endif]> <div> <p class=MsoNormal align=left style='text-align:left;text-indent:.2pt; mso-char-indent-count:0;mso-layout-grid-align:none;text-autospace:none'><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;background:white; mso-highlight:white;mso-font-kerning:0pt'>=&gt; <span style='color:#CC7A7A'>(</span><span style='color:#7F0055'>is</span> <span style='color:#CCB07A'>(</span><span style='color:#7F0055'>=</span> 5 <span style='color:#7ACC7A'>(</span><span style='color:#7F0055'>+</span> 2 2<span style='color:#7ACC7A'>)</span><span style='color:#CCB07A'>)</span><span style='color:#CC7A7A'>)</span></span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;mso-font-kerning: 0pt'><o:p></o:p></span></p> <p class=MsoNormal align=left style='text-align:left;text-indent:.2pt; mso-char-indent-count:0;mso-layout-grid-align:none;text-autospace:none'><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;mso-font-kerning: 0pt'><o:p>&nbsp;</o:p></span></p> <p class=MsoNormal align=left style='text-align:left;text-indent:.2pt; mso-char-indent-count:0;mso-layout-grid-align:none;text-autospace:none'><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;mso-font-kerning: 0pt'>FAIL in clojure.lang.PersistentList$EmptyList@1 (NO_SOURCE_FILE:1)<o:p></o:p></span></p> <p class=MsoNormal align=left style='text-align:left;text-indent:.2pt; mso-char-indent-count:0;mso-layout-grid-align:none;text-autospace:none'><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;mso-font-kerning: 0pt'>expected: (= 5 (+ 2 2))<o:p></o:p></span></p> <p class=MsoNormal align=left style='text-align:left;text-indent:.2pt; mso-char-indent-count:0;mso-layout-grid-align:none;text-autospace:none'><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;mso-font-kerning: 0pt'><span style='mso-spacerun:yes'>&nbsp; </span>actual: (not (= 5 4))<o:p></o:p></span></p> <p class=MsoNormal style='text-indent:20.0pt'><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:green;mso-font-kerning: 0pt'>false</span><span lang=EN-US style='mso-bidi-font-size:14.0pt'><o:p></o:p></span></p> </div> <![if !mso]></td> </tr> </table> <![endif]></v:textbox> <w:wrap type="none" anchorx="page" anchory="page" /> <w:anchorlock /> </v:rect><![endif]--><!--[if !vml]-->文本框: => (is (= 5 (+ 2 2)))

FAIL in clojure.lang.PersistentList$EmptyList@1 (NO_SOURCE_FILE:1)
expected: (= 5 (+ 2 2))
  actual: (not (= 5 4))
false
<!--[endif]--><!--[if mso & !supportInlineShapes & supportFields]><span style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK36'></span></span><v:shape id="_x0000_i1026" type="#_x0000_t75" style='width:361.7pt;height:105.3pt'> <v:imagedata croptop="-65520f" cropbottom="65520f" /> </v:shape><span style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK36'><span lang=EN-US style='font-size:9.0pt'><span style='mso-element:field-end'></span></span></span></span><![endif]-->

is宏有两个特殊的断言形式,用以测试异常。
<!--[if !supportLists]-->*  <!--[endif]-->"(is (thrown? c ...))"形式测试是否有一个类c的异常抛出。例如:
<!--[if mso & !supportInlineShapes & supportFields]><span
style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK36'><span
style='mso-bookmark:OLE_LINK5'><span style='mso-bookmark:OLE_LINK6'></span></span></span></span><span
style='mso-element:field-begin;mso-field-lock:yes'></span><span
style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK36'><span
style='mso-bookmark:OLE_LINK5'><span style='mso-bookmark:OLE_LINK6'><span
lang=EN-US style='font-size:9.0pt'> SHAPE <span style='mso-spacerun:yes'> </span>\* MERGEFORMAT <span
style='mso-element:field-separator'></span></span></span></span></span></span><![endif]--><!--[if gte vml 1]><v:rect id="_x0000_s1038"
 style='width:235.7pt;height:30.3pt;flip:x;mso-wrap-style:none;
 mso-wrap-distance-top:7.2pt;mso-wrap-distance-bottom:36pt;
 mso-position-horizontal-relative:char;mso-position-vertical-relative:line'
 o:allowincell="f" fillcolor="white [3201]" strokecolor="#c2d69b [1942]"
 strokeweight="1pt">
 <v:fill color2="#d6e3bc [1302]" focusposition="1" focussize="" focus="100%"
  type="gradient" />
 <v:stroke dashstyle="1 1" endcap="round" />
 <v:shadow on="t" type="perspective" color="#4e6128 [1606]" opacity=".5"
  offset="1pt" offset2="-3pt" />
 <v:textbox style='mso-next-textbox:#_x0000_s1038;mso-fit-shape-to-text:t'
  inset="2.5mm,2.5mm,2.5mm,7.2pt">
  <![if !mso]>
  <table cellpadding=0 cellspacing=0 width="100%">
   <tr>
    <td><![endif]>
    <div>
    <p class=MsoNormal style='text-indent:0cm;mso-char-indent-count:0'><span
    lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CC7A7A;
    mso-font-kerning:0pt'>(</span><span lang=EN-US style='font-size:10.0pt;
    font-family:Consolas;color:#7F0055;mso-font-kerning:0pt'>is</span><span
    lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black;
    mso-font-kerning:0pt'> </span><span lang=EN-US style='font-size:10.0pt;
    font-family:Consolas;color:#CCB07A;mso-font-kerning:0pt'>(</span><span
    lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#7F0055;
    mso-font-kerning:0pt'>thrown?</span><span lang=EN-US style='font-size:10.0pt;
    font-family:Consolas;color:black;mso-font-kerning:0pt'> <i>ArithmeticException</i> </span><span
    lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#7ACC7A;
    mso-font-kerning:0pt'>(</span><span lang=EN-US style='font-size:10.0pt;
    font-family:Consolas;color:#7F0055;mso-font-kerning:0pt'>/</span><span
    lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black;
    mso-font-kerning:0pt'> 1 0</span><span lang=EN-US style='font-size:10.0pt;
    font-family:Consolas;color:#7ACC7A;mso-font-kerning:0pt'>)</span><span
    lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CCB07A;
    mso-font-kerning:0pt'>)</span><span lang=EN-US style='font-size:10.0pt;
    font-family:Consolas;color:#CC7A7A;mso-font-kerning:0pt'>)</span><span
    lang=EN-US style='mso-bidi-font-size:14.0pt'><o:p></o:p></span></p>
    </div>
    <![if !mso]></td>
   </tr>
  </table>
  <![endif]></v:textbox>
 <w:wrap type="none" anchorx="page" anchory="page" />
 <w:anchorlock />
</v:rect><![endif]--><!--[if !vml]-->文本框: (is (thrown? ArithmeticException (/ 1 0)))<!--[endif]--><!--[if mso & !supportInlineShapes & supportFields]><span
style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK36'><span
style='mso-bookmark:OLE_LINK5'><span style='mso-bookmark:OLE_LINK6'></span></span></span></span><v:shape
 id="_x0000_i1028" type="#_x0000_t75" style='width:235.7pt;height:30.3pt'>
 <v:imagedata croptop="-65520f" cropbottom="65520f" />
</v:shape><span style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK36'><span
style='mso-bookmark:OLE_LINK5'><span style='mso-bookmark:OLE_LINK6'><span
lang=EN-US style='font-size:9.0pt'><span style='mso-element:field-end'></span></span></span></span></span></span><![endif]-->
<!--[if !supportLists]-->*  <!--[endif]-->"(is (thrown-with-msg? c re ...))"做着同样的事情,不同的是会检测异常消息是否和正则表达式re匹配。例如:
<!--[if mso & !supportInlineShapes & supportFields]><span
style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK36'></span></span><span
style='mso-element:field-begin;mso-field-lock:yes'></span><span
style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK36'><span
lang=EN-US style='font-size:9.0pt'> SHAPE <span style='mso-spacerun:yes'> </span>\* MERGEFORMAT <span
style='mso-element:field-separator'></span></span></span></span><![endif]--><!--[if gte vml 1]><v:rect id="_x0000_s1037"
 style='width:324.95pt;height:45.3pt;flip:x;mso-wrap-style:none;
 mso-wrap-distance-top:7.2pt;mso-wrap-distance-bottom:36pt;
 mso-position-horizontal-relative:char;mso-position-vertical-relative:line'
 o:allowincell="f" fillcolor="white [3201]" strokecolor="#c2d69b [1942]"
 strokeweight="1pt">
 <v:fill color2="#d6e3bc [1302]" focusposition="1" focussize="" focus="100%"
  type="gradient" />
 <v:stroke dashstyle="1 1" endcap="round" />
 <v:shadow on="t" type="perspective" color="#4e6128 [1606]" opacity=".5"
  offset="1pt" offset2="-3pt" />
 <v:textbox style='mso-next-textbox:#_x0000_s1037;mso-fit-shape-to-text:t'
  inset="2.5mm,2.5mm,2.5mm,7.2pt">
  <![if !mso]>
  <table cellpadding=0 cellspacing=0 width="100%">
   <tr>
    <td><![endif]>
    <div>
    <p class=MsoNormal align=left style='text-align:left;text-indent:0cm;
    mso-char-indent-count:0;mso-layout-grid-align:none;text-autospace:none'><span
    lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CC7A7A;
    mso-font-kerning:0pt'>(</span><span lang=EN-US style='font-size:10.0pt;
    font-family:Consolas;color:#7F0055;mso-font-kerning:0pt'>is</span><span
    lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black;
    mso-font-kerning:0pt'> </span><span lang=EN-US style='font-size:10.0pt;
    font-family:Consolas;color:#CCB07A;mso-font-kerning:0pt'>(</span><span
    lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#7F0055;
    mso-font-kerning:0pt'>thrown-with-msg?</span><span lang=EN-US
    style='font-size:10.0pt;font-family:Consolas;color:black;mso-font-kerning:
    0pt'> <i>ArithmeticException</i> </span><span lang=EN-US style='font-size:
    10.0pt;font-family:Consolas;color:#2A00FF;mso-font-kerning:0pt'>#&quot;Divide by zero&quot;</span><span
    lang=EN-US style='font-size:10.0pt;font-family:Consolas;mso-font-kerning:
    0pt'><o:p></o:p></span></p>
    <p class=MsoNormal style='text-indent:20.0pt'><span lang=EN-US
    style='font-size:10.0pt;font-family:Consolas;color:black;mso-font-kerning:
    0pt'><span style='mso-spacerun:yes'>                      </span></span><span
    lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#7ACC7A;
    mso-font-kerning:0pt'>(</span><span lang=EN-US style='font-size:10.0pt;
    font-family:Consolas;color:#7F0055;mso-font-kerning:0pt'>/</span><span
    lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black;
    mso-font-kerning:0pt'> 1 0</span><span lang=EN-US style='font-size:10.0pt;
    font-family:Consolas;color:#7ACC7A;mso-font-kerning:0pt'>)</span><span
    lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CCB07A;
    mso-font-kerning:0pt'>)</span><span lang=EN-US style='font-size:10.0pt;
    font-family:Consolas;color:#CC7A7A;mso-font-kerning:0pt'>)</span><span
    lang=EN-US style='mso-bidi-font-size:14.0pt'><o:p></o:p></span></p>
    </div>
    <![if !mso]></td>
   </tr>
  </table>
  <![endif]></v:textbox>
 <w:wrap type="none" anchorx="page" anchory="page" />
 <w:anchorlock />
</v:rect><![endif]--><!--[if !vml]-->文本框: (is (thrown-with-msg? ArithmeticException #"Divide by zero"
                      (/ 1 0)))
<!--[endif]--><!--[if mso & !supportInlineShapes & supportFields]><span
style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK36'></span></span><v:shape
 id="_x0000_i1027" type="#_x0000_t75" style='width:324.95pt;height:45.3pt'>
 <v:imagedata croptop="-65520f" cropbottom="65520f" />
</v:shape><span style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK36'><span
lang=EN-US style='font-size:9.0pt'><span style='mso-element:field-end'></span></span></span></span><![endif]-->

<!--[if !supportLists]-->第二章<!--[endif]-->给测试单元添加说明文档

clojure的测试框架中允许开发人员给测试单元添加相应的说明文档,is宏便支持一个可选的额外参数,用于描述断言。这些额外参数在断言测试失败时将作为消息包含在错误报告中。

<!--[if mso & !supportInlineShapes & supportFields]><span style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK36'></span></span><span style='mso-element:field-begin;mso-field-lock:yes'></span><span style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK36'><span lang=EN-US style='font-size:9.0pt'><span style='mso-spacerun:yes'>&nbsp;</span>SHAPE <span style='mso-spacerun:yes'>&nbsp;</span>\* MERGEFORMAT <span style='mso-element: field-separator'></span></span></span></span><![endif]--><!--[if gte vml 1]><v:rect id="_x0000_s1036" style='width:366.95pt;height:105.3pt;flip:x;mso-wrap-style:none; mso-wrap-distance-top:7.2pt;mso-wrap-distance-bottom:36pt; mso-position-horizontal-relative:char;mso-position-vertical-relative:line' o:allowincell="f" fillcolor="white [3201]" strokecolor="#c2d69b [1942]" strokeweight="1pt"> <v:fill color2="#d6e3bc [1302]" focusposition="1" focussize="" focus="100%" type="gradient" /> <v:stroke dashstyle="1 1" endcap="round" /> <v:shadow on="t" type="perspective" color="#4e6128 [1606]" opacity=".5" offset="1pt" offset2="-3pt" /> <v:textbox style='mso-next-textbox:#_x0000_s1036;mso-fit-shape-to-text:t' inset="2.5mm,2.5mm,2.5mm,7.2pt"> <![if !mso]> <table cellpadding=0 cellspacing=0 width="100%"> <tr> <td><![endif]> <div> <p class=MsoNormal style='text-indent:0cm;mso-char-indent-count:0'><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CC7A7A; mso-font-kerning:0pt;mso-no-proof:yes'><v:shape id="图片_x0020_22" o:spid="_x0000_i1041" type="#_x0000_t75" style='width:351.75pt;height:78.75pt;visibility:visible; mso-wrap-style:square'> <v:imagedata src="file:///C:\Users\ADMINI~1\AppData\Local\Temp\msohtmlclip1\01\clip_image006.png" o:title="" /> </v:shape></span><span lang=EN-US style='mso-bidi-font-size:14.0pt'><o:p></o:p></span></p> </div> <![if !mso]></td> </tr> </table> <![endif]></v:textbox> <w:wrap type="none" anchorx="page" anchory="page" /> <w:anchorlock /> </v:rect><![endif]--><!--[if !vml]-->文本框:  <!--[endif]--><!--[if mso & !supportInlineShapes & supportFields]><span style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK36'></span></span><v:shape id="_x0000_i1029" type="#_x0000_t75" style='width:366.95pt;height:105.3pt'> <v:imagedata croptop="-65520f" cropbottom="65520f" /> </v:shape><span style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK36'><span lang=EN-US style='font-size:9.0pt'><span style='mso-element:field-end'></span></span></span></span><![endif]-->

testing可以对单元测试进行分组,还可以为各个分组添加说明文档,如下:

<!--[if mso & !supportInlineShapes & supportFields]><span style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK36'><span style='mso-bookmark:OLE_LINK7'><span style='mso-bookmark:OLE_LINK8'></span></span></span></span><span style='mso-element:field-begin;mso-field-lock:yes'></span><span style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK36'><span style='mso-bookmark:OLE_LINK7'><span style='mso-bookmark:OLE_LINK8'><span lang=EN-US style='font-size:9.0pt'><span style='mso-spacerun:yes'>&nbsp;</span>SHAPE <span style='mso-spacerun:yes'>&nbsp;</span>\* MERGEFORMAT <span style='mso-element: field-separator'></span></span></span></span></span></span><![endif]--><!--[if gte vml 1]><v:rect id="_x0000_s1035" style='width:271.7pt;height:240.3pt;flip:x;mso-wrap-style:none; mso-wrap-distance-top:7.2pt;mso-wrap-distance-bottom:36pt; mso-position-horizontal-relative:char;mso-position-vertical-relative:line' o:allowincell="f" fillcolor="white [3201]" strokecolor="#c2d69b [1942]" strokeweight="1pt"> <v:fill color2="#d6e3bc [1302]" focusposition="1" focussize="" focus="100%" type="gradient" /> <v:stroke dashstyle="1 1" endcap="round" /> <v:shadow on="t" type="perspective" color="#4e6128 [1606]" opacity=".5" offset="1pt" offset2="-3pt" /> <v:textbox style='mso-next-textbox:#_x0000_s1035;mso-fit-shape-to-text:t' inset="2.5mm,2.5mm,2.5mm,7.2pt"> <![if !mso]> <table cellpadding=0 cellspacing=0 width="100%"> <tr> <td><![endif]> <div> <p class=MsoNormal style='text-indent:0cm;mso-char-indent-count:0'><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CC7A7A; mso-font-kerning:0pt;mso-no-proof:yes'><v:shape id="图片_x0020_26" o:spid="_x0000_i1040" type="#_x0000_t75" style='width:256.5pt;height:225pt;visibility:visible; mso-wrap-style:square'> <v:imagedata src="file:///C:\Users\ADMINI~1\AppData\Local\Temp\msohtmlclip1\01\clip_image008.png" o:title="" /> </v:shape></span><span lang=EN-US style='mso-bidi-font-size:14.0pt'><o:p></o:p></span></p> </div> <![if !mso]></td> </tr> </table> <![endif]></v:textbox> <w:wrap type="none" anchorx="page" anchory="page" /> <w:anchorlock /> </v:rect><![endif]--><!--[if !vml]-->文本框:  <!--[endif]--><!--[if mso & !supportInlineShapes & supportFields]><span style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK36'><span style='mso-bookmark:OLE_LINK7'><span style='mso-bookmark:OLE_LINK8'></span></span></span></span><v:shape id="_x0000_i1030" type="#_x0000_t75" style='width:271.7pt;height:240.3pt'> <v:imagedata croptop="-65520f" cropbottom="65520f" /> </v:shape><span style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK36'><span style='mso-bookmark:OLE_LINK7'><span style='mso-bookmark:OLE_LINK8'><span lang=EN-US style='font-size:9.0pt'><span style='mso-element:field-end'></span></span></span></span></span></span><![endif]-->

<!--[if !supportLists]-->第三章<!--[endif]-->定义单元测试

有两种定义测试的方式:with-testdeftest

<!--[if !supportLists]-->ü  <!--[endif]-->with-test接受一个defn或者def定义作为其首参,后跟任意数量的断言表达式。测试部分将作为defn或者def定义的元数据。

<!--[if mso & !supportInlineShapes & supportFields]><span style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK36'><span style='mso-bookmark:OLE_LINK9'><span style='mso-bookmark:OLE_LINK10'></span></span></span></span><span style='mso-element:field-begin;mso-field-lock:yes'></span><span style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK36'><span style='mso-bookmark:OLE_LINK9'><span style='mso-bookmark:OLE_LINK10'><span lang=EN-US style='font-size:9.0pt'><span style='mso-spacerun:yes'>&nbsp;</span>SHAPE <span style='mso-spacerun:yes'>&nbsp;</span>\* MERGEFORMAT <span style='mso-element: field-separator'></span></span></span></span></span></span><![endif]--><!--[if gte vml 1]><v:rect id="_x0000_s1034" style='width:176.45pt;height:90.3pt;flip:x;mso-wrap-style:none; mso-wrap-distance-top:7.2pt;mso-wrap-distance-bottom:36pt; mso-position-horizontal-relative:char;mso-position-vertical-relative:line' o:allowincell="f" fillcolor="white [3201]" strokecolor="#c2d69b [1942]" strokeweight="1pt"> <v:fill color2="#d6e3bc [1302]" focusposition="1" focussize="" focus="100%" type="gradient" /> <v:stroke dashstyle="1 1" endcap="round" /> <v:shadow on="t" type="perspective" color="#4e6128 [1606]" opacity=".5" offset="1pt" offset2="-3pt" /> <v:textbox style='mso-next-textbox:#_x0000_s1034;mso-fit-shape-to-text:t' inset="2.5mm,2.5mm,2.5mm,7.2pt"> <![if !mso]> <table cellpadding=0 cellspacing=0 width="100%"> <tr> <td><![endif]> <div> <p class=MsoNormal align=left style='text-align:left;text-indent:0cm; mso-char-indent-count:0;mso-layout-grid-align:none;text-autospace:none'><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CC7A7A; mso-font-kerning:0pt'>(</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#7F0055;mso-font-kerning:0pt'>with-test</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;mso-font-kerning: 0pt'><o:p></o:p></span></p> <p class=MsoNormal align=left style='text-align:left;text-indent:0cm; mso-char-indent-count:0;mso-layout-grid-align:none;text-autospace:none'><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black; mso-font-kerning:0pt'><span style='mso-spacerun:yes'>&nbsp; </span></span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CCB07A; mso-font-kerning:0pt'>(</span><b><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#7F0055;mso-font-kerning:0pt'>defn</span></b><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black; mso-font-kerning:0pt'> my-function [x y]</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;mso-font-kerning:0pt'><o:p></o:p></span></p> <p class=MsoNormal align=left style='text-align:left;text-indent:0cm; mso-char-indent-count:0;mso-layout-grid-align:none;text-autospace:none'><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black; mso-font-kerning:0pt'><span style='mso-spacerun:yes'>&nbsp;&nbsp;&nbsp; </span></span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#7ACC7A; mso-font-kerning:0pt'>(</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#7F0055;mso-font-kerning:0pt'>+</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black; mso-font-kerning:0pt'> x y</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#7ACC7A;mso-font-kerning:0pt'>)</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CCB07A; mso-font-kerning:0pt'>)</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;mso-font-kerning:0pt'><o:p></o:p></span></p> <p class=MsoNormal align=left style='text-align:left;text-indent:0cm; mso-char-indent-count:0;mso-layout-grid-align:none;text-autospace:none'><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black; mso-font-kerning:0pt'><span style='mso-spacerun:yes'>&nbsp; </span></span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CCB07A; mso-font-kerning:0pt'>(</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#7F0055;mso-font-kerning:0pt'>is</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black; mso-font-kerning:0pt'> </span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#7ACC7A;mso-font-kerning:0pt'>(</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#7F0055; mso-font-kerning:0pt'>=</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:black;mso-font-kerning:0pt'> 4 </span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#7ACCB0; mso-font-kerning:0pt'>(</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#7F0055;mso-font-kerning:0pt'>my-function</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black; mso-font-kerning:0pt'> 2 2</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#7ACCB0;mso-font-kerning:0pt'>)</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#7ACC7A; mso-font-kerning:0pt'>)</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#CCB07A;mso-font-kerning:0pt'>)</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;mso-font-kerning: 0pt'><o:p></o:p></span></p> <p class=MsoNormal style='text-indent:0cm;mso-char-indent-count:0'><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black; mso-font-kerning:0pt'><span style='mso-spacerun:yes'>&nbsp; </span></span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CCB07A; mso-font-kerning:0pt'>(</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#7F0055;mso-font-kerning:0pt'>is</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black; mso-font-kerning:0pt'> </span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#7ACC7A;mso-font-kerning:0pt'>(</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#7F0055; mso-font-kerning:0pt'>=</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:black;mso-font-kerning:0pt'> 7 </span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#7ACCB0; mso-font-kerning:0pt'>(</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#7F0055;mso-font-kerning:0pt'>my-function</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black; mso-font-kerning:0pt'> 3 4</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#7ACCB0;mso-font-kerning:0pt'>)</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#7ACC7A; mso-font-kerning:0pt'>)</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#CCB07A;mso-font-kerning:0pt'>)</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CC7A7A; mso-font-kerning:0pt'>)</span><span lang=EN-US style='mso-bidi-font-size: 14.0pt'><o:p></o:p></span></p> </div> <![if !mso]></td> </tr> </table> <![endif]></v:textbox> <w:wrap type="none" anchorx="page" anchory="page" /> <w:anchorlock /> </v:rect><![endif]--><!--[if !vml]-->文本框: (with-test
  (defn my-function [x y]
    (+ x y))
  (is (= 4 (my-function 2 2)))
  (is (= 7 (my-function 3 4))))
<!--[endif]--><!--[if mso & !supportInlineShapes & supportFields]><span style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK36'><span style='mso-bookmark:OLE_LINK9'><span style='mso-bookmark:OLE_LINK10'></span></span></span></span><v:shape id="_x0000_i1031" type="#_x0000_t75" style='width:176.45pt;height:90.3pt'> <v:imagedata croptop="-65520f" cropbottom="65520f" /> </v:shape><span style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK36'><span style='mso-bookmark:OLE_LINK9'><span style='mso-bookmark:OLE_LINK10'><span lang=EN-US style='font-size:9.0pt'><span style='mso-element:field-end'></span></span></span></span></span></span><![endif]-->

<!--[if !supportLists]-->ü  <!--[endif]-->deftest方式所定义单元测试是和我们的其他代码分离的,即使是在不同命名空间中。

<!--[if mso & !supportInlineShapes & supportFields]><span style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK36'><span style='mso-bookmark:OLE_LINK11'><span style='mso-bookmark:OLE_LINK12'></span></span></span></span><span style='mso-element:field-begin;mso-field-lock:yes'></span><span style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK36'><span style='mso-bookmark:OLE_LINK11'><span style='mso-bookmark:OLE_LINK12'><span lang=EN-US style='font-size:9.0pt'><span style='mso-spacerun:yes'>&nbsp;</span>SHAPE <span style='mso-spacerun:yes'>&nbsp;</span>\* MERGEFORMAT <span style='mso-element: field-separator'></span></span></span></span></span></span><![endif]--><!--[if gte vml 1]><v:rect id="_x0000_s1033" style='width:144.2pt;height:120.3pt;flip:x;mso-wrap-style:none; mso-wrap-distance-top:7.2pt;mso-wrap-distance-bottom:36pt; mso-position-horizontal-relative:char;mso-position-vertical-relative:line' o:allowincell="f" fillcolor="white [3201]" strokecolor="#c2d69b [1942]" strokeweight="1pt"> <v:fill color2="#d6e3bc [1302]" focusposition="1" focussize="" focus="100%" type="gradient" /> <v:stroke dashstyle="1 1" endcap="round" /> <v:shadow on="t" type="perspective" color="#4e6128 [1606]" opacity=".5" offset="1pt" offset2="-3pt" /> <v:textbox style='mso-next-textbox:#_x0000_s1033;mso-fit-shape-to-text:t' inset="2.5mm,2.5mm,2.5mm,7.2pt"> <![if !mso]> <table cellpadding=0 cellspacing=0 width="100%"> <tr> <td><![endif]> <div> <p class=MsoNormal align=left style='text-align:left;text-indent:0cm; mso-char-indent-count:0;mso-layout-grid-align:none;text-autospace:none'><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CC7A7A; mso-font-kerning:0pt'>(</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#7F0055;mso-font-kerning:0pt'>deftest</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black; mso-font-kerning:0pt'> addition</span><span lang=EN-US style='font-size: 10.0pt;font-family:Consolas;mso-font-kerning:0pt'><o:p></o:p></span></p> <p class=MsoNormal align=left style='text-align:left;text-indent:0cm; mso-char-indent-count:0;mso-layout-grid-align:none;text-autospace:none'><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black; mso-font-kerning:0pt'><span style='mso-spacerun:yes'>&nbsp; </span></span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CCB07A; mso-font-kerning:0pt'>(</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#7F0055;mso-font-kerning:0pt'>is</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black; mso-font-kerning:0pt'> </span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#7ACC7A;mso-font-kerning:0pt'>(</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#7F0055; mso-font-kerning:0pt'>=</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:black;mso-font-kerning:0pt'> 4 </span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#7ACCB0; mso-font-kerning:0pt'>(</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#7F0055;mso-font-kerning:0pt'>+</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black; mso-font-kerning:0pt'> 2 2</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#7ACCB0;mso-font-kerning:0pt'>)</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#7ACC7A; mso-font-kerning:0pt'>)</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#CCB07A;mso-font-kerning:0pt'>)</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;mso-font-kerning: 0pt'><o:p></o:p></span></p> <p class=MsoNormal align=left style='text-align:left;text-indent:0cm; mso-char-indent-count:0;mso-layout-grid-align:none;text-autospace:none'><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black; mso-font-kerning:0pt'><span style='mso-spacerun:yes'>&nbsp; </span></span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CCB07A; mso-font-kerning:0pt'>(</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#7F0055;mso-font-kerning:0pt'>is</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black; mso-font-kerning:0pt'> </span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#7ACC7A;mso-font-kerning:0pt'>(</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#7F0055; mso-font-kerning:0pt'>=</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:black;mso-font-kerning:0pt'> 7 </span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#7ACCB0; mso-font-kerning:0pt'>(</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#7F0055;mso-font-kerning:0pt'>+</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black; mso-font-kerning:0pt'> 3 4</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#7ACCB0;mso-font-kerning:0pt'>)</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#7ACC7A; mso-font-kerning:0pt'>)</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#CCB07A;mso-font-kerning:0pt'>)</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CC7A7A; mso-font-kerning:0pt'>)</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;mso-font-kerning:0pt'><o:p></o:p></span></p> <p class=MsoNormal align=left style='text-align:left;text-indent:0cm; mso-char-indent-count:0;mso-layout-grid-align:none;text-autospace:none'><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;mso-font-kerning: 0pt'><o:p>&nbsp;</o:p></span></p> <p class=MsoNormal align=left style='text-align:left;text-indent:0cm; mso-char-indent-count:0;mso-layout-grid-align:none;text-autospace:none'><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CC7A7A; mso-font-kerning:0pt'>(</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#7F0055;mso-font-kerning:0pt'>deftest</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black; mso-font-kerning:0pt'> subtraction</span><span lang=EN-US style='font-size: 10.0pt;font-family:Consolas;mso-font-kerning:0pt'><o:p></o:p></span></p> <p class=MsoNormal align=left style='text-align:left;text-indent:0cm; mso-char-indent-count:0;mso-layout-grid-align:none;text-autospace:none'><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black; mso-font-kerning:0pt'><span style='mso-spacerun:yes'>&nbsp; </span></span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CCB07A; mso-font-kerning:0pt'>(</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#7F0055;mso-font-kerning:0pt'>is</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black; mso-font-kerning:0pt'> </span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#7ACC7A;mso-font-kerning:0pt'>(</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#7F0055; mso-font-kerning:0pt'>=</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:black;mso-font-kerning:0pt'> 1 </span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#7ACCB0; mso-font-kerning:0pt'>(</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#7F0055;mso-font-kerning:0pt'>-</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black; mso-font-kerning:0pt'> 4 3</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#7ACCB0;mso-font-kerning:0pt'>)</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#7ACC7A; mso-font-kerning:0pt'>)</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#CCB07A;mso-font-kerning:0pt'>)</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;mso-font-kerning: 0pt'><o:p></o:p></span></p> <p class=MsoNormal style='text-indent:20.0pt'><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black;mso-font-kerning: 0pt'><span style='mso-spacerun:yes'>&nbsp; </span></span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CCB07A;mso-font-kerning: 0pt'>(</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas; color:#7F0055;mso-font-kerning:0pt'>is</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black;mso-font-kerning: 0pt'> </span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas; color:#7ACC7A;mso-font-kerning:0pt'>(</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#7F0055;mso-font-kerning: 0pt'>=</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas; color:black;mso-font-kerning:0pt'> 3 </span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#7ACCB0;mso-font-kerning: 0pt'>(</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas; color:#7F0055;mso-font-kerning:0pt'>-</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black;mso-font-kerning: 0pt'> 7 4</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas; color:#7ACCB0;mso-font-kerning:0pt'>)</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#7ACC7A;mso-font-kerning: 0pt'>)</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas; color:#CCB07A;mso-font-kerning:0pt'>)</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CC7A7A;mso-font-kerning: 0pt'>)</span><span lang=EN-US style='mso-bidi-font-size:14.0pt'><o:p></o:p></span></p> </div> <![if !mso]></td> </tr> </table> <![endif]></v:textbox> <w:wrap type="none" anchorx="page" anchory="page" /> <w:anchorlock /> </v:rect><![endif]--><!--[if !vml]-->文本框: (deftest addition
  (is (= 4 (+ 2 2)))
  (is (= 7 (+ 3 4))))

(deftest subtraction
  (is (= 1 (- 4 3)))
  (is (= 3 (- 7 4))))
<!--[endif]--><!--[if mso & !supportInlineShapes & supportFields]><span style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK36'><span style='mso-bookmark:OLE_LINK11'><span style='mso-bookmark:OLE_LINK12'></span></span></span></span><v:shape id="_x0000_i1032" type="#_x0000_t75" style='width:144.2pt;height:120.3pt'> <v:imagedata croptop="-65520f" cropbottom="65520f" /> </v:shape><span style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK36'><span style='mso-bookmark:OLE_LINK11'><span style='mso-bookmark:OLE_LINK12'><span lang=EN-US style='font-size:9.0pt'><span style='mso-element:field-end'></span></span></span></span></span></span><![endif]-->

上述代码定义了名为additonsubtraction的两个函数,可以像其他任何函数一样对他们进行调用。因此测试代码能够进行分组或者某种形式的组合,如:

<!--[if mso & !supportInlineShapes & supportFields]><span style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK36'></span></span><span style='mso-element:field-begin;mso-field-lock:yes'></span><span style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK36'><span lang=EN-US style='font-size:9.0pt'><span style='mso-spacerun:yes'>&nbsp;</span>SHAPE <span style='mso-spacerun:yes'>&nbsp;</span>\* MERGEFORMAT <span style='mso-element: field-separator'></span></span></span></span><![endif]--><!--[if gte vml 1]><v:rect id="_x0000_s1032" style='width:114.95pt;height:60.3pt;flip:x;mso-wrap-style:none; mso-wrap-distance-top:7.2pt;mso-wrap-distance-bottom:36pt; mso-position-horizontal-relative:char;mso-position-vertical-relative:line' o:allowincell="f" fillcolor="white [3201]" strokecolor="#c2d69b [1942]" strokeweight="1pt"> <v:fill color2="#d6e3bc [1302]" focusposition="1" focussize="" focus="100%" type="gradient" /> <v:stroke dashstyle="1 1" endcap="round" /> <v:shadow on="t" type="perspective" color="#4e6128 [1606]" opacity=".5" offset="1pt" offset2="-3pt" /> <v:textbox style='mso-next-textbox:#_x0000_s1032;mso-fit-shape-to-text:t' inset="2.5mm,2.5mm,2.5mm,7.2pt"> <![if !mso]> <table cellpadding=0 cellspacing=0 width="100%"> <tr> <td><![endif]> <div> <p class=MsoNormal align=left style='text-align:left;text-indent:0cm; mso-char-indent-count:0;mso-layout-grid-align:none;text-autospace:none'><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CC7A7A; mso-font-kerning:0pt'>(</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#7F0055;mso-font-kerning:0pt'>deftest</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black; mso-font-kerning:0pt'> arithmetic</span><span lang=EN-US style='font-size: 10.0pt;font-family:Consolas;mso-font-kerning:0pt'><o:p></o:p></span></p> <p class=MsoNormal align=left style='text-align:left;text-indent:0cm; mso-char-indent-count:0;mso-layout-grid-align:none;text-autospace:none'><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black; mso-font-kerning:0pt'><span style='mso-spacerun:yes'>&nbsp; </span></span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CCB07A; mso-font-kerning:0pt'>(</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#7F0055;mso-font-kerning:0pt'>addition</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CCB07A; mso-font-kerning:0pt'>)</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;mso-font-kerning:0pt'><o:p></o:p></span></p> <p class=MsoNormal style='mso-char-indent-count:1.0'><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CCB07A;mso-font-kerning: 0pt'>(</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas; color:#7F0055;mso-font-kerning:0pt'>subtraction</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CCB07A;mso-font-kerning: 0pt'>)</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas; color:#CC7A7A;mso-font-kerning:0pt'>)</span><span lang=EN-US style='mso-bidi-font-size:14.0pt'><o:p></o:p></span></p> </div> <![if !mso]></td> </tr> </table> <![endif]></v:textbox> <w:wrap type="none" anchorx="page" anchory="page" /> <w:anchorlock /> </v:rect><![endif]--><!--[if !vml]-->文本框: (deftest arithmetic
  (addition)
(subtraction))
<!--[endif]--><!--[if mso & !supportInlineShapes & supportFields]><span style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK36'></span></span><v:shape id="_x0000_i1033" type="#_x0000_t75" style='width:114.95pt;height:60.3pt'> <v:imagedata croptop="-65520f" cropbottom="65520f" /> </v:shape><span style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK36'><span lang=EN-US style='font-size:9.0pt'><span style='mso-element:field-end'></span></span></span></span><![endif]-->

<!--[if !supportLists]-->第四章<!--[endif]-->运行单元测试代码

使用函数run-testsrun-all-tests可以运行编写好的测试代码,前者可以指定运行指定的命名空间中的测试代码,后者则运行所有命名空间中的测试代码。

<!--[if mso & !supportInlineShapes & supportFields]><span style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK36'><span style='mso-bookmark:OLE_LINK15'><span style='mso-bookmark:OLE_LINK16'></span></span></span></span><span style='mso-element:field-begin;mso-field-lock:yes'></span><span style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK36'><span style='mso-bookmark:OLE_LINK15'><span style='mso-bookmark:OLE_LINK16'><span lang=EN-US style='font-size:9.0pt'><span style='mso-spacerun:yes'>&nbsp;</span>SHAPE <span style='mso-spacerun:yes'>&nbsp;</span>\* MERGEFORMAT <span style='mso-element: field-separator'></span></span></span></span></span></span><![endif]--><!--[if gte vml 1]><v:rect id="_x0000_s1031" style='width:272.45pt;height:30.3pt;flip:x;mso-wrap-style:none; mso-wrap-distance-top:7.2pt;mso-wrap-distance-bottom:36pt; mso-position-horizontal-relative:char;mso-position-vertical-relative:line' o:allowincell="f" fillcolor="white [3201]" strokecolor="#c2d69b [1942]" strokeweight="1pt"> <v:fill color2="#d6e3bc [1302]" focusposition="1" focussize="" focus="100%" type="gradient" /> <v:stroke dashstyle="1 1" endcap="round" /> <v:shadow on="t" type="perspective" color="#4e6128 [1606]" opacity=".5" offset="1pt" offset2="-3pt" /> <v:textbox style='mso-next-textbox:#_x0000_s1031;mso-fit-shape-to-text:t' inset="2.5mm,2.5mm,2.5mm,7.2pt"> <![if !mso]> <table cellpadding=0 cellspacing=0 width="100%"> <tr> <td><![endif]> <div> <p class=MsoNormal style='text-indent:0cm;mso-char-indent-count:0'><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CC7A7A; mso-font-kerning:0pt'>(</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#7F0055;mso-font-kerning:0pt'>run-tests</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black; mso-font-kerning:0pt'> 'your.namespace 'some.other.namespace</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CC7A7A; mso-font-kerning:0pt'>)</span></p> </div> <![if !mso]></td> </tr> </table> <![endif]></v:textbox> <w:wrap type="none" anchorx="page" anchory="page" /> <w:anchorlock /> </v:rect><![endif]--><!--[if !vml]-->文本框: (run-tests 'your.namespace 'some.other.namespace)<!--[endif]--><!--[if mso & !supportInlineShapes & supportFields]><span style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK36'><span style='mso-bookmark:OLE_LINK15'><span style='mso-bookmark:OLE_LINK16'></span></span></span></span><v:shape id="_x0000_i1034" type="#_x0000_t75" style='width:272.45pt;height:30.3pt'> <v:imagedata croptop="-65520f" cropbottom="65520f" /> </v:shape><span style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK36'><span style='mso-bookmark:OLE_LINK15'><span style='mso-bookmark:OLE_LINK16'><span lang=EN-US style='font-size:9.0pt'><span style='mso-element:field-end'></span></span></span></span></span></span><![endif]-->

在调用run-tests时如若不指定命名空间则缺省为当前命名空间。

当使用上述方式运行测试代码时,所有的测试单元将以一种未定义的顺序运行。如果想要组织测试代码,如上述的”arithmetic”示例,也许是我们不想让”additiona””subtraction”分开运行。这种场景下,我们不得不定义一个被命名为”test-ns-hook”的特殊函数,它使得测试代码以正确的顺序运行,如:

<!--[if mso & !supportInlineShapes & supportFields]><span style='mso-bookmark:OLE_LINK36'><span style='mso-bookmark:OLE_LINK35'></span></span><span style='mso-element:field-begin;mso-field-lock:yes'></span><span style='mso-bookmark:OLE_LINK36'><span style='mso-bookmark:OLE_LINK35'><span lang=EN-US style='font-size:9.0pt'><span style='mso-spacerun:yes'>&nbsp;</span>SHAPE <span style='mso-spacerun:yes'>&nbsp;</span>\* MERGEFORMAT <span style='mso-element: field-separator'></span></span></span></span><![endif]--><!--[if gte vml 1]><v:rect id="_x0000_s1030" style='width:198.95pt;height:30.3pt;flip:x;mso-wrap-style:none; mso-wrap-distance-top:7.2pt;mso-wrap-distance-bottom:36pt; mso-position-horizontal-relative:char;mso-position-vertical-relative:line' o:allowincell="f" fillcolor="white [3201]" strokecolor="#c2d69b [1942]" strokeweight="1pt"> <v:fill color2="#d6e3bc [1302]" focusposition="1" focussize="" focus="100%" type="gradient" /> <v:stroke dashstyle="1 1" endcap="round" /> <v:shadow on="t" type="perspective" color="#4e6128 [1606]" opacity=".5" offset="1pt" offset2="-3pt" /> <v:textbox style='mso-next-textbox:#_x0000_s1030;mso-fit-shape-to-text:t' inset="2.5mm,2.5mm,2.5mm,7.2pt"> <![if !mso]> <table cellpadding=0 cellspacing=0 width="100%"> <tr> <td><![endif]> <div> <p class=MsoNormal style='text-indent:0cm;mso-char-indent-count:0'><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CC7A7A; mso-font-kerning:0pt'>(</span><b><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#7F0055;mso-font-kerning:0pt'>defn</span></b><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black; mso-font-kerning:0pt'> test-ns-hook [] </span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CCB07A;mso-font-kerning: 0pt'>(</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas; color:#7F0055;mso-font-kerning:0pt'>arithmetic</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CCB07A;mso-font-kerning: 0pt'>)</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas; color:#CC7A7A;mso-font-kerning:0pt'>)</span><span lang=EN-US style='mso-bidi-font-size:14.0pt'><o:p></o:p></span></p> </div> <![if !mso]></td> </tr> </table> <![endif]></v:textbox> <w:wrap type="none" anchorx="page" anchory="page" /> <w:anchorlock /> </v:rect><![endif]--><!--[if !vml]-->文本框: (defn test-ns-hook [] (arithmetic))<!--[endif]--><!--[if mso & !supportInlineShapes & supportFields]><span style='mso-bookmark:OLE_LINK36'><span style='mso-bookmark:OLE_LINK35'></span></span><v:shape id="_x0000_i1035" type="#_x0000_t75" style='width:198.95pt;height:30.3pt'> <v:imagedata croptop="-65520f" cropbottom="65520f" /> </v:shape><span style='mso-bookmark:OLE_LINK36'><span style='mso-bookmark:OLE_LINK35'><span lang=EN-US style='font-size:9.0pt'><span style='mso-element:field-end'></span></span></span></span><![endif]-->

需要注意的是”test-ns-hook”将阻止其他的固定装置(”fixtures”)测试代码执行。

 

在发布模式下省略测试代码:当在发布模式进行载入或者编译代码时可以通过将” *load-test*”绑定到false以防止任何通过”with-test””deftest”创建的测试代码被编译和运行。

 

 

<!--[if !supportLists]-->第五章<!--[endif]-->Clojure测试框架中的装配函数

装配函数允许我们在运行测试代码之前或者之后运行某些代码,用以设置测试代码应该运行的上下文环境。一个装配函数仅仅是一个将其他函数作为参数进行调用的函数。形如:

<!--[if mso & !supportInlineShapes & supportFields]><span style='mso-bookmark:OLE_LINK36'><span style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK21'><span style='mso-bookmark:OLE_LINK22'></span></span></span></span><span style='mso-element:field-begin;mso-field-lock:yes'></span><span style='mso-bookmark:OLE_LINK36'><span style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK21'><span style='mso-bookmark:OLE_LINK22'><span lang=EN-US style='font-size:9.0pt'><span style='mso-spacerun:yes'>&nbsp;</span>SHAPE <span style='mso-spacerun:yes'>&nbsp;</span>\* MERGEFORMAT <span style='mso-element: field-separator'></span></span></span></span></span></span><![endif]--><!--[if gte vml 1]><v:rect id="_x0000_s1029" style='width:264.95pt;height:90.3pt;flip:x;mso-wrap-style:none; mso-wrap-distance-top:7.2pt;mso-wrap-distance-bottom:36pt; mso-position-horizontal-relative:char;mso-position-vertical-relative:line' o:allowincell="f" fillcolor="white [3201]" strokecolor="#c2d69b [1942]" strokeweight="1pt"> <v:fill color2="#d6e3bc [1302]" focusposition="1" focussize="" focus="100%" type="gradient" /> <v:stroke dashstyle="1 1" endcap="round" /> <v:shadow on="t" type="perspective" color="#4e6128 [1606]" opacity=".5" offset="1pt" offset2="-3pt" /> <v:textbox style='mso-next-textbox:#_x0000_s1029;mso-fit-shape-to-text:t' inset="2.5mm,2.5mm,2.5mm,7.2pt"> <![if !mso]> <table cellpadding=0 cellspacing=0 width="100%"> <tr> <td><![endif]> <div> <p class=MsoNormal align=left style='text-align:left;text-indent:0cm; mso-char-indent-count:0;mso-layout-grid-align:none;text-autospace:none'><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CC7A7A; mso-font-kerning:0pt'>(</span><b><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#7F0055;mso-font-kerning:0pt'>defn</span></b><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black; mso-font-kerning:0pt'> my-fixture [f]</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;mso-font-kerning:0pt'><o:p></o:p></span></p> <p class=MsoNormal align=left style='text-align:left;text-indent:0cm; mso-char-indent-count:0;mso-layout-grid-align:none;text-autospace:none'><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black; mso-font-kerning:0pt'><span style='mso-spacerun:yes'>&nbsp;&nbsp; </span></span><b style='mso-bidi-font-weight:normal'><i><span lang=EN-US style='font-size: 10.0pt;font-family:Consolas;color:#3F7F5F;mso-font-kerning:0pt'>;Perform setup, establish bindings, whatever.</span></i></b><b style='mso-bidi-font-weight: normal'><span lang=EN-US style='font-size:10.0pt;font-family:Consolas; mso-font-kerning:0pt'><o:p></o:p></span></b></p> <p class=MsoNormal align=left style='text-align:left;text-indent:0cm; mso-char-indent-count:0;mso-layout-grid-align:none;text-autospace:none'><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black; mso-font-kerning:0pt'><span style='mso-spacerun:yes'>&nbsp; </span></span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CCB07A; mso-font-kerning:0pt'>(</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#7F0055;mso-font-kerning:0pt'>f</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CCB07A; mso-font-kerning:0pt'>)</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:black;mso-font-kerning:0pt'><span style='mso-spacerun:yes'>&nbsp; </span></span><i><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#3F7F5F;mso-font-kerning: 0pt'>;Then call the function we were passed.</span></i><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;mso-font-kerning:0pt'><o:p></o:p></span></p> <p class=MsoNormal align=left style='text-align:left;text-indent:0cm; mso-char-indent-count:0;mso-layout-grid-align:none;text-autospace:none'><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black; mso-font-kerning:0pt'><span style='mso-spacerun:yes'>&nbsp;&nbsp; </span></span><b style='mso-bidi-font-weight:normal'><i><span lang=EN-US style='font-size: 10.0pt;font-family:Consolas;color:#3F7F5F;mso-font-kerning:0pt'>;Tear-down / clean-up code here.</span></i></b><b style='mso-bidi-font-weight:normal'><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;mso-font-kerning: 0pt'><o:p></o:p></span></b></p> <p class=MsoNormal align=left style='text-align:left;text-indent:0cm; mso-char-indent-count:0;mso-layout-grid-align:none;text-autospace:none'><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black; mso-font-kerning:0pt'><span style='mso-spacerun:yes'>&nbsp;</span></span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CC7A7A; mso-font-kerning:0pt'>)</span><span lang=EN-US style='mso-bidi-font-size: 14.0pt'><o:p></o:p></span></p> </div> <![if !mso]></td> </tr> </table> <![endif]></v:textbox> <w:wrap type="none" anchorx="page" anchory="page" /> <w:anchorlock /> </v:rect><![endif]--><!--[if !vml]-->文本框: (defn my-fixture [f]
   ;Perform setup, establish bindings, whatever.
  (f)  ;Then call the function we were passed.
   ;Tear-down / clean-up code here.
 )
<!--[endif]--><!--[if mso & !supportInlineShapes & supportFields]><span style='mso-bookmark:OLE_LINK36'><span style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK21'><span style='mso-bookmark:OLE_LINK22'></span></span></span></span><v:shape id="_x0000_i1036" type="#_x0000_t75" style='width:264.95pt;height:90.3pt'> <v:imagedata croptop="-65520f" cropbottom="65520f" /> </v:shape><span style='mso-bookmark:OLE_LINK36'><span style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK21'><span style='mso-bookmark:OLE_LINK22'><span lang=EN-US style='font-size:9.0pt'><span style='mso-element:field-end'></span></span></span></span></span></span><![endif]-->

装配函数以两种方式中的一种附加到命名空间中。”each”类型的装配函数会反复执行,每次运行通过”deftest”或者”with-test”创建的测试函数都会执行一次。”each”类型的装配函数有利于为每一个测试函数建立一致的前置或者后置状态,例如清理数据库表。

”each”类型的装配函数能够像下述代码那样附加到当前命名空间中。

<!--[if mso & !supportInlineShapes & supportFields]><span style='mso-bookmark:OLE_LINK36'><span style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK23'><span style='mso-bookmark:OLE_LINK24'></span></span></span></span><span style='mso-element:field-begin;mso-field-lock:yes'></span><span style='mso-bookmark:OLE_LINK36'><span style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK23'><span style='mso-bookmark:OLE_LINK24'><span lang=EN-US style='font-size:9.0pt'><span style='mso-spacerun:yes'>&nbsp;</span>SHAPE <span style='mso-spacerun:yes'>&nbsp;</span>\* MERGEFORMAT <span style='mso-element: field-separator'></span></span></span></span></span></span><![endif]--><!--[if gte vml 1]><v:rect id="_x0000_s1028" style='width:235.7pt;height:30.3pt;flip:x;mso-wrap-style:none; mso-wrap-distance-top:7.2pt;mso-wrap-distance-bottom:36pt; mso-position-horizontal-relative:char;mso-position-vertical-relative:line' o:allowincell="f" fillcolor="white [3201]" strokecolor="#c2d69b [1942]" strokeweight="1pt"> <v:fill color2="#d6e3bc [1302]" focusposition="1" focussize="" focus="100%" type="gradient" /> <v:stroke dashstyle="1 1" endcap="round" /> <v:shadow on="t" type="perspective" color="#4e6128 [1606]" opacity=".5" offset="1pt" offset2="-3pt" /> <v:textbox style='mso-next-textbox:#_x0000_s1028;mso-fit-shape-to-text:t' inset="2.5mm,2.5mm,2.5mm,7.2pt"> <![if !mso]> <table cellpadding=0 cellspacing=0 width="100%"> <tr> <td><![endif]> <div> <p class=MsoNormal style='text-indent:0cm;mso-char-indent-count:0'><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CC7A7A; mso-font-kerning:0pt'>(</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#7F0055;mso-font-kerning:0pt'>use-fixtures</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black; mso-font-kerning:0pt'> </span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#2A00FF;mso-font-kerning:0pt'>:each</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black; mso-font-kerning:0pt'> fixture1 fixture2 ...</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CC7A7A;mso-font-kerning: 0pt'>)</span><span lang=EN-US style='mso-bidi-font-size:14.0pt'><o:p></o:p></span></p> </div> <![if !mso]></td> </tr> </table> <![endif]></v:textbox> <w:wrap type="none" anchorx="page" anchory="page" /> <w:anchorlock /> </v:rect><![endif]--><!--[if !vml]-->文本框: (use-fixtures :each fixture1 fixture2 ...)<!--[endif]--><!--[if mso & !supportInlineShapes & supportFields]><span style='mso-bookmark:OLE_LINK36'><span style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK23'><span style='mso-bookmark:OLE_LINK24'></span></span></span></span><v:shape id="_x0000_i1037" type="#_x0000_t75" style='width:235.7pt;height:30.3pt'> <v:imagedata croptop="-65520f" cropbottom="65520f" /> </v:shape><span style='mso-bookmark:OLE_LINK36'><span style='mso-bookmark:OLE_LINK35'><span style='mso-bookmark:OLE_LINK23'><span style='mso-bookmark:OLE_LINK24'><span lang=EN-US style='font-size:9.0pt'><span style='mso-element:field-end'></span></span></span></span></span></span><![endif]-->

fixture1fixture2这些装配函数也仅仅是形如上述事例代码那样的函数(my-fixture)。他们也可以是匿名函数,像这样:

<!--[if mso & !supportInlineShapes & supportFields]><span style='mso-bookmark:OLE_LINK36'><span style='mso-bookmark:OLE_LINK35'></span></span><span style='mso-element:field-begin;mso-field-lock:yes'></span><span style='mso-bookmark:OLE_LINK36'><span style='mso-bookmark:OLE_LINK35'><span lang=EN-US style='font-size:9.0pt'><span style='mso-spacerun:yes'>&nbsp;</span>SHAPE <span style='mso-spacerun:yes'>&nbsp;</span>\* MERGEFORMAT <span style='mso-element: field-separator'></span></span></span></span><![endif]--><!--[if gte vml 1]><v:rect id="_x0000_s1027" style='width:293.45pt;height:30.3pt;flip:x;mso-wrap-style:none; mso-wrap-distance-top:7.2pt;mso-wrap-distance-bottom:36pt; mso-position-horizontal-relative:char;mso-position-vertical-relative:line' o:allowincell="f" fillcolor="white [3201]" strokecolor="#c2d69b [1942]" strokeweight="1pt"> <v:fill color2="#d6e3bc [1302]" focusposition="1" focussize="" focus="100%" type="gradient" /> <v:stroke dashstyle="1 1" endcap="round" /> <v:shadow on="t" type="perspective" color="#4e6128 [1606]" opacity=".5" offset="1pt" offset2="-3pt" /> <v:textbox style='mso-next-textbox:#_x0000_s1027;mso-fit-shape-to-text:t' inset="2.5mm,2.5mm,2.5mm,7.2pt"> <![if !mso]> <table cellpadding=0 cellspacing=0 width="100%"> <tr> <td><![endif]> <div> <p class=MsoNormal style='text-indent:0cm;mso-char-indent-count:0'><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CC7A7A; mso-font-kerning:0pt'>(</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#7F0055;mso-font-kerning:0pt'>use-fixtures</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black; mso-font-kerning:0pt'> </span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#2A00FF;mso-font-kerning:0pt'>:each</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black; mso-font-kerning:0pt'> </span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#CCB07A;mso-font-kerning:0pt'>(</span><b><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#7F0055; mso-font-kerning:0pt'>fn</span></b><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:black;mso-font-kerning:0pt'> [f] setup... </span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#7ACC7A; mso-font-kerning:0pt'>(</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#7F0055;mso-font-kerning:0pt'>f</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#7ACC7A; mso-font-kerning:0pt'>)</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:black;mso-font-kerning:0pt'> cleanup...</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CCB07A; mso-font-kerning:0pt'>)</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#CC7A7A;mso-font-kerning:0pt'>)</span><span lang=EN-US style='mso-bidi-font-size:14.0pt'><o:p></o:p></span></p> </div> <![if !mso]></td> </tr> </table> <![endif]></v:textbox> <w:wrap type="none" anchorx="page" anchory="page" /> <w:anchorlock /> </v:rect><![endif]--><!--[if !vml]-->文本框: (use-fixtures :each (fn [f] setup... (f) cleanup...))<!--[endif]--><!--[if mso & !supportInlineShapes & supportFields]><span style='mso-bookmark:OLE_LINK36'><span style='mso-bookmark:OLE_LINK35'></span></span><v:shape id="_x0000_i1038" type="#_x0000_t75" style='width:293.45pt;height:30.3pt'> <v:imagedata croptop="-65520f" cropbottom="65520f" /> </v:shape><span style='mso-bookmark:OLE_LINK36'><span style='mso-bookmark:OLE_LINK35'><span lang=EN-US style='font-size:9.0pt'><span style='mso-element:field-end'></span></span></span></span><![endif]-->

另外一种”once”类型的装配函数,在贯穿命名空间的所有测试代码执行期间只运行一次,它非常有利于执行那些只需要一次的操作,例如建立数据库连接或者执行那些比较消耗时间的任务。

附加”once”类型的装配函数到当前命名空间:

<!--[if mso & !supportInlineShapes & supportFields]><span style='mso-bookmark:OLE_LINK36'><span style='mso-bookmark:OLE_LINK35'></span></span><span style='mso-element:field-begin;mso-field-lock:yes'></span><span style='mso-bookmark:OLE_LINK36'><span style='mso-bookmark:OLE_LINK35'><span lang=EN-US style='font-size:9.0pt'><span style='mso-spacerun:yes'>&nbsp;</span>SHAPE <span style='mso-spacerun:yes'>&nbsp;</span>\* MERGEFORMAT <span style='mso-element: field-separator'></span></span></span></span><![endif]--><!--[if gte vml 1]><v:rect id="_x0000_s1026" style='width:235.7pt;height:30.3pt;flip:x;mso-wrap-style:none; mso-wrap-distance-top:7.2pt;mso-wrap-distance-bottom:36pt; mso-position-horizontal-relative:char;mso-position-vertical-relative:line' o:allowincell="f" fillcolor="white [3201]" strokecolor="#c2d69b [1942]" strokeweight="1pt"> <v:fill color2="#d6e3bc [1302]" focusposition="1" focussize="" focus="100%" type="gradient" /> <v:stroke dashstyle="1 1" endcap="round" /> <v:shadow on="t" type="perspective" color="#4e6128 [1606]" opacity=".5" offset="1pt" offset2="-3pt" /> <v:textbox style='mso-next-textbox:#_x0000_s1026;mso-fit-shape-to-text:t' inset="2.5mm,2.5mm,2.5mm,7.2pt"> <![if !mso]> <table cellpadding=0 cellspacing=0 width="100%"> <tr> <td><![endif]> <div> <p class=MsoNormal style='text-indent:0cm;mso-char-indent-count:0'><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CC7A7A; mso-font-kerning:0pt'>(</span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#7F0055;mso-font-kerning:0pt'>use-fixtures</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black; mso-font-kerning:0pt'> </span><span lang=EN-US style='font-size:10.0pt; font-family:Consolas;color:#2A00FF;mso-font-kerning:0pt'>:once</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:black; mso-font-kerning:0pt'> fixture1 fixture2 ...</span><span lang=EN-US style='font-size:10.0pt;font-family:Consolas;color:#CC7A7A;mso-font-kerning: 0pt'>)</span><span lang=EN-US style='mso-bidi-font-size:14.0pt'><o:p></o:p></span></p> </div> <![if !mso]></td> </tr> </table> <![endif]></v:textbox> <w:wrap type="none" anchorx="page" anchory="page" /> <w:anchorlock /> </v:rect><![endif]--><!--[if !vml]-->文本框: (use-fixtures :once fixture1 fixture2 ...)<!--[endif]--><!--[if mso & !supportInlineShapes & supportFields]><span style='mso-bookmark:OLE_LINK36'><span style='mso-bookmark:OLE_LINK35'></span></span><v:shape id="_x0000_i1039" type="#_x0000_t75" style='width:235.7pt;height:30.3pt'> <v:imagedata croptop="-65520f" cropbottom="65520f" /> </v:shape><span style='mso-bookmark:OLE_LINK36'><span style='mso-bookmark:OLE_LINK35'><span lang=EN-US style='font-size:9.0pt'><span style='mso-element:field-end'></span></span></span></span><![endif]-->

注意:装配函数和test-ns-hook是互不兼容的,如果我们正在使用test-ns-hook,那么装配函数将永远得不到执行。

<!--[if !supportLists]-->第六章<!--[endif]-->保存测试结果到一个文件中

将测试结果保存到一个文件中,这是多么美妙的一件事啊,在写了大量代码之后发现不少测试没有通过,而这些测试中包含了比较耗时的IO操作,这时又急着下班去泡妞,电脑也不得不关,没关系,将测试结果保存到某个文件中,并发送到邮箱中,等周末没事再看看咋回事吧。

Clojure中所有的测试报告都被写入*test-out*中。*test-out*默认为*out*,我们可以将其重绑定到任何的PrintWriter对象上。例如,可以是一个使用clojure.java.io/writer打开的文件。

<!--[if !supportLists]-->第七章<!--[endif]-->扩展test-is

通过给”assert-expr”multimethod定义新的方法可以对is宏进行扩展。这些方法在is宏展开期间被调用,因此他们理应返回引用形式以便求值。
我们可以通过重绑定”report”函数插入自己的测试报告框架:(report event)
event参数是一个map,它总是有一个:type关键词,它的值是一个暗示被报告事件的类型。标准的事件类型在断言通过、失败和抛出异常时被调用,分别对应于:type的值:pass:fail:error
既然如此就需要有如下所示关键词所表示事件结果相应的状态:

  :expected   期望结果为true的表达式形式

  :actual     一个表示实际上所发生表达式形式

  :message    作为参数传递给is宏的给定字符串信息

所有*testing*字面值被记录再*testing-contexts*所表示的List中,而被测试的所有vars都记录在*testing-vars*所表示的List中。 

你的报告函数应该包裹任何打印调用在*with-test-out*宏中,这个宏将*out*重绑定到*test-out*的当前值上。

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics