引言
  不要再犹豫写单元测试是否浪费时间,是否能减少开发过程中的bug,Just do it!
  开发环境及相关配置
  Win7 OS
  Visual Studio 2012 IDE
  .Net Framework 4.5
  Resharper (username:ronle , key:ZoJzmeVBoAv9Sskw76emgksMMFiLn4NM)
  NUnit Framework(Installed by NuGet package manage tool)
  Windows Console Application Project in C# Programming (Just an project example,you could use Unit Testing in other project such as Mvc,WCF,Web Service )
  代码示例
  代码主要功能:通过比较寻找一个list中的大值。
  1.项目代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UnitTestDemo
{
public class Cmp
{
public static int Largest(int[] list)
{
int max = Int32.MinValue;
if (list.Length == 0)
{
throw new AggregateException("Empty list");
}
for (int index=0; index < list.Length; index++)
{
if (list[index] > max)
{
max = list[index];
}
}
return max;
}
}
}
  2.针对项目代码写的测试代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using  UnitTestDemo;
namespace UnitTestDemo.Test
{
[TestFixture]
public class TestCmp
{
[Test]
public void LargestOfNegative()
{
Assert.AreEqual(-7,Cmp.Largest(new int[]{-9,-7,-8}));
}
[Test,ExpectedException(typeof(AggregateException))]
public void LargestOfEmpty()
{
Cmp.Largest(new int[] {});
}
}
}