您的位置:軟件測(cè)試 > 開源軟件測(cè)試 > 開源單元測(cè)試工具 > Nunit
NUnit學(xué)習(xí)筆記
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2013/3/20 15:10:20 ] 推薦標(biāo)簽:

 測(cè)試效果:示例中包含5個(gè)類,前4個(gè)類被分為class1和class2兩個(gè)組,后1個(gè)類中包含的四個(gè)方法被分為funtion1和function2兩個(gè)組。對(duì)于這種分組的測(cè)試,要想看出效果,在打開NUnit后,需要先在Categories選項(xiàng)卡中作出選擇,如下圖所示:

 可以看到,所有的組默認(rèn)都在上邊的Available Categories列表框中,選中的組則通過(guò)Add按鈕添加到下邊的Selected Categories列表框中,NUnit允許選中多個(gè)。測(cè)試完成后對(duì)于不想要的組,還可以通過(guò)Remove按鈕放回到上邊。選好后,選擇Tests選項(xiàng)卡,選中根節(jié)點(diǎn),然后點(diǎn)擊Run按鈕,效果如下:

 可以看到,只有與class1組對(duì)應(yīng)的Class11和Class12這兩個(gè)類被測(cè)試了。
 此外您可能也注意到了,Categories選項(xiàng)卡中還提供了一個(gè)Exclude these categories選項(xiàng),選中該選項(xiàng),回到Tests選項(xiàng)卡,選中根節(jié)點(diǎn),然后點(diǎn)擊Run按鈕,效果如下:

 此時(shí)與class1組對(duì)應(yīng)的Class11和Class12這兩個(gè)類在測(cè)試中被忽略了。
 
    Explicit屬性,與Ignore屬性有些類似,也是用于對(duì)暫時(shí)不想運(yùn)行的測(cè)試類或測(cè)試方法做忽略的。但與Ignore屬性相比還有兩點(diǎn)差別:一個(gè)是Explicit屬性不需要說(shuō)明信息,一個(gè)是使用Explicit屬性做忽略的類或方法在NUnit中被選中后,將不再被忽略,而是進(jìn)行測(cè)試。
    示例:
VB代碼:
        <TestFixture()> _
Public Class Test
<Test(), Explicit()> _
Public Sub TestMethod()
End Sub
End Class
 
<TestFixture(), Explicit()> _
Public Class Test2
<Test()> _
Public Sub TestMethod()
End Sub
End Class
C#代碼:
        [TestFixture]
public class Test
{
[Test, Explicit]
public void TestMethod()
{}
}
 
[TestFixture, Explicit]
public class Test2
{
[Test]
public void TestMethod()
{}
}
J#代碼:
        /** @attribute TestFixture() */
public class Test
{
/** @attribute Test(),Explicit() */
public void TestMethod()
{}
}
 
/** @attribute TestFixture(),Explicit() */
public class Test2
{
/** @attribute Test() */
public void TestMethod()
{}
}
    測(cè)試效果:類Test所忽略的是方法,若不在NUnit中選中該方法,測(cè)試后該方法前的圓點(diǎn)為黃色,即在測(cè)試中被忽略,若選中了則在測(cè)試后顯示為綠色或紅色,這與測(cè)試的結(jié)果有關(guān);類Test2所忽略的是類,效果與Test類似。
 
    Platform屬性,平臺(tái)屬性,用來(lái)指定運(yùn)行平臺(tái),若測(cè)試類或測(cè)試方法所運(yùn)行的平臺(tái)與指定的一致則執(zhí)行,否則忽略。此外,Platform屬性還提供了兩個(gè)參數(shù),Exclude和Include,前者用于指定將要忽略的平臺(tái),后者指定將要運(yùn)行的平臺(tái)。后者與默認(rèn)指定的情況一致。
    示例:
VB代碼:
<TestFixture()> _
Public Class Test
<Test(), Platform(Exclude:="Win98,Linux")> _
Public Sub PlatformTest1()
End Sub
 
<Test(), Platform(Include:="Win98,Linux")> _
Public Sub PlatformTest2()
End Sub
End Class
 
<TestFixture(), Platform("Net-2.0")> _
Public Class Test2
<Test()> _
Public Sub PlatformTest1()
End Sub
 
<Test()> _
Public Sub PlatformTest2()
End Sub
End Class
C#代碼:
[TestFixture]
public class Test
{
[Test, Platform(Exclude="Win98,Linux")]
public void PlatformTest1()
{}
 
[Test, Platform(Include="Win98,Linux")]
public void PlatformTest2()
{}
}
 
[TestFixture, Platform("Net-2.0")]
public class Test2
{
[Test]
public void PlatformTest1()
{}
 
[Test]
public void PlatformTest2()
{}
}
J#代碼:
/** @attribute TestFixture() */
public class Test {
/** @attribute Test() ,Platform(Exclude="Win98,Linux") */
public void PlatformTest1()
{}
 
/** @attribute Test() ,Platform(Include="Win98,Linux") */
public void PlatformTest2()
{}
}
 
/** @attribute TestFixture(), Platform("Net-2.0") */
public class Test2 {
/** @attribute Test() */
public void PlatformTest1()
{}
 
/** @attribute Test() */
public void PlatformTest2()
{}
}
 測(cè)試效果:類Test所作用的是方法,測(cè)試后PlatformTest1前的圓點(diǎn)為綠色,PlatformTest2前的圓點(diǎn)為黃色,即在測(cè)試中被忽略,這是因?yàn)槲覟镻latformTest1指定的參數(shù)是Exclude,而PlatformTest2的參數(shù)是Include,因此在當(dāng)前的.net2.0平臺(tái)下,不運(yùn)行于Win98和Linux平臺(tái)的PlatformTest1能夠順利通過(guò),而PlatformTest2則遭到忽略;類Test2所作用的是類,這里默認(rèn)指定運(yùn)行平臺(tái)為.net2.0,而當(dāng)前運(yùn)行平臺(tái)剛好是.net2.0,因此兩個(gè)方法都會(huì)運(yùn)行通過(guò)。
    此外,NUnit還給出了平臺(tái)的參考,摘錄如下:
Win     Win32       Win32S      Win32Windows        Win32NT
WinCE   Win95       Win98       WinMe               NT3
NT4     NT5         Win2K       WinXP               Win2003Server
Unix    Linux       Net         Net-1.0             Net-1.1
Net-2.0 NetCF       SSCLI       Rotor               Mono
  八、版本
    《NUnit學(xué)習(xí)筆記》,2005年7月24日,NUnit版本2.2.0,IDE為Visual Studio.net 2003。
    《NUnit學(xué)習(xí)筆記 VS.net 2005篇》,2006年1月28日,NUnit版本2.2.6,IDE為Visual Studio.net 2005。
    《NUnit學(xué)習(xí)筆記 進(jìn)階篇》,2006年1月29日,NUnit版本2.2.6,IDE為Visual Studio.net 2005和Delphi 2006。

上一頁(yè)1234567下一頁(yè)
軟件測(cè)試工具 | 聯(lián)系我們 | 投訴建議 | 誠(chéng)聘英才 | 申請(qǐng)使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd