.NET Compact Framework下的单元测试
在 Wince和Windows Mobile下native C++的單元測試?里講述了在Wince和Windows Mobile下native C++進行單元測試的方法,這篇將會講述.NET Compact Framework下的單元測試。在.NET Compact Framework下可以使用NUintLite進行單元測試。
NUintLite是簡化版的NUnit,可以應用于.NET Compact Framework,Mono等平臺。
生成NUnitLite庫
NUintLite已經從codeplex遷移到launchpad.net/nunitlite,但是一直沒有release,所以本文使用最后的elease版本 NUnitLite-0.2.0.zip,下載地址為http://nunitlite.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=6568
解壓源代碼,打開src\NUnitLiteCF目錄下的項目文件,編譯生成NUnitLite.dll。
使用NUnitLite
在使用NUnitLite的項目中添加對NUnitLite.dll的引用。在Main函數加入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輸出到控制臺(Console),DebugUI輸出Debug信息,新版本還支持TcpUI把結果輸出通過TCP發送。
下面以SqlCeHelper的單元測試作為例子。原文可見 .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()
????????{
????????}
????}
?
編寫測試案例(Test Cases)必須定義Test Fixture,在NUnitLite里使用屬性[TestFixture]標識該類為Test Fixture。具體的測試就是該類的方法(Methods)。NUnitLite支持SetUp和TearDown,SetUp在執行測試案例之前先執行,用于初始化和分配資源,可以通過屬性[SetUp]指定SetUp方法;TearDown在執行完測試案例后執行,用于釋放相應的資源,可以使用屬性[TearDown]指定TearDown方法。屬性[Test]用于定義需要執行的測試案例,Test Runner會執行Test Fixture下所有的[Test]方法。
下面為測試案例的編寫,也就是[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中進行檢驗值還是使用Assert,但是不能使用NUnit下的AreEquels等判斷方法,需要使用Assert.That和Is類組合完成AreEquels等判斷函數的功能。同時NUnitLite還是支持異常的捕捉(ExpectedException)。
?
程序運行后會生成運行結果如下:
NUnitLite?version?0.2.0Copyright?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()
?
參考文獻
http://launchpad.net/nunitlite
NUnitLite: Embedded, readable tests
?Jake's Blog in 博客園 -- 精簡開發 無線生活
總結
以上是生活随笔為你收集整理的.NET Compact Framework下的单元测试的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: BizTalk开发系列(十九) BizT
- 下一篇: Windows Mobile下C++取屏