- 浏览: 15810 次
- 性别:
- 来自: 广州
最新评论
Clojure的测试框架 基于测试的开发方式: 好的测试框架有利于驱动项目的开发,当编写代码前写了测试代码,那么就非常有利于开发人员集中精力,着力于当前待解决的
Clojure的测试框架
基于测试的开发方式:
好的测试框架有利于驱动项目的开发,当编写代码前写了测试代码,那么就非常有利于开发人员集中精力,着力于当前待解决的问题,加上Clojure的REPL环境,提高了开发速度。
<!--[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'> </span>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_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'>"abcde"</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'>"ab"</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]--><!--[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'> </span>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_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'>=> <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> </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'> </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]--><!--[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]--><!--[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'>#"Divide by zero"</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]--><!--[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'> </span>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_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'> </span>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_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-test和deftest。
<!--[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'> </span>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_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'> </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'> </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'> </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'> </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]--><!--[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'> </span>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_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'> </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'> </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> </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'> </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'> </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]--><!--[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]-->
上述代码定义了名为additon和subtraction的两个函数,可以像其他任何函数一样对他们进行调用。因此测试代码能够进行分组或者某种形式的组合,如:
<!--[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'> </span>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_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'> </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]--><!--[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-tests和run-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'> </span>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_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]--><!--[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'> </span>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_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]--><!--[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'> </span>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_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'> </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'> </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'> </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'> </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'> </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]--><!--[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'> </span>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_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]--><!--[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]-->
fixture1,fixture2这些装配函数也仅仅是形如上述事例代码那样的函数(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'> </span>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_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]--><!--[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'> </span>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_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]--><!--[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 DSL,允许使用Clojure s表达式和宏编写C代码 生成默认情况下仅依赖于以下3个标准头文件的可移植C99兼容代码:stdint.h,stddef.h,stdbool.h 创建到不同编译器后端的接口,该接口允许在开发模式下...
clojure:添加测试源 clojure:编译 clojure:测试 clojure:junit测试 clojure:运行 clojure:repl clojure:nrepl clojure:抽烟 clojure:nailgun clojure:gendoc clojure:autodoc clojure:marginalia ...
并在“问题”视图中链接到失败或导致错误的测试调试器(现在为可选-无需调试即可运行交互式REPL) 设置断点检查断点处的堆栈框架/变量在断点评估代码为“ editor.wordSeparators”添加特定于Clojure的设置,使选择...
dashboard-clj, 用于构建实时仪表板的Clojure迷你框架 仪表板 clj 一个用于构建仪表板的clojure迷你框架,由 dash提供。很多管道工作都是用来建造仪表板的。 设置仪表板涉及以下挑战:正在获取数据调度数据检索将...
绿灯:Clojure集成测试框架
一个小型遗传算法框架,用 Clojure 编写
状态Clojure 中状态计算的基于属性的生成测试。示例: java.util.Set 在这个例子中,我们将指定与实现Set接口的对象的交互。 让我们从需要必要的命名空间开始。 ( require '[clojure.test.check :refer [quick-check...
Hypercrud: 用于可组合UI的clojure客户端/服务器框架
Clojure Copyright (c) Rich Hickey. All rights reserved. The use and distribution terms for this software are covered by the Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) ...
tools.analyzer, 用于Clojure代码的分析器,用Clojure编写,并在EDN中生成 AST tools.analyzer主机无关Clojure代码的分析器,用Clojure编写,并在EDN中生成 AST 。我在 2015年12月的ClojureX上讨论了 tools.analyzer...
Schema 是一个 Clojure(Script) 库,用来声明数据描述和验证。 代码示例:(ns schema-examples (:require [schema.core :as s :include-macros true ;; cljs only ])) (def Data "A schema for ...
Clojure web开发视频教程,或在线观看地址 http://sharevip.org/tags/clojure%E8%A7%86%E9%A2%91%E6%95%99%E7%A8%8B/
粉色的 用Clojure编写的音乐系统开发库。介绍该库为开发音乐系统提供了基础。 它的设计还可以根据用户需求扩展,无论他们是在探索和设计低级信号处理算法,开发预先编写的合成文件还是创建交互式实时系统。 它提供了...
Cavia用Clojure编写,因此可以直接在项目和源代码中使用。 Cavia从远程下载测试资源,在测试之前检查其哈希,并提供方便的功能来访问资源。安装Cavia可以从作为Maven工件获得。 使用Leiningen / Boot: [cavia " ...
长期以来,ClojureScript中的React应用程序将依赖于 , 和 , 等全面的库来解决大部分Web应用程序问题,而将React纯粹视为视图层。 特别是,在状态管理之上,这些库还将处理React性:确保在状态发生变化时重新渲染...
Clojure Lab是一个旨在在 Clojure 中为 Clojure 构建开发环境的项目,其主要目标是可用性和可扩展性。 这个项目目前正在开发中,所以接下来的大多数断言都更接近一厢情愿而不是现实......现在:)。 可用性 刚接触...
clojure-essential-ref::bookmark:cider-doc到“ Clojure,基本参考”
circleci.test Clojure测试运行程序与使用clojure.test编写的测试兼容。 保留现有的deftest ,但在如何运行它们方面获得灵活性。用法将[circleci/circleci.test "0.5.0"]到:dev下的:dependencies 。 推荐使用这组 ...
根据https://github.com/clojure-cookbook/clojure-cookbook的描述,将cookbook的代码转换为了html文档,供方便下载。
clojure2minizinc 是一个通过 MiniZinc 解决约束满足问题的 Clojure 库。clojure2minizinc 提供一个 state-of-the-art constraint solvers (via MiniZinc) 和高级编程语言 Clojure 之间的接口。代码示例:(ns ...