现在的位置: 首页 > 综合 > 正文

使用NUnit进行DotNet程序测试

2013年02月13日 ⁄ 综合 ⁄ 共 4104字 ⁄ 字号 评论关闭

使用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));

抱歉!评论已关闭.