[转]NUnit快速入门

From: http://www.fjshop.org/mic/SupportInfo/NUnitQuickStart.htm
    
    为了让学员快速掌握Nunit的用法,我们整理了Nunit Quick Start的相关内容,并与实训实际情况相结合,指导学生如何对.Net程序进行单元测试。
    假设我们要测试一个类:Account。Account具有存款、取款和资金转帐功能。Account类的代码如下:

namespace bank
{
   public class Account
   {
      private float balance;
      private float minimumBalance = 10.00F;
      public float MinimumBalance
    {
     get{ return minimumBalance;}
    }
      public void Deposit(float amount)
      {
         balance+=amount;
       }
      public void Withdraw(float amount)
      {
         balance-=amount;
       }
      public void TransferFunds(Account destination, float amount)
      {
        destination.Deposit(amount);
        if(balance-amount<minimumBalance)
        throw new InsufficientFundsException();
        Withdraw(amount);
      }
        public float Balance
       {
          get{ return balance;}
        }
        public class InsufficientFundsException : ApplicationException
   {
    }
   }
}

   Account类有三个方法:Deposit(存款), Withdraw(取款), TransferFunds(资金转帐)和两个属性:Balance (剩余金额),MinimumBalance(最小透支保护费)。当Balance小于MinimumBalance时,在TransferFunds 方法中会抛出InsufficientFundsException异常

  现在我们来为这个类写一个测试类——AccountTest。我们第一个要测试的Account类中的方法是TransferFuncs。代码如下:

namespace bank
{
  using NUnit.Framework;
  [TestFixture]
  public class AccountTest
  {
    [Test]
    public void TransferFunds()
    {
      Account source = new Account();
      source.Deposit(200.00F);
      Account destination = new Account();
      destination.Deposit(150.00F);
      source.TransferFunds(destination, 100.00F);
      Assert.AreEqual(250.00F, destination.Balance);
      Assert.AreEqual(100.00F, source.Balance);
    }
  }
}

   首先要注意的是这个测试类关联了一个[TestFixture]特性(attribute),这表示这个类包含了测试代码(这个特性可以被继承)。这个类必须是公有的,但他的父类并不受限制。这个类还必须有一个默认构造函数。

   测试类中的方法TransferFunds关联了一个[Test]特性,这表示它是一个测试方法。测试方法的返回值必须为void并且不能带有参数。在我们的测试方法中,我们对被测试的对象进行了一般的初始化,执行了被测试的方法并检查了对象的状态。Assert类定义了一组方法用于检查给定的条件,在我们的例子中我们使用了AreEqual方法来确保交易过后两个账户都有正确的余额(这个方法有很多重载,我们在这个例子中使用的版本带有两个参数:第一个参数是我们的期望值,第二个参数是实际值)。

  编译并运行这个例子。假设你已经将你的测试代码编译为bank.dll。打开NUint Gui(安装程序会在你的桌面和“程序”菜单中建立一个快捷方式),打开GUI后,选择File->Open菜单项,找到你的bank.dll并在“Open”对话框中选中它。bank.dll装载后你会在左边的面板中看到一个测试树结构,还有右边的一组状态面板。单击Run按钮,状态条和测试树种的TransferFunds节点变成了绿色,表示测试代码成功的通过了。(注意GUI会自动地为你重新加载测试程序集;我们可以一直开着NUnit 的GUI而在Visual Studio IDE中继续编写更多的测试代码)。

  下一步我们测试类Account的方法TransferFunds是否会抛出InsufficientFundsException异常。

  向我们的AccountTest类添加一个新的方法,代码如下:

[Test]
[ExpectedException(typeof(InsufficientFundsException))
public void TransferWithInsufficientFunds()
{
       Account source = new Account();
       source.Deposit(200.00F);
       Account destination = new Account();
       destination.Deposit(150.00F);
       source.TransferFunds(destination, 300.00F);
}

   这个测试方法除了[Test]特性之外还关联了一个[ExpectedException]特性——这指出测试代码希望抛出一个指定类型的异常;如果在执行过程中没有抛出这样的一个异常——该测试将会失败。编译代码并回到GUI。由于你编译了你的测试代码,GUI会变灰并重构了测试树,好像这个测试还没有被运行过(GUI可以监视测试程序集的变化,并在测试树结构发生变化时进行更新——例如,添加了新的测试)。点击“Run”按钮,同样显示绿色。不过仔细看类Account中TransferFunds方法的代码,我们会发现银行在每一笔不成功的转账操作(Balance小于MinimumBalance)时都亏钱了,因为接收转帐的人的存款增加了但转帐的人钱没有减少(WithDraw方法没有被执行)。让我们来写一个测试来确认我们的猜测,添加这个测试方法:

[Test]
public void TransferWithInsufficientFundsAtomicity()
{
   Account source = new Account();
   source.Deposit(200.00F);
   Account destination = new Account();
   destination.Deposit(150.00F);
   try
   {
      source.TransferFunds(destination, 300.00F);
   }
   catch(InsufficientFundsException expected)
   {
   }
   Assert.AreEqual(200.00F,source.Balance);
   Assert.AreEqual(150.00F,destination.Balance);
}

   编译代码并回到GUI,点击“Run”按钮,显示红条(出错信息显示destination帐户的钱不是150.00而是450.00)。银行平白无故地损失了300块钱:source账户有正确的余额200.00,但destination账户的余额是450.00。

   这时我们知道程序有个漏洞,这里我们不讨论如何修改程序,那如何使眼前的测试代码通过呢?删除它?一个不错的方法是临时忽略它。在你的测试方法中添加下面的特性:

[Test]
[Ignore("Need to decide how to implement transaction management in the application")]
public void TransferWithInsufficientFundsAtomicity()
{
  // code is the same
}

    编译并运行,显示一个黄条。单击“Test Not Run”选项卡,你会看到bank. Account Test. Transfer With Insufficient Funds Atomicity()连同这个测试被忽略的原因一起列在列表中。

  下面的代码是完整的测试代码(将一些初始化代码放到一个setup方法中并在所有的测试中重用它们):

namespace bank
{
   using System;
   using NUnit.Framework;
   [TestFixture]
   public class AccountTest
   {
      Account source;
      Account destination;
      [SetUp]
      public void Init()
      {
         source = new Account();
         source.Deposit(200.00F);
         destination = new Account();
         destination.Deposit(150.00F);
      }
      [Test]
      public void TransferFunds()
      {
         source.TransferFunds(destination, 100.00f);
         Assert.AreEqual(250.00F, destination.Balance);
         Assert.AreEqual(100.00F, source.Balance);
      }
      [Test]
      [ExpectedException(typeof(InsufficientFundsException))]
      public void TransferWithInsufficientFunds()
      {
         source.TransferFunds(destination, 300.00F);
      }
      [Test, Ignore("Need to decide how to implement transaction management in the application")]
      public void TransferWithInsufficientFundsAtomicity()
      {
        try
        {
           source.TransferFunds(destination, 300.00F);
         }
         catch(InsufficientFundsException expected)
        {
         }
         Assert.AreEqual(200.00F,source.Balance);
         Assert.AreEqual(150.00F,destination.Balance);
      }
   }
}

   注意初始化方法Init拥有通用的初始化代码,它的返回值类型为void,没有参数,并且由[SetUp]特性标记。

原文地址:https://www.cnblogs.com/SummerRain/p/1109323.html