对自己要求要更严格一些,对别人要再多宽容一些。

单元测试学习之旅(一)

上一篇 / 下一篇  2009-06-17 21:17:29 / 个人分类:单元测试

    最近在学习单元测试Nunit(当然啦,个人感觉Junit貌似发展得更加成熟了)

    今天就介绍下Nunit框架。

    1、环境:VS 20005,Nunit 2.5

    2、先在VS 2005中新建一个类库项目(供测试使用),于是构造以下供测试使用的类Class1.cs:

    


namespace ClassLibrary1
{

    public class Class1
    {
        public static int Largest(int[] list)
        {
            int index, max = Int32.MinValue;
            for(index=0;index<list.Length;index++)
            {
            if(list[index]>max)
            {
                max=list[index];
            }
            }
            return max;
        }

    }
}

    3、然后就是新建一个测试用的类,Test.cs(这里应该注意的是引入NUnit.FrameWork引用):

   

using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;

namespace ClassLibrary1
{
    [TestFixture]
    class TicketTest
    {
        [Test]
        public void Largesttest()
        {
            //Assert.AreEqual(9, Class1.Largest(new int[] { 8,9,7}));
            Assert.AreEqual(9, Class1.Largest(new int[] { 7, 8, 9 }));
            Assert.AreEqual(9, Class1.Largest(new int[] { 8, 9, 7 }));
            Assert.AreEqual(9, Class1.Largest(new int[] { 9, 8, 7 }));
       
        }
        [Test]
        public void TestOne()
        {
            Assert.AreEqual(9, Class1.Largest(new int[] { 9,7,8,9 }));
        }
        [Test]
        public void TestTwo()
        {
            Assert.AreEqual(1, Class1.Largest(new int[] {1}));
        }
        [Test]
        public void TestThree()
        {
            Assert.AreEqual(-7, Class1.Largest(new int[] {-7, -8, -9 }));
        }
        [Test]
        public void TestFour()
        {
            int [] array = new int[] { -9,-8,-7};
            Assert.AreEqual(-7, Class1.Largest(array));
        }

    }
}

      4、编译该类,生成对应的dll,如图所示:

     

      5、在NUnit中,运行该dll文件,选中某个断言,点击运行(其实一个断言就有点想一个用例那样),如图所示:

    

 

 

     


TAG:

引用 删除 xiaoler   /   2010-08-23 16:26:09
使用过Nunit,主要针对C#的单元测试,如果针对C语言需要用到CppUnit,建议看看C++test专业测试博客上面的一些论述:www.mytestlife.com
 

评分:0

我来说两句

日历

« 2024-05-12  
   1234
567891011
12131415161718
19202122232425
262728293031 

数据统计

  • 访问量: 29261
  • 日志数: 52
  • 文件数: 1
  • 书签数: 4
  • 建立时间: 2009-04-20
  • 更新时间: 2010-02-02

RSS订阅

Open Toolbar