VS2010和NUnit整合

简介:


1

下载安装NUnit(最新win版本为NUnit-2.6.0.12051.msihttp://www.nunit.org/index.php?p=download

 

2

下载并安装VSVisual Nunit 2010插件 

http://visualstudiogallery.msdn.microsoft.com/c8164c71-0836-4471-80ce-633383031099

注:可通过VS视图”->“其他窗口找到并打开该插件(快捷键:Ctrl+F7

 

3

新建测试项目(示例为简单的控制台应用程序),引入nunit.framework类库,默认安装后文件所在路径:

C:\Program Files \NUnit2.6\bin\framework\nunit.framewor.dll

 

4

测试代码准备:

namespace ConsoleApplication1

{

   /// <summary>

   /// 需要测试的方法

   /// </summary>

   public class TestMethods

    {

       public int Add(int a, int b)

       {

           return (a + b);

       }

 

       public bool ReturnBool(int i)

       {

           return (i == 1);

       }

    }

}

 

using NUnit.Framework;

 

namespace ConsoleApplication1

{

    [TestFixture]

   class Program

    {

       [Test]

       public void TestAdd01()

       {

           TestMethods t = new TestMethods();

           int result = t.Add(1, 1);

           Assert.AreEqual(2, result);

       }

 

       [Test]

       public void TestAdd02()

       {

           TestMethods t = new TestMethods();

           int result = t.Add(1, 1);

           Assert.AreEqual(3, result);

       }

 

       [Test]

       public void TestBool01()

       {

           TestMethods t = new TestMethods();

           Assert.IsTrue(t.ReturnBool(1));

       }

 

       [Test]

       public void TestBool02()

       {

           TestMethods t = new TestMethods();

           Assert.IsTrue(t.ReturnBool(2));

       }

  

       // 因为是控制台应用程序,所以需要一个入口

       public static void Main(String[] args)

       {

       

       }

    }

}

 

5 测试开始:



点击Run,正确与否立刻现行:

 

看结果:



我们在测试程序中故意出错的打了叉。简单吧。当然,还需要深入研究。

 

值得注意的是,我们在对真是项目测试的时候,有时候需要用到数据库链接,而我们一般将数据库链接放到Web.config里面,在通过

System.Configuration.ConfigurationSettings.AppSettings["con"].ToString()去取得。但是这样在测试中是读不到这个值的,所以测试的时候要把链接写成固定字符串。

 

 

参考地址:

http://www.cnblogs.com/jeffwongishandsome/archive/2012/03/18/2404845.html


目录
相关文章
|
测试技术 程序员 数据库连接
|
测试技术 开发工具 IDE
|
测试技术 C# 数据库连接
|
测试技术 程序员 C++