NUnit2.0使用简单教程

今天想测试一下类库的方法.用到了nunit,所以在网上搜索,下面这篇非常简单.告诉了最基本的使用方法.

首先到

1.      http://charliepoole.org/cp.php?p=downloads

2.      http://www.nunit.org/index.php?p=download

 下载NUnit2.0的安装包,下载后简单安装.

 

然后,打开vs.net2005新建项目,

 

 

这里模板选择类库”,

 

然后,

解决方案栏里添加nunit.framework.dll引用,

 

 

然后,手动添加引用using NUnit.Framework;

建立你的测试类(用于测试其它类)

这里作为sample的测试类为:

    [TestFixture]

    public class Testclass

    {

        [Test]

        public void Testmultiply()

        {

            classtobetested tc1 = new classtobetested(1, 0, 70, 84);

            tc1.multiply();

        }

 

        [Test]

        public void Testdevide()

 

        {

          

 

            classtobetested tc2 = new classtobetested(70,0,71,80);

            tc2.devide();

        }

    }

 

这里注意:标识[TestFixture]指明了紧跟其后的类为用于测试其它类的类,标识[Test]指明了紧跟其后的方法为用于测试其它方法的方法,

 

下面是原始的用于测试的简单被测类:

 

public class classtobetested

    {

            

        int A,B,C,D,temp;

 

        public classtobetested(int a, int b, int c, int d)

        {

            A = a;

            B = b;

            C = c;

            D = d;

        }

 

 

        public void devide()

        {

            if (B == 0)

            {

                Console.Write("被除数不可以为0");

            }

            else

            {

                temp = A / B;

 

                Console.Write("A:" + A.ToString() + "除以B:" + B.ToString() + "的结果是:" + temp.ToString());

            }

            }

       

        public void multiply()

        {

 

            temp = A * B;

            Console.Write("A:"+A.ToString()+"乘以B:"+B.ToString()+"的结果是:"+temp.ToString());

        }

       

}

另注:(测试类与被测类在同一namespace 里面)

 

待测试类代码编写完毕,点击菜单生成->生成解决方案,vs.net2005会在项目目录下的bin\Debug下生成一个dll文件

然后,打开Nunit GUI就用界面

点击File-> Open找到适才生成的dll文件,

打开。即可,致于测试哪一个函数、类,测试结果取决于你写的测试类的内容.

 以上转自http://blueexpanse.bokee.com/viewdiary.15080158.html

posted on 2008-03-31 15:29  书生  阅读(375)  评论(0编辑  收藏  举报

导航