小寒的blog
Programming is a darned hard thing—but I am going to like it.

使用NUnit进行DotNet程序测试

作者:kongxx

介绍

NUnit是目前比较流行的.Net平台的测试工具,以下就简单介绍一下他的开发。

准备

要使用NUnit,首先要确保您的机器上有NUnit的开发包,您可以从http://www.nunit.org/

地方获取并安装(目前版本是NUnit v2.1.91)。正确安装后会在开始菜单下添加一个NUnit 2.2项目。

属性说明

在开始写例子之前,先把NUnit的属性说明一下:

TestFixture (NUnit2.0)

标识当前类是一个包含测试方法的类。

注意:这个类必须有一个默认的构造方法,并且也必须声明成Public

例如

namespace NUnit.Tests {
  using System;
  using NUnit.Framework;
  [TestFixture]
  public class SuccessTests {
    // ...
  }

}

Test (NUnit2.0)

标识一个使用TestFixture标识的类中的方法是一个需要测试的方法。

注意:方法的签名被标识为无返回值。

例如:

namespace NUnit.Tests {
  using System;
  using NUnit.Framework;
  [TestFixture]
  public class SuccessTests {
[Test]
public void Add()
    { /* ... */ }
    public void TestSubtract()
    { /* backwards compatibility */ }
  }
}

SetUp/TearDown

TestFixtureSetUp/SetUp用来在运行测试方法之前构造环境;

TestFixtureTearDown/TearDown用来在运行测试方法之后还原环境。

注意:一个测试类(标识TestFixture)中只可以有一对标记。

TestFixtureSetUp/TestFixtureTearDown (NUnit2.1)

例如:

namespace NUnit.Tests {
  using System;
  using NUnit.Framework;
  [TestFixture]
  public class SuccessTests {
    [TestFixtureSetUp] public void Init()
    { /* ... */ }
    [TestFixtureTearDown] public void Dispose()
    { /* ... */ }
    [Test] public void Add()
    { /* ... */ }
  }
}

SetUp/TearDown (NUnit2.0)

例如:

namespace NUnit.Tests {
  using System;
  using NUnit.Framework;
  [TestFixture]
  public class SuccessTests {
    [SetUp] public void Init()
    { /* ... */ }
    [TearDown] public void Dispose()
    { /* ... */ }
    [Test] public void Add()
    { /* ... */ }
  }
}

ExpectedException (NUnit2.0)

指定一个测试将要抛出的异常。

namespace NUnit.Tests {
  using System;
  using NUnit.Framework;
  [TestFixture]
  public class SuccessTests {
    [Test]
    [ExpectedException(typeof(InvalidOperationException))]
    public void ExpectAnException()
    { /* ... */ }
  }

}

Category NUnit2.2

标识在一组测试中选中的测试。

当使用此属性是,只有选中的Category才会被调用。

例如:

TestFixture上使用Category属性

namespace NUnit.Tests{
  using System;
  using NUnit.Framework;
  [TestFixture]
  [Category("LongRunning")]
  public class LongRunningTests
  {/*…*/}
}

Test上使用Category属性

namespace NUnit.Tests {
  using System;
  using NUnit.Framework;
  [TestFixture]
  public class SuccessTests {
    [Test]
    [Category("Long")]
    public void VeryLongTest()
    { /* ... */ }

}

ExplicitNUnit2.2

指定一个TestTestFixture被排除在测试选中的测试中。

例如:

TestFixture上使用Explicit属性

namespace NUnit.Tests {
  using System;
  using NUnit.Framework;
  [TestFixture, Explicit]
  public class ExplicitTests
  {/* ... */}
}

Test上使用Explicit属性

namespace NUnit.Tests {
  using System;
  using NUnit.Framework;
  [TestFixture]
  public class SuccessTests {
    [Test, Explicit]
    public void ExplicitTest()
    { /* ... */ }
}

Suite NUnit2.0

标识一个测试单元。

????

例如:

namespace NUnit.Tests {
  using System;
  using NUnit.Framework;
  public class AllTests {
    [Suite]
    public static TestSuite Suite {
      get {
        TestSuite suite = new TestSuite("All Tests");
        suite.Add(new OneTestCase());
        suite.Add(new Assemblies.AssemblyTests());
        suite.Add(new AssertionTest());
        return suite;
      }
    }
  }

}

IgnoreNUnit2.0

在一定的时间内标识TestTestFixture不运行。

例如:

namespace NUnit.Tests {
  using System;
  using NUnit.Framework;
  [TestFixture]
  [Ignore("Ignore a fixture")]
  public class SuccessTests
{/* ... */}

}

简单例子

首先用VS.Net建立一个控制台应用程序项目(TestNUnit),然后在项目中添加引用,选中nunit.framework可以在NUnit的安装目录下找到),然后添加两个类,一个是待测试的类,一个是测试类,

待测试的类内容如下:

using System;

namespace TestNUnit.Sample1

{

    public class Sample1 {

        public Sample1() {

           

        }

        public String GetHelloWorld() {

            return "Hello World!";

        }

        public int Add(int i1 ,int i2) {

            return i1 + i2 ;

        }

        public int Minus(int i1 ,int i2) {

            return i1 - i2 ;

        }

    }

}

测试类内容如下:

using System;

using NUnit.Framework;

namespace TestNUnit.Sample1 {  

    [TestFixture]    //--------------------------------------------1

    public class TestSample1 {     

        private Sample1 sample ;       

        [SetUp]     //--------------------------------------------2

        public void Init() {           

            this.sample = new Sample1();;

        }

        [TearDown]  //--------------------------------------------3

        public void TearDown() {           

            this.sample = null ;

        }

        [Test]      //--------------------------------------------4

        public void TestGetHelloWorld() {

            Assert.IsNotNull(this.sample.GetHelloWorld());

            Assert.AreEqual("Hello World!" ,this.sample.GetHelloWorld());

        }

        [Test]

        public void TestAdd() {

            Assert.AreEqual(2,this.sample.Add(1,1));

            Assert.AreEqual(3,this.sample.Add(1,2));

            Assert.AreEqual(4,this.sample.Add(2,2));

        }

        [Test]

        public void TestMinus() {

            Assert.AreEqual(0,this.sample.Minus(1,1));

            Assert.AreEqual(1,this.sample.Minus(2,1));

            Assert.AreEqual(-1,this.sample.Minus(1,2));

        }

    }

}

注意测试类(TestSample1)中几个特殊的地方:

1表示当前类是一个测试类;

2表示测试启动前要执行的操作;

3表示测试后要执行的操作;

4表示具体的每个测试方法,这里每个方法都对应要测试类中的方法。

编译以上类,运行NUnitGUI界面(开始->NUnit 2.2->Nunit-Gui),选择File->Open,打开刚才编译项目生成的文件,这里选中TestNUnit.exe(根据具体应用可以是DLL或者别的类型)文件,此时出现一下窗口:                                            

点击Run按钮,出现以下界面:

当运行栏全部是绿色的时候,表示写的测试全部通过,如果出现运行栏显示红色,表示测试出现问题,需要我们修改。

此时表示有两个方法(TestAdd,TestMinus)测试出现问题,需要我们去检查修改,然后重复以上的操作,直到运行栏全部不在显示红色为止。

Mock测试

简介

Mock测试就是在测试过程中,对于某个不容易构造或获取的对象,用一个虚假的对象来创建以方便测试的测试方法。

要使用Mock测试,需要有专门的Mock测试开发包支持,以下使用Nmock来说明。要获得Nmock可以从http://www.nmock.org/获得,目前使用NMock V1.1

示例

添加引用

在项目中添加引用nmock.dll,可以从http://www.nmock.org/获得。

代码

需要被Mock的类:

using System;

namespace TestNUnit.MockSample {

    public class BigClass {

   public virtual string DoSoming(string hello,string name,string symbol){

           return "hello" + " " + name + symbol;

       }

       public virtual void DoNoing() {

           //TODO

       }

    }

}

被测试的类:

using System;

namespace TestNUnit.MockSample {

    public class Sample {

       private BigClass bc ;

       public Sample(BigClass bc) {

           this.bc = bc ;

       }

       public string GetHelloWorld() {

           return "Hello World!";

       }  

       public string GenerateHelloWorld(string hello,string name ,string symbol) {

           return this.bc.DoSoming("Hello",name,symbol);

       }

    }

}

测试类:

using System;

using NUnit.Framework;

using NMock ;

using NMock.Constraints;

namespace TestNUnit.MockSample {

    [TestFixture]

    public class TestMockSample {

       private Mock mock ;

       private Sample sample ;    

       [SetUp]

       public void Init() {       

           this.mock = new DynamicMock(typeof(BigClass));

           this.sample = new Sample((BigClass)mock.MockInstance);

       }

       [TearDown]

       public void TearDown() {

           mock.Verify();

       }

       [Test]

       public void TestGetHelloWorld() {

           this.mock.ExpectNoCall("DoSoming", typeof(String),typeof(String),typeof(String));

           this.mock.ExpectNoCall("DoNoing");

           Assert.IsNotNull(this.sample.GetHelloWorld());

           Assert.AreEqual(this.sample.GetHelloWorld() ,"Hello World!" );       

       }

       [Test]

       public void TestGenerateHelloWorld() {

           IsEqual hello = new IsEqual("Hello");

           IsAnything name = new IsAnything();

           IsEqual symbol    = new IsEqual("!");

           this.mock.ExpectAndReturn("DoSoming","Hello kongxx!" ,hello, name, symbol);

           this.mock.ExpectNoCall("DoNoing");    

           Assert.AreEqual("Hello kongxx!",sample.GenerateHelloWorld("Hello","kongxx","!"));

       }

    }

}

运行

编译以上类,然后运行NUnit的图形界面,载入编译过的程序(exedll),出现以下界面:

然后运行,出现以下界面:

如果运行栏全部显示绿色表示通过测试,否则检查错误,修改,编译,运行直到全部运行成功为止。

参考资料

1NUnit http://www.nunit.org/

2Nmock http://www.nmock.org/index.html

3mockobjects http://www.mockobjects.com/FrontPage.html



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=55742


posted on 2007-12-20 11:30  xhan  阅读(457)  评论(0编辑  收藏  举报