一、环境准备

1. NUnit 2.6.3

下载地址:https://launchpadlibrarian.net/153448476/NUnit-2.6.3.msi

2. VS2010

二、安装 NUnit(略)

三、编写代码

项目结构

NUnit + VS2010 简单入门-LMLPHP

AlgLib 代码

Alg.cs

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace AlgLib
{
/// <summary>
/// 算法库,待测试对象
/// </summary>
public class Alg
{
public int Add(int a, int b)
{
return (a + b);
}
}
}

Singleton.cs

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace AlgLib
{
/// <summary>
/// 单例类,待测试对象
/// </summary>
public sealed class Singleton
{
private static readonly Singleton _instance = new Singleton();
private Guid _guid; private Singleton()
{
_guid = Guid.NewGuid();
} public static Singleton SingletonInstance
{
get { return _instance; }
} public Guid InstanceGuid
{
get { return _guid; }
}
}
}

AlgLibTest 单元测试项目代码

AlgTest.cs

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using AlgLib; namespace AlgLibTest
{
/// <summary>
/// 算法库单元测试
/// </summary>
[TestFixture]
public class AlgTest
{
[Test]
public void TestAdd()
{
Alg alg = new Alg();
var result = alg.Add(, );
Assert.AreEqual(, result); // NUnit报错
}
}
}

SingletonTest.cs

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using AlgLib; namespace AlgLibTest
{
[TestFixture]
public class SingletonTest
{
[Test]
public void TestSingleton()
{
var instanceA = Singleton.SingletonInstance;
var instanceB = Singleton.SingletonInstance; Assert.AreEqual(instanceA.InstanceGuid, instanceB.InstanceGuid);
}
}
}

编译。。。

四、用NUnit 进行单元测试

NUnit + VS2010 简单入门-LMLPHP

NUnit + VS2010 简单入门-LMLPHP

NUnit + VS2010 简单入门-LMLPHP

运行结果

NUnit + VS2010 简单入门-LMLPHP

文章代码下载:

http://pan.baidu.com/s/1o6zCuG6

12-5 新增

附加nuint.exe 进程还能在错误的时候进行代码调试

NUnit + VS2010 简单入门-LMLPHP

05-11 11:32