`
xiuzhuchenqing
  • 浏览: 16138 次
  • 性别: 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*的当前值上。

 

分享到:
评论

相关推荐

    绿灯:Clojure集成测试框架

    绿灯(Greenlight)是一个专为Clojure设计的集成测试框架,它旨在提供一种高效、简洁的方式来编写和执行集成测试。Clojure是一种基于Lisp的函数式编程语言,被广泛用于构建可扩展和高并发的系统。集成测试是软件开发...

    clojure1.4包

    要开始使用,你需要解压文件,然后在终端或命令行中启动 Clojure 的 REPL,通过 `require` 命令引入所需库,开始编写和测试代码。 总的来说,Clojure 1.4 和 http-kit 结合,为开发者提供了一套强大的工具,用于...

    茄子:Clojure的行为驱动开发(BDD)库。 简单是关键

    茄子,又名Eggplant,是一款专为Clojure编程语言设计的行为驱动开发(BDD)库。它旨在简化BDD实践,使开发者能够更自然地表达代码中的行为和期望,从而促进更好的沟通和代码质量。BDD是一种软件开发方法,强调以业务...

    Clojure Data Analysis Cookbook

    - **性能优化**:介绍如何编写高效的 Clojure 代码,包括内存管理和垃圾回收机制的理解。 - **并发编程**:深入探讨 Clojure 的并发模型,如原子、引用、代理和通道等。 - **社区资源和工具**:列出了一系列 ...

    Apress.Practical.Clojure.May.2010.rar

    4. 数据结构:Clojure的主导数据结构是链表和映射,它们都是不可变的,这有利于代码的并发处理和数据安全。 5. 与Java互操作:由于Clojure运行在JVM上,可以直接调用Java类库,同时Java也能调用Clojure代码,实现两...

    克隆:基于沙沙作响的学习Clojure的项目

    测试驱动开发(TDD)是一种常见的编程实践,它提倡先编写测试,再编写满足测试的代码。通过TDD,学习者可以更好地理解Clojure的测试框架,如Leiningen和Midje,同时提高代码质量。 在Clojure中,Leiningen是一个...

    场景:Clojure BDD库-行为驱动开发的可执行规范

    Clojure BDD库是为Clojure编程语言设计的一组工具,它们帮助开发者以更加清晰和可读的方式来编写测试,确保代码符合预期的行为。这些库通常采用Domain-Specific Language (DSL)来表达类似于Cucumber中的“Given-When...

    企业clojure培训:面向开发人员和高级开发人员的clojure培训课程

    Clojure是一种基于Lisp的函数式编程语言,它运行在Java虚拟机(JVM)上,充分利用了Java的生态系统。由于其简洁、高效和并行处理能力,Clojure在企业级应用开发中越来越受到欢迎。面向开发人员和高级开发人员的...

    clojure-must-watch-源码.rar

    8. **测试**:Clojure提供了`clojure.test`库进行单元测试,学习源码时应关注测试代码,理解测试驱动开发(TDD)和行为驱动开发(BDD)在Clojure中的应用。 9. **函数式数据库**:如Datomic,是Clojure生态系统中的...

    SimpleClojureWebApp:简单的基于 Clojure 的 Web 应用程序模板

    - **测试**:Clojure 有丰富的测试框架,如 Midje、Spec 和 clojure.test,鼓励开发者编写自动化测试。 通过理解和学习 "SimpleClojureWebApp",开发者可以快速掌握 Clojure Web 开发的基本概念,进而构建更复杂的 ...

    链接:用于非阻塞网络编程的Clojure框架

    7. **文档和示例**:为了让Clojure程序员更好地理解和使用这个框架,完整的文档和示例代码是必不可少的,它们会展示如何配置服务器、处理请求、响应客户端等基本操作。 在"link-master"这个文件名中,"master"通常...

    spacemacs-content:这本书的内容-使用Spacemacs进行Clojure开发

    - Midje和Criterium:介绍这两个Clojure测试框架的使用,以及如何在Spacemacs中进行TDD(测试驱动开发)。 5. **ClojureScript开发**: - Cljsbuild和figwheel:解释如何使用Cljsbuild编译ClojureScript项目,...

    fulcro-spec:包装clojure.test的库,以获得更好的BDD测试体验

    `fulcro-spec` 是一个为 Clojure 和 ClojureScript 社区设计的测试库,它扩展了标准的 `clojure.test` 框架,提供了更丰富的行为驱动开发(BDD)风格的测试体验。BDD 测试方法强调用自然语言来描述软件的行为,使得...

    HTTPKit-Clojure的高性能事件驱动HTTP客户端服务器

    HTTPKit是专门为Clojure设计的一款高性能、事件驱动的HTTP客户端和服务器库。它以其轻量级、快速且易于使用的特性,在Clojure社区中受到了广泛欢迎。这个库是基于Java NIO(非阻塞I/O)构建的,充分利用了Java平台的...

    cljweb:一个 clojure web 项目开始

    4. **测试代码 (test)**:如果项目遵循 TDD(测试驱动开发)原则,那么测试代码会位于 `test` 目录下,通常以 `test.*.cljs` 的格式命名,使用ClojureScript测试库如 Midje 或 Expectations 进行编写。 5. **配置...

    mr-clojure-exploded:MixRadio leiningen模板的结果代码

    4. `test`目录:包含项目的测试代码,通常使用Clojure的TestNG或Midje等测试框架进行编写,确保代码的质量和稳定性。 5. `target`目录:Leiningen在构建过程中生成的中间和最终输出文件,如编译后的类文件和最终的...

    testit:Midje喜欢Clojure.test的断言

    在Clojure的世界里,测试是开发过程中的重要组成部分,它帮助我们确保代码的质量和稳定性。Midje和Clojure.test是两个流行的Clojure测试框架,它们提供了丰富的断言机制来验证程序的行为。本文将深入探讨"testit:...

    commix:用于数据驱动的可组合系统架构的微框架

    它是一个基于Clojure编程语言的微框架,设计目标是支持数据驱动的、可组合的系统架构。微框架意味着它具有轻量级的特性,旨在提供一种高效、简洁的方式来构建复杂的应用程序。数据驱动表示系统的主要行为由数据流和...

Global site tag (gtag.js) - Google Analytics