.NET Compact Framework下使用NUintLite进行单元测试(在WinCE上)

这里有一个关键就是.NET Compact Framework的版本一定要和NUintLite的版本一致,否则将无法进行测试。

在WinCE的“Program.cs”中添加如下代码:

        static void Main(string[] args)
        {
            string strPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
            System.IO.TextWriter writer = new System.IO.StreamWriter(strPath + @"\Test\TestResult.txt");
            new NUnitLite.Runner.TextUI(writer).Execute(args);
            writer.Close();
        }

 

最新的NUintLite的DLL下载地址为:https://launchpad.net/nunitlite/+download 版本号0.9.0

我已将此内容上传至我的资源,地址为:http://download.csdn.net/detail/esunshine1985/5554021

 

以下内容为粘贴部分:

        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

  解压源代码,打开srcNUnitLiteCF目录下的项目文件,编译生成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("deletefrom t");
  Assert.That(i,
Is.GreaterThan(-1));
  i
= sqlCe.ExecuteNonQuery("insertinto t (f1, f2)values (1,"abc")");
  Assert.That(i,
Is.GreaterThan(-1));
  i
= sqlCe.ExecuteNonQuery("update tset 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,0Not 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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值