.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 . 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()

 

参考文献

http://launchpad.net/nunitlite

NUnitLite: Embedded, readable tests

 Jake's Blog in 博客园 -- 精简开发 无线生活

转载于:https://www.cnblogs.com/procoder/archive/2009/04/08/1431724.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值