使用VS2013进行单元测试

这次的作业安装了VS2013,对于它的安装过程我就不再细说了,归结起来就是一个字——等,尤其是语言包,最后只好放弃了装语言包,凭借我3级半的英语水平,明白这些没有问题——这仅仅个玩笑话,其实我是用有道词典,好用又方便,申明一下啊,这绝对不是打广告!好吧,为了证明我的确下载了VS2013,我有图为证。

装完VS2013后我开始了我的测试之路,打开之后我开始百度如何建一个C#项目,后面又参照这个同学的进行了测试(http://www.cnblogs.com/dreamq/p/5299080.html)

1.第一步先生成一个"Unit Test Generator"插件,具体做法就是TOOLS->Extension and Updates,然后看看有没有这个图标是试管的插件,没有的话点左边框的Online,在右边搜素的位置输入插件名字,安装后点Restart now重新启动即可。

把这个装好后就可以进行单元测试了。

2.打开FILE->New->projectho后按照下图箭头所指的操作。

执行完上面步骤之后可见下图

之后在这里面编写代码,把要测试的程序的类和函数写在这里边,如我所示,就是个简单的加法。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace test
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13         }
14     }
15     public class measurement
16     {
17         public measurement()
18         {
19 
20         }
21         public int add(int a, int b) 
22         {
23              return a + b;        
24         }
25     }
26 }

写完后随便找一空地点鼠标右键,出现下图,之后点击图标为试管的选项

OK之后会生成一个新的项目

再在measurement()函数里添加测试的数值和其正确结果

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using test;
 7 using Microsoft.VisualStudio.TestTools.UnitTesting;
 8 namespace test.Tests
 9 {
10     [TestClass()]
11     public class measurementTests
12     {
13         [TestMethod()]
14         public void measurementTest()
15         {
16              int a = 1, b = 2, expect = 3;
17              measurement t = new measurement();
18              int real = t.add(a, b);
19              Assert.AreEqual(real, expect);
20             
21         }
22     }
23 }

在空白处点鼠标右键,然后Run Tests,运行后得到下图最下框里的结果,或者右键点击TEST->Windows->Test Explorer。

最终的出了正确的结果。

这就是我的VS2013的单元测试,如有不足之处,请多多包涵!

原文地址:https://www.cnblogs.com/congshen/p/5303053.html