NUnit + VS2010 简单入门

一、环境准备 

1. NUnit 2.6.3

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

2. VS2010

 

二、安装 NUnit(略)

 

三、编写代码

项目结构

 

AlgLib 代码

Alg.cs

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace AlgLib
 7 {
 8     /// <summary>
 9     /// 算法库,待测试对象
10     /// </summary>
11     public class Alg
12     {
13         public int Add(int a, int b)
14         {
15             return (a + b);
16         }
17     }
18 }
View Code

Singleton.cs

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace AlgLib
 7 {
 8     /// <summary>
 9     /// 单例类,待测试对象
10     /// </summary>
11     public sealed class Singleton
12     {
13         private static readonly Singleton _instance = new Singleton();
14         private Guid _guid;
15 
16         private Singleton()
17         {
18             _guid = Guid.NewGuid();
19         }
20 
21 
22         public static Singleton SingletonInstance
23         {
24             get { return _instance; }
25         }
26 
27         public Guid InstanceGuid
28         {
29             get { return _guid; }
30         }
31     }
32 }
View Code

 

AlgLibTest 单元测试项目代码

AlgTest.cs

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using NUnit.Framework;
 6 using AlgLib;
 7 
 8 namespace AlgLibTest
 9 {
10     /// <summary>
11     /// 算法库单元测试
12     /// </summary>
13     [TestFixture]
14     public class AlgTest
15     {
16         [Test]
17         public void TestAdd()
18         {
19             Alg alg = new Alg();
20             var result = alg.Add(1, 2);
21             Assert.AreEqual(2, result); // NUnit报错
22         }
23     }
24 }
View Code

SingletonTest.cs

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using NUnit.Framework;
 6 using AlgLib;
 7 
 8 namespace AlgLibTest
 9 {
10     [TestFixture]
11     public class SingletonTest
12     {
13         [Test]
14         public void TestSingleton()
15         {
16             var instanceA = Singleton.SingletonInstance;
17             var instanceB = Singleton.SingletonInstance;
18 
19             Assert.AreEqual(instanceA.InstanceGuid, instanceB.InstanceGuid);
20         }
21     }
22 }
View Code

编译。。。

 

四、用NUnit 进行单元测试

 

运行结果

 

文章代码下载:

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

 

12-5 新增

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

posted on 2014-12-03 17:13  ~江慕白~  阅读(280)  评论(0编辑  收藏  举报

导航