首页 > 其他 > 详细

NUnit + VS2010 简单入门

时间:2014-12-03 18:50:56      阅读:275      评论:0      收藏:0      [点我收藏+]

一、环境准备 

1. NUnit 2.6.3

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

2. VS2010

 

二、安装 NUnit(略)

 

三、编写代码

项目结构

bubuko.com,布布扣

 

AlgLib 代码

Alg.cs

bubuko.com,布布扣
 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

bubuko.com,布布扣
 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

bubuko.com,布布扣
 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

bubuko.com,布布扣
 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 进行单元测试

bubuko.com,布布扣

bubuko.com,布布扣

bubuko.com,布布扣

 

运行结果

bubuko.com,布布扣

 

文章代码下载:

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

 

NUnit + VS2010 简单入门

原文:http://www.cnblogs.com/Aphasia/p/4140602.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!