日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > asp.net >内容正文

asp.net

.NET Compact Framework下的单元测试

發(fā)布時(shí)間:2024/4/14 asp.net 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 .NET Compact Framework下的单元测试 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

在 Wince和Windows Mobile下native C++的單元測(cè)試?里講述了在Wince和Windows Mobile下native C++進(jìn)行單元測(cè)試的方法,這篇將會(huì)講述.NET Compact Framework下的單元測(cè)試。在.NET Compact Framework下可以使用NUintLite進(jìn)行單元測(cè)試。

NUintLite是簡(jiǎn)化版的NUnit,可以應(yīng)用于.NET Compact Framework,Mono等平臺(tái)。

生成NUnitLite庫(kù)

NUintLite已經(jīng)從codeplex遷移到launchpad.net/nunitlite,但是一直沒(méi)有release,所以本文使用最后的elease版本 NUnitLite-0.2.0.zip,下載地址為http://nunitlite.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=6568

解壓源代碼,打開(kāi)src\NUnitLiteCF目錄下的項(xiàng)目文件,編譯生成NUnitLite.dll。


使用NUnitLite

在使用NUnitLite的項(xiàng)目中添加對(duì)NUnitLite.dll的引用。在Main函數(shù)加入Test Runner

static?void?Main(string[]?args)
{
????????????System.IO.TextWriter?writer?
=?new?System.IO.StreamWriter("\\Test\\TestResult.txt");
????????????
new?NUnitLite.Runner.TextUI(writer).Execute(args);
????????????writer.Close();

}

?

NUnitLite的Test Runner支持不同的輸出,TextUI輸出到文件,ConsoleUI輸出到控制臺(tái)(Console),DebugUI輸出Debug信息,新版本還支持TcpUI把結(jié)果輸出通過(guò)TCP發(fā)送。

下面以SqlCeHelper的單元測(cè)試作為例子。原文可見(jiàn) .NET Compact Framework下SQL CE的使用

????using?NUnit.Framework;

????[TestFixture]
????
class?SqlCeHelperTest
????{
????????
private?SqlCeHelper?sqlCe?=?new?SqlCeHelper();

????????[SetUp]
????????
public?void?SetUp()
????????{
????????????sqlCe.Open();
????????}

????????[TearDown]
????????
public?void?TearDown()
????????{
????????????sqlCe.Close();
????????}
?
????????[Test]
????????
public?void?Test()
????????{
????????}
????}

?

編寫(xiě)測(cè)試案例(Test Cases)必須定義Test Fixture,在NUnitLite里使用屬性[TestFixture]標(biāo)識(shí)該類為T(mén)est Fixture。具體的測(cè)試就是該類的方法(Methods)。NUnitLite支持SetUp和TearDown,SetUp在執(zhí)行測(cè)試案例之前先執(zhí)行,用于初始化和分配資源,可以通過(guò)屬性[SetUp]指定SetUp方法;TearDown在執(zhí)行完測(cè)試案例后執(zhí)行,用于釋放相應(yīng)的資源,可以使用屬性[TearDown]指定TearDown方法。屬性[Test]用于定義需要執(zhí)行的測(cè)試案例,Test Runner會(huì)執(zhí)行Test Fixture下所有的[Test]方法。

下面為測(cè)試案例的編寫(xiě),也就是[Test]方法。

????????[Test]
????????
public?void?ExecuteNonQueryTest()
????????{
????????????
int?i?=?sqlCe.ExecuteNonQuery("delete?from?t");
????????????Assert.That(i,?Is.GreaterThan(
-1));

????????????i?
=?sqlCe.ExecuteNonQuery("insert?into?t?(f1,?f2)?values?(1,?'abc')");
????????????Assert.That(i,?Is.GreaterThan(
-1));

????????????i?
=?sqlCe.ExecuteNonQuery("update?t?set?f2?=?'xyz'?where?f1?=?1");
????????????Assert.That(i,?Is.GreaterThan(
-1));
????????}

????????[Test]
????????
public?void?ExecuteReaderTest()
????????{
????????????SqlCeDataReader?reader?
=?sqlCe.ExecuteReader("select?*?from?t");
????????????Assert.NotNull(reader);
????????????Assert.True(
!reader.IsClosed);
????????????
while?(reader.Read())
????????????{
????????????????Assert.That(Is.Equals(reader[
"f2"],?"abc"));
????????????}
????????????reader.Close();
????????}

????????[Test]
????????
public?void?ExecuteDataSetTest()
????????{
????????????DataSet?ds?
=?sqlCe.ExecuteDataSet("select?*?from?t");
????????????
foreach?(DataRow?dr?in?ds.Tables[0].Rows)
????????????{
????????????????Assert.That(dr[
"f2"],?Is.EqualTo("xyz"));
????????????}
????????}

????????[Test]
????????[ExpectedException(
typeof(ApplicationException))]
????????
public?void?ThrowsException()
????????{
????????????
throw?new?ApplicationException("an?application?exception");
????????}

在NUnitLite中進(jìn)行檢驗(yàn)值還是使用Assert,但是不能使用NUnit下的AreEquels等判斷方法,需要使用Assert.That和Is類組合完成AreEquels等判斷函數(shù)的功能。同時(shí)NUnitLite還是支持異常的捕捉(ExpectedException)。

?

程序運(yùn)行后會(huì)生成運(yùn)行結(jié)果如下:

NUnitLite?version?0.2.0
Copyright?
2007,?Charlie?Poole

Runtime?Environment?
-
????OS?Version:?Microsoft?Windows?CE?
5.0.0
??.NET?Version:?
2.0.7045.0

4?Tests?:?0?Errors,?1?Failures,?0?Not?Run

Errors?and?Failures:

1)?ExecuteReaderTest?(TestNameSpace.SqlCeHelperTest.ExecuteReaderTest)
??Expected:?True
??But?was:??False

at?TestNameSpace.SqlCeHelperTest.ExecuteReaderTest()
at?System.Reflection.RuntimeMethodInfo.InternalInvoke()
at?System.Reflection.RuntimeMethodInfo.InternalInvoke()
at?System.Reflection.RuntimeMethodInfo.Invoke()
at?System.Reflection.MethodBase.Invoke()
at?NUnitLite.Reflect.InvokeMethod()
at?NUnitLite.TestCase.InvokeMethod()
at?NUnitLite.TestCase.RunTest()
at?NUnitLite.TestCase.RunBare()
at?NUnitLite.TestCase.Run()
at?NUnitLite.TestCase.Run()
at?NUnitLite.TestSuite.Run()
at?NUnitLite.TestSuite.Run()
at?NUnitLite.Runner.TestRunner.Run()
at?NUnitLite.Runner.TestRunner.Run()
at?NUnitLite.Runner.TextUI.Run()
at?NUnitLite.Runner.TextUI.Execute()
at?TestNameSpace.TestForm.Main()

?

參考文獻(xiàn)

http://launchpad.net/nunitlite

NUnitLite: Embedded, readable tests

?Jake's Blog in 博客園 -- 精簡(jiǎn)開(kāi)發(fā) 無(wú)線生活

總結(jié)

以上是生活随笔為你收集整理的.NET Compact Framework下的单元测试的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。