随笔 - 9  文章 - 0 评论 - 5 阅读 - 9773
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

概述

VS2015这款软件是一款功能非常强大的开发工具,而且还提供了自动测试管理工具。

工具介绍

首先,在VS2015中新建一个类库ClassLibrary2,在类库中新建一个类Class1,在这个类中插入如下代码:

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ClassLibrary2
{
    public class Class1
    {
        private int i;
        public int test()
        {
复制代码
            arr[0] = -1;
            arr[1] = 2;
            arr[2] = 3;
            arr[3] = -1;
            arr[4] = 1;
            ///O(n)时间长度的算法。
            for (i = 0; i < 5; i++)
            {
                if (cha < 0)
                {
                    cha = arr[i];
                }
                else
                {
                    cha = cha + arr[i];
                }
                if (sum < cha)
                {
                    sum = cha;
                }
            }
            ///Console.WriteLine("最大子序列和为{0}", sum);
            return sum;
复制代码
        }
    }
}
复制代码

然后生成一下,右键在文件资源管理器中打开文件夹,进入./bin/Debug文件夹找到ClassLibrary2.dll文件。

再创建一个测试类,直接点击确定。如下图:

在测试类中添加引用,将刚刚创建的ClassLibrary2.dll文件添加到引用中。然后再创建的测试类的UnitTest1.cs中最开始添加代码:

复制代码
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ClassLibrary2;

namespace UnitTestProject1
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            Class1 a = new Class1();
            int jieguo = a.test();
            Assert.AreEqual<Int32>(5, jieguo);
        }
    }
}
复制代码

注意,测试类是不能够直接按F5运行的,需要点击测试。打开测试->窗口->测试资源管理器。

然后点击运行就可以看到测试结果。

详细请参照:http://www.cnblogs.com/heqichang/archive/2011/09/27/2192220.html

posted on   liannngwei  阅读(5265)  评论(1编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示